Accessing class variables in staticmethods.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ramashish Baranwal

    #1

    Accessing class variables in staticmethods.

    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.printna me() # 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

  • Sam

    #2
    Re: Accessing class variables in staticmethods.

    On 21 Jan 2007 12:49:17 -0800, Ramashish Baranwal
    <ramashish.list s@gmail.comwrot e:
    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.printna me() # 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)?
    No, staticmethods get told nothing about the class they're being
    defined in. What you want is a classmethod, which gets passed the
    class to work with.

    Using classmethods, your code becomes:

    #untested, bear in mind
    class Base:
    staticvar = 'Base'

    @classmethod
    def printname(cls):
    print cls.staticvar

    class Derived(Base):
    staticvar = 'Derived'

    Base.printname( ) #prints 'Base'
    Derived.printna me() #prints 'Derived'

    Incidentally, you can also use cls.__name__ for this purpose, but I
    guess that your actual motivation for this is more complicated than
    class names.

    Comment

    • Ramashish Baranwal

      #3
      Re: Accessing class variables in staticmethods.

      Sam wrote:
      On 21 Jan 2007 12:49:17 -0800, Ramashish Baranwal
      <ramashish.list s@gmail.comwrot e:
      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.printna me() # 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)?
      >
      No, staticmethods get told nothing about the class they're being
      defined in. What you want is a classmethod, which gets passed the
      class to work with.
      >
      Using classmethods, your code becomes:
      >
      #untested, bear in mind
      class Base:
      staticvar = 'Base'
      >
      @classmethod
      def printname(cls):
      print cls.staticvar
      >
      class Derived(Base):
      staticvar = 'Derived'
      >
      Base.printname( ) #prints 'Base'
      Derived.printna me() #prints 'Derived'
      >
      Incidentally, you can also use cls.__name__ for this purpose, but I
      guess that your actual motivation for this is more complicated than
      class names.
      Thanks Sam, using classmethod works. You guessed it correctly, my
      actual motivation is more complicated but on the same line.

      -Ram

      Comment

      Working...