Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access static method from static variable

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?

like image 570
asherbret Avatar asked May 11 '15 15:05

asherbret


People also ask

How can we access static method in static method?

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.

Can static method access static variable in Java?

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.

How do you access a static variable?

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.

Can static methods be accessed?

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.

How do I access a static variable in Java?

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.

How to access an instance variable from a static method?

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.

Which method can access only static data?

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).

How do I access a static method from another class?

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:


2 Answers

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.

like image 148
Emile Avatar answered Sep 23 '22 05:09

Emile


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.

like image 28
shx2 Avatar answered Sep 20 '22 05:09

shx2