python how to call static method from inside of a class body

arrowknee

Let's assume I have a class, with a static method, and I want a class property to be set to the value that this method returns:

class A:
    @staticmethod
    def foo():
        return 12

     baz = foo()

But doing this I get an error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in A
TypeError: 'staticmethod' object is not callable

I found a way to get around this:

class A:
    class B:
        @staticmethod
        def foo():
            return 2
baz = B.foo()

But for example if I write:

class A:
    class B:
        @staticmethod
        def foo():
            return 2

    class C:
        baz = B.foo()

I also get an error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 6, in A
  File "<stdin>", line 7, in C
NameError: name 'B' is not defined

Is there a way to call static methods from within a class while declaring it? Why 1st and 3rd examples of code does not work but 2nd does? How python interpretor handles such declarations?

donkopotamus

The staticmethod is a descriptor. A descriptor exposes the __get__(instance, cls) method allowing it to be accessed either through an instance or at the class level.

Now in your case you wish to call it within a class stanza. Normally this would not be possible as neither an instance nor the class are yet available. However a staticmethod ignores both in any case so you can use the following rather nasty approach to call it.

class A:
    @staticmethod
    def foo():
        return 12

    baz = foo.__get__(None, object)()

Then

>>> A.baz
12

Note: The only reason to pass object as the second argument is that staticmethod insists on being passed a class of some kind as the second argument.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Javascript

js call static method from class

From Java

How to call a python method from a java class?

From

How to call non-static method from static method of same class?

From

How to call a static method from a private base class?

From

call a static method inside a class?

From Dev

calling static method from inside the class

From Dev

How to call static methods inside the same class in python

From Dev

Grails How to call service from static method of domain class?

From Dev

Call a static method from other class in python

From Dev

Python for .NET: How to call a method of a static class using Reflection?

From Dev

TypeError when testing static method call inside class method

From Dev

Call method from method inside same class

From Dev

Call a static method inside other static method of a class in Javascript

From Dev

How to call a method from a class inside the same module

From Dev

How to call a class method from inside same class method?

From Dev

How to call method in other class from a static class

From Dev

Python How to call a method from inside another method

From Dev

How to call non static method from main class

From Dev

Call a method from inside another class in Python

From Dev

Dynamically call static method on class from string

From Dev

How to call static class method from a struct?

From Dev

Can't call static method inside class

From Dev

Call to dialog from View class with static method

From Dev

How to call a method from a different class inside the main activity?

From Dev

How to call python api method inside another api in same class?

From Dev

How to use a Method from a nested class inside the Body of a Method from the first layer class

From Dev

Python - Unable to call static method inside another static method

From Dev

How to call class A method inside class B method in Python

From Dev

how to call a static method from another class in JS

Related Related

  1. 1

    js call static method from class

  2. 2

    How to call a python method from a java class?

  3. 3

    How to call non-static method from static method of same class?

  4. 4

    How to call a static method from a private base class?

  5. 5

    call a static method inside a class?

  6. 6

    calling static method from inside the class

  7. 7

    How to call static methods inside the same class in python

  8. 8

    Grails How to call service from static method of domain class?

  9. 9

    Call a static method from other class in python

  10. 10

    Python for .NET: How to call a method of a static class using Reflection?

  11. 11

    TypeError when testing static method call inside class method

  12. 12

    Call method from method inside same class

  13. 13

    Call a static method inside other static method of a class in Javascript

  14. 14

    How to call a method from a class inside the same module

  15. 15

    How to call a class method from inside same class method?

  16. 16

    How to call method in other class from a static class

  17. 17

    Python How to call a method from inside another method

  18. 18

    How to call non static method from main class

  19. 19

    Call a method from inside another class in Python

  20. 20

    Dynamically call static method on class from string

  21. 21

    How to call static class method from a struct?

  22. 22

    Can't call static method inside class

  23. 23

    Call to dialog from View class with static method

  24. 24

    How to call a method from a different class inside the main activity?

  25. 25

    How to call python api method inside another api in same class?

  26. 26

    How to use a Method from a nested class inside the Body of a Method from the first layer class

  27. 27

    Python - Unable to call static method inside another static method

  28. 28

    How to call class A method inside class B method in Python

  29. 29

    how to call a static method from another class in JS

HotTag

Archive