3

How do I statically (i.e. in the class body) call a staticmethod which calls another staticmethod in Python 3.6? I would like to use a staticmethod to initialize a static member of my class. The problem is that I'm trying to refer to my class before it has been created (unlike in this question).

Below is an example of my problem. I was actually trying to initialize a static dictionary using dictionary comprehension which makes use of a static function, so I've included an example of that too. I managed to solve that using a basic loop, but there are also other ways to use comprehensions statically.

class Test:

    @staticmethod
    def something(q):
        return q + 10

    @staticmethod
    def foo(x):
        return "Hello {0}".format(Test.something(x))

    # **** foo is called, but throws error at call to Test.something() ****
    #print(foo.__func__(4))    

    # something() is also used by itself.
    print(something.__func__(2))



    # My problem involved initializing a dictionary. Solved using loop.
    a = [1,2,3,4]

    #d = {k: "yup" for k in a}          # Dictionary comprehension works
    #d = {k: Test.something.__func__(k) for k in a}     # Error, Test/something not defined

    d = {}
    for k in a:
        d[k] = something.__func__(k)   




print(Test.foo(5))  # Works, class defined (but not what I need).

EDIT: This does not appear to be a duplicate of Calling class staticmethod within the class body?. This question is explicitly asking how to call one staticmethod from another in the class body, whereas the other question only asks how to call a staticmethod within the class body. (In any case, the best solution seems to be to do away with the staticmethod and use a module-level function instead)

Aralox
  • 1,441
  • 1
  • 24
  • 44
  • 2
    Python 2 or 3? But really, my advice would be just *don't use `staticmethod`* sounds like a use case for a `classmethod`, or better yet, just a couple of module-level functions – juanpa.arrivillaga Sep 07 '17 at 02:07
  • As for your `d` attribute, just add it directly to you class outside the definition... – juanpa.arrivillaga Sep 07 '17 at 02:10
  • Python 3, and i've just figured out a simple way to initialize my dictionary, edited. How would I use a `classmethod` for this? It still needs the class to be defined already I think. Thanks – Aralox Sep 07 '17 at 02:13
  • No, you use the first argument to the `classmethod` to refer to the *class*. That's the whole point. But again, you probably don't even want methods at all, just normal functions. – juanpa.arrivillaga Sep 07 '17 at 02:22
  • If you are in Python 3, you can use `__class__` inside your `staticmethod` decorated methods to statically call *another* static-method. But again, I urge you to simply use normal functions. – juanpa.arrivillaga Sep 07 '17 at 02:27
  • So for example I create a `classmethod` `bar` which takes `cls`, the class, and `x` as a parameter. How would I call this *statically*? Both `Test.bar.__func__(x)` and `bar.__func__(Test, x)` don't work. – Aralox Sep 07 '17 at 02:28
  • Uh, are you trying to call it *in the class body*? I was answering how "to statically call one staticmethod from another". But again, for like the millionth time, **just use normal functions**. – juanpa.arrivillaga Sep 07 '17 at 02:29
  • Yeah I am, I thought that was what calling something 'statically' meant - in the class body. I'll edit the question. What do you mean by 'normal functions'? I don't want to do this for instance variables, I specifically want to initialize a static member using static functions which call other static functions. Thanks EDIT: I just saw that you said 'module-level functions'. I understand now, cheers. I'd still like to know if there is an answer to this question. If possible, could you elaborate on how to use `__class__` to do this? I can't figure it out. – Aralox Sep 07 '17 at 02:31
  • `__class__` is simply to refer to the *class itself* inside a method in Python 3. It wasn't how you would do it from the *class body*, which is really a weird thing to do. – juanpa.arrivillaga Sep 07 '17 at 02:46

0 Answers0