Access static method from static variable
Question: There are plenty answers for how to access static variables from static
methods (like [this one](http://stackoverflow.com/questions/11233729/access-
static-variable-from-static-method), and [that
one](http://stackoverflow.com/questions/707380/in-python-how-can-i-access-
static-class-variables-within-class-methods), and the great info on the
subject [here](http://stackoverflow.com/questions/68645/static-class-
variables-in-python)), however I'm having trouble with the other direction:
How can you use static methods to initialize static variables. E.g.:

    
    
    import os, platform
    class A(object):
        @staticmethod
        def getRoot():
            return 'C:\\' if platform.system() == 'Windows' else '/'
        pth = os.path.join(A.getRoot(), 'some', 'path')
    

The last line gives an exception: `NameError: name 'A' is not defined`. The
same error happens if I use `@classmethod` instead of `@staticmethod`.

Is it possible to access a static method from a class variable?


Answer: The problem is that the class "A" doesn't exist yet at the moment your
vatiable pth is declared, so the evaluation fails. How about declaring it
right after?

    
    
    import os, platform
    class A(object):
        @staticmethod
        def getRoot():
            return 'C:\\' if platform.system() == 'Windows' else '/'
    A.pth = os.path.join(A.getRoot(), 'some', 'path')
    

An uglier alternative would be:

    
    
    import os, platform
    class A(object):
        @staticmethod
        def getRoot():
            return 'C:\\' if platform.system() == 'Windows' else '/'
        pth = os.path.join(getRoot.__func__(), 'some', 'path')
    

... but it's pretty unreadable (and depends on implementation details of
@staticmethod, which is bad).

For this specific case I'd do something like this (which doesn't really answer
your question, instead it sidesteps the need for it):

    
    
    import os, platform
    class A(object):
        _root = 'C:\\' if platform.system() == 'Windows' else '/'
        @staticmethod
        def getRoot():
            return A._root
        pth = os.path.join(_root, 'some', 'path')
    

... because your platform is pretty unlikely to change while your program is
still running, right? :) If you have a better reason to do something like
that, maybe use one of the methods above.

