python - Access static method from static variable -


there plenty answers how access static variables static methods (like this one, , that one, , great info on subject here), i'm having trouble other direction: how can use static methods 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 exception: nameerror: name 'a' not defined. same error happens if use @classmethod instead of @staticmethod.

is possible access static method class variable?

the problem class "a" doesn't exist yet @ moment vatiable pth declared, evaluation fails. how declaring 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 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') 

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

for specific case i'd (which doesn't answer question, instead sidesteps need 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 platform pretty unlikely change while program still running, right? :) if have better reason that, maybe use 1 of methods above.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -