There are plenty answers for how to access static variables from static methods (like this one, and that one, and the great info on the subject here), 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?
Static methods have access to class variables (static variables) without using the class's object (instance). Only static data may be accessed by a static method. It is unable to access data that is not static (instance variables). In both static and non-static methods, static methods can be accessed directly.
Static Methods in JavaThe static methods of a particular class can only access the static variables and can change them. A static method can only call other static methods. Static methods can't refer to non-static variables or methods. Static methods can't refer to “super” or “this” members.
An instance variable, as the name suggests is tied to an instance of a class. Therefore, accessing it directly from a static method, which is not tied to any specific instance doesn't make sense. Therefore, to access an instance variable, we must have an instance of the class from which we access the instance variable.
Common Use for Static Methods They are declared with the keyword "static" when defining a method. Static methods can be accessed without having to create a new object. A static method can only use and call other static methods or static data members.
A static variable can be accessed directly by the class name and doesn’t need any object Syntax : What is Static Method in Java? Static method in Java is a method which belongs to the class and not to the object.
Therefore, accessing it directly from a static method, which is not tied to any specific instance doesn't make sense. Therefore, to access an instance variable, we must have an instance of the class from which we access the instance variable.
A static method can access only static data. It is a method which belongs to the class and not to the object (instance). A static method can access only static data. It cannot access non-static data (instance variables).
A static method can be accessed from a method in the same class using the self keyword and double colon (::): echo "Hello World!"; Static methods can also be called from methods in other classes. To do this, the static method should be public:
The problem is that the class "A" doesn't exist yet at the moment your variable path 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.
You can defer the evaluation of pth
by using a classproperty
:
class A(object):
@staticmethod
def getRoot():
return 'C:\\' if platform.system() == 'Windows' else '/'
@classproperty
def pth(cls):
return os.path.join(cls.getRoot(), 'some', 'path')
classproperty
is not a builtin, but it is a widely used recipe.
As a side note, IMHO, you should always prefer using classmethod
over staticmethod
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With