Accessing class variables in staticmethods.

Ramashish Baranwal ramashish.lists at gmail.com
Sun Jan 21 15:49:17 EST 2007


Hi,

I want to access a static variable in a staticmethod. The variable can
be redefined by derived classes and that should be reflected in base's
staticmethod. Consider this trivial example-

class Base:
    staticvar = 'Base'

    @staticmethod
    def printname():
        # this doesn't work
        # print staticvar
        # this does work but derived classes wouldn't behave as I want
        print Base.staticvar

class Derived(Base):
    staticvar = 'Derived'

Base.printname() # should print 'Base'
Derived.printname() # should print 'Derived'

Any idea on how to go about this? Also from a staticmethod how can I
find out other attributes of the class (not objects)? Do static methods
get some classinfo via some implicit argument(s)?

Thanks in advance,
Ram




More information about the Python-list mailing list