classmethod & staticmethod

Discussion in 'Python' started by Bruno Desthuilliers, Jul 21, 2007.

  1. Alex Popescu a écrit :
    > Bruno Desthuilliers <> wrote
    > in news:46a5b2ad$0$18903$:
    >
    >
    >
    >>[snip...]
    >>
    >>
    >>Not necessarily - you can access class attributes from within an
    >>instance method (but obviously a classmethod cannot access instance
    >>attributes).
    >>

    >
    > What I am doing wrong here then:
    >
    > class MyClass(object):
    > class_list = ['a', 'b']
    >
    > def instance_method(self):
    > print "instance_method with class list %s" % class_list
    >
    > o = MyClass()
    > o.instance_method()
    >
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > File "<stdin>", line 4, in instance_method
    > NameError: global name 'class_list' is not defined


    The answer is in the error message. There's no local definition of
    'class_list' in function 'instance_method', and no global definition of
    'class_list' in the module. If you want to access MyClass.class_list in
    function instance_method, you need to use a fully qualified name, ie
    either self.class_list[1], self.__class__.class_list[2],
    type(self).class_list[3] or MyClass.class_list[4]

    [1] this one is ok in 'read mode' since attributes not found in the
    instance's __dict__ will be looked up in the class's __dict__ (and then
    in parent's classes __dict__), but be warned that something like
    'self.class_list = []' will create an instance attribute of the same
    name, shadowing the class one. This is one of the (in)famous Python gotchas.

    [2] && [3] Those two ones are equivalent. [3] is cleaner since it avoids
    direct access to an implementation attribute, but [2] is faster since it
    avoids a function call. In both cases, the lookup will be what one would
    expect in OO, that is first on the concrete class, then in the parents
    classes.

    [4] Only use this one if you really want to bypass inheritance.
     
    Bruno Desthuilliers, Jul 21, 2007
    #1
    1. Advertising

  2. Bruno Desthuilliers

    james_027 Guest

    hi,

    python's staticmethod is the equivalent of java staticmethod right?

    with classmethod, I can call the method without the need for creating
    an instance right? since the difference between the two is that
    classmethod receives the class itself as implicti first argument. From
    my understanding classmethod are for dealing with class attributes?

    Can somebody teach me the real use of classmethod & staticmethod?

    Thanks
    james
     
    james_027, Jul 24, 2007
    #2
    1. Advertising

  3. On Tue, 24 Jul 2007 03:19:05 +0000, james_027 wrote:

    > python's staticmethod is the equivalent of java staticmethod right?


    Correct. `staticmethod` is essentially just a function moved into a class
    and accessible at the class object and instances of that class.

    As Python opposed to Java has functions, `staticmethod` isn't that useful.

    > with classmethod, I can call the method without the need for creating
    > an instance right? since the difference between the two is that
    > classmethod receives the class itself as implicti first argument. From
    > my understanding classmethod are for dealing with class attributes?


    It's possible to access class attributes and, maybe more important, it's
    possible to call the class to return an instance. So if you call a
    `classmethod` on a subclass an instance of that subclass is returned.
    Silly example:

    class A(object):
    def __init__(self, x, y):
    print 'init A'
    self.x = x
    self.y = y

    @classmethod
    def from_str(cls, string):
    return cls(*map(float, string.split(',')))


    class B(A):
    def __init__(self, x, y):
    print 'init B'
    A.__init__(self, x, y)


    def main():
    B.from_str('42,23')

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, Jul 24, 2007
    #3
  4. james_027 a écrit :
    > hi,
    >
    > python's staticmethod is the equivalent of java staticmethod right?


    IIRC, yes. A 'staticmethod' is in fact nothing more than a function
    attached to a class, and which can be called on the class or an instance
    of. Note that since Python supports modules and functions, staticmethods
    are of little practical use.

    > with classmethod, I can call the method without the need for creating
    > an instance right? since the difference between the two is that
    > classmethod receives the class itself as implicti first argument.


    Yes.

    > From
    > my understanding classmethod are for dealing with class attributes?


    Not necessarily - you can access class attributes from within an
    instance method (but obviously a classmethod cannot access instance
    attributes).

    > Can somebody teach me the real use of classmethod & staticmethod?


    The 'real' use is (are) the one(s) you'll find. FWIW, I use
    staticmethods for helper functions that don't need access to the class
    or instance but are too specific to a class to be of any use as plain
    functions. Which is not a very frequent case. Classmethods are more
    usefull - mostly as alternate constructors or utility methods for an
    alternate constructor, but there are other possible uses (sorry, I have
    no concrete example at hand).
     
    Bruno Desthuilliers, Jul 24, 2007
    #4
  5. Bruno Desthuilliers

    james_027 Guest

    hi,

    > The 'real' use is (are) the one(s) you'll find. FWIW, I use
    > staticmethods for helper functions that don't need access to the class
    > or instance but are too specific to a class to be of any use as plain
    > functions. Which is not a very frequent case. Classmethods are more
    > usefull - mostly as alternate constructors or utility methods for an
    > alternate constructor, but there are other possible uses (sorry, I have
    > no concrete example at hand).


    You mean like the example from Marc

    Thanks
    james
     
    james_027, Jul 24, 2007
    #5
  6. james_027 a écrit :
    > hi,
    >
    >> The 'real' use is (are) the one(s) you'll find. FWIW, I use
    >> staticmethods for helper functions that don't need access to the class
    >> or instance but are too specific to a class to be of any use as plain
    >> functions. Which is not a very frequent case. Classmethods are more
    >> usefull - mostly as alternate constructors or utility methods for an
    >> alternate constructor, but there are other possible uses (sorry, I have
    >> no concrete example at hand).

    >
    > You mean like the example from Marc


    Marc's example is typically an alternate constructor. This is indeed one
    of the most obvious use case of classmethod, but what I meant is that
    there are others cases where classmethods can help.
     
    Bruno Desthuilliers, Jul 24, 2007
    #6
  7. Bruno Desthuilliers

    Alex Popescu Guest

    Bruno Desthuilliers <> wrote
    in news:46a5b2ad$0$18903$:


    >
    > [snip...]
    >
    >
    > Not necessarily - you can access class attributes from within an
    > instance method (but obviously a classmethod cannot access instance
    > attributes).
    >


    What I am doing wrong here then:

    class MyClass(object):
    class_list = ['a', 'b']

    def instance_method(self):
    print "instance_method with class list %s" % class_list

    o = MyClass()
    o.instance_method()

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "<stdin>", line 4, in instance_method
    NameError: global name 'class_list' is not defined

    bests,
    ../alex
    --
    ..w( the_mindstorm )p.
     
    Alex Popescu, Jul 24, 2007
    #7
  8. Bruno Desthuilliers

    Neil Cerutti Guest

    On 2007-07-24, Alex Popescu <> wrote:
    > Bruno Desthuilliers <> wrote
    > in news:46a5b2ad$0$18903$:
    >
    >
    >>
    >> [snip...]
    >>
    >>
    >> Not necessarily - you can access class attributes from within an
    >> instance method (but obviously a classmethod cannot access instance
    >> attributes).
    >>

    >
    > What I am doing wrong here then:
    >
    > class MyClass(object):
    > class_list = ['a', 'b']
    >
    > def instance_method(self):
    > print "instance_method with class list %s" % class_list


    There's no implicit self or class for Python identifiers.

    The name class_list must be quailified: self.class_list or
    MyClass.class_list.

    --
    Neil Cerutti
     
    Neil Cerutti, Jul 24, 2007
    #8
  9. Bruno Desthuilliers

    Alex Popescu Guest

    Neil Cerutti <> wrote in
    news::

    > On 2007-07-24, Alex Popescu <> wrote:
    >> Bruno Desthuilliers <>
    >> wrote in news:46a5b2ad$0$18903$:
    >>

    >
    > [snip...]
    >
    >>
    >> class MyClass(object):
    >> class_list = ['a', 'b']
    >>
    >> def instance_method(self):
    >> print "instance_method with class list %s" % class_list

    >
    > There's no implicit self or class for Python identifiers.
    >
    > The name class_list must be quailified: self.class_list or
    > MyClass.class_list.
    >


    After more investigation I have figured this out by myself, but thanks for
    the details.
    Now I am wondering if in the above case there is a prefered way:
    MyClass.class_list or self.__class__.class_list? (IMO the 2nd is more safe
    in terms of refactorings).

    ../alex
    --
    ..w( the_mindstorm )p.
     
    Alex Popescu, Jul 24, 2007
    #9
  10. On Tue, 24 Jul 2007 21:35:58 +0000, Alex Popescu wrote:

    > Neil Cerutti <> wrote in
    > news::
    >
    >> On 2007-07-24, Alex Popescu <> wrote:
    >>> Bruno Desthuilliers <>
    >>> wrote in news:46a5b2ad$0$18903$:
    >>>

    >>
    >> [snip...]
    >>
    >>>
    >>> class MyClass(object):
    >>> class_list = ['a', 'b']
    >>>
    >>> def instance_method(self):
    >>> print "instance_method with class list %s" % class_list

    >>
    >> There's no implicit self or class for Python identifiers.
    >>
    >> The name class_list must be quailified: self.class_list or
    >> MyClass.class_list.
    >>

    >
    > After more investigation I have figured this out by myself, but thanks for
    > the details.
    > Now I am wondering if in the above case there is a prefered way:
    > MyClass.class_list or self.__class__.class_list? (IMO the 2nd is more safe
    > in terms of refactorings).


    Consider what happens when you sub-class:

    class MyClass(object):
    class_list = [1, 2, 3]
    def method(self, x):
    return sum(MyClass.class_list) + x

    class MySubclass(MyClass):
    class_list = ['a', 'b', 'c'] # over-ride the class attribute

    expecting_a_string = MySubclass().method('x')


    Use a direct reference to MyClass.class_list when you want a direct
    reference to MyClass regardless of which instance or class you are calling
    from.

    Use self.class_list when you want to use inheritance.

    Use self.__class__.class_list when you have an instance method and you
    need the class it belongs to.

    Use a classmethod and the first argument (by convention called klass
    or cls) when you don't care about the instance and just want the class.



    --
    Steven.
     
    Steven D'Aprano, Jul 25, 2007
    #10
  11. Bruno Desthuilliers

    Alex Popescu Guest

    "Steven D'Aprano" <> wrote in
    news:p:

    > On Tue, 24 Jul 2007 21:35:58 +0000, Alex Popescu wrote:
    >
    >> Neil Cerutti <> wrote in
    >> news::
    >>
    >>> On 2007-07-24, Alex Popescu <> wrote:
    >>>> Bruno Desthuilliers
    >>>> <> wrote in
    >>>> news:46a5b2ad$0$18903$:
    >>>>
    >>>
    >>> [snip...]
    >>>

    >
    > Use self.class_list when you want to use inheritance.
    >


    As a matter of style, how do you figure out that class_list is a class
    attribute and not an instance attribute? (I don't remember seeing anything
    in the PEP describing the coding style).

    tia,
    ../alex
    --
    ..w( the_mindstorm )p.
     
    Alex Popescu, Jul 25, 2007
    #11
  12. Bruno Desthuilliers

    Neil Cerutti Guest

    On 2007-07-25, Alex Popescu <> wrote:
    > As a matter of style, how do you figure out that class_list is
    > a class attribute and not an instance attribute? (I don't
    > remember seeing anything in the PEP describing the coding
    > style).


    Check out dir(MyClass) and dir(MyClass()) for some insight, if it
    turns out that it matters. Preferably, the user of a class
    doesn't have to really think about it much.

    --
    Neil Cerutti
     
    Neil Cerutti, Jul 25, 2007
    #12
  13. Bruno Desthuilliers

    Alex Popescu Guest

    Neil Cerutti <> wrote in news:eRwpi.36813$G23.28496
    @newsreading01.news.tds.net:

    > On 2007-07-25, Alex Popescu <> wrote:
    >> As a matter of style, how do you figure out that class_list is
    >> a class attribute and not an instance attribute? (I don't
    >> remember seeing anything in the PEP describing the coding
    >> style).

    >
    > Check out dir(MyClass) and dir(MyClass()) for some insight, if it
    > turns out that it matters.


    I must confess that I am a bit confused by this advise, as both are
    returning exactly the same thing.

    > Preferably, the user of a class
    > doesn't have to really think about it much.
    >

    I know that this would be prefered, but in case you are getting 3rd party
    code and you modify a class attribute without knowing it is a class
    attribute then you may get into trouble (indeed the real problem is with
    the designer of the 3rd party code, but still I think it is a valid
    concern).

    tia,
    ../alex
    --
    ..w( the_mindstorm )p.
     
    Alex Popescu, Jul 25, 2007
    #13
  14. Bruno Desthuilliers

    Neil Cerutti Guest

    On 2007-07-25, Alex Popescu <> wrote:
    > Neil Cerutti <> wrote in news:eRwpi.36813$G23.28496
    > @newsreading01.news.tds.net:
    >
    >> On 2007-07-25, Alex Popescu <> wrote:
    >>> As a matter of style, how do you figure out that class_list is
    >>> a class attribute and not an instance attribute? (I don't
    >>> remember seeing anything in the PEP describing the coding
    >>> style).

    >>
    >> Check out dir(MyClass) and dir(MyClass()) for some insight, if it
    >> turns out that it matters.

    >
    > I must confess that I am a bit confused by this advise, as both
    > are returning exactly the same thing.


    Oops! I misthought myself.

    I was thinking of MyClass.__dict__.keys() and
    MyClass().__dict__.keys().

    --
    Neil Cerutti
    To succeed in the world it is not enough to be stupid, you must also be well-
    mannered. --Voltaire
     
    Neil Cerutti, Jul 25, 2007
    #14
  15. En Tue, 24 Jul 2007 21:55:17 -0300, Alex Popescu
    <> escribió:

    > Neil Cerutti <> wrote in news:eRwpi.36813$G23.28496
    > @newsreading01.news.tds.net:
    >> On 2007-07-25, Alex Popescu <> wrote:
    >>> As a matter of style, how do you figure out that class_list is
    >>> a class attribute and not an instance attribute? (I don't
    >>> remember seeing anything in the PEP describing the coding
    >>> style).

    >>
    >> Check out dir(MyClass) and dir(MyClass()) for some insight, if it
    >> turns out that it matters.

    >
    > I must confess that I am a bit confused by this advise, as both are
    > returning exactly the same thing.


    Perhaps he meant to say vars(MyClass) and vars(MyClass())

    >> Preferably, the user of a class
    >> doesn't have to really think about it much.

    > I know that this would be prefered, but in case you are getting 3rd party
    > code and you modify a class attribute without knowing it is a class
    > attribute then you may get into trouble (indeed the real problem is with
    > the designer of the 3rd party code, but still I think it is a valid
    > concern).


    Well, you should read its documentation before modifying 3rd. party
    code... :)

    If you access (read) an attribute through an instance (let's say, you
    write x = self.name inside a method) the attribute is first searched in
    the instance, and when not found, in the class (The actual rules are more
    complicated but this will suffice for now). If you assign an attribute, it
    is always set on the instance (never on the class). So you can use a class
    attribute as a default value for an instance attribute. For this use case,
    you don't care whether it's an instance attribute or class attribute:

    py> class Title:
    .... color = "white"
    .... def __init__(self, text, color=None):
    .... self.text = text
    .... if color is not None:
    .... self.color = color
    ....
    py> t1 = Title("Hello")
    py> vars(t1)
    {'text': 'Hello'}
    py> t1.color
    'white'
    py> Title.color
    'white'
    py> t1.color = "red"
    py> vars(t1)
    {'color': 'red', 'text': 'Hello'}
    py> Title.color
    'white'
    py> t2 = Title("Goodbye", "blue")
    py> vars(t2)
    {'color': 'blue', 'text': 'Goodbye'}


    --
    Gabriel Genellina
     
    Gabriel Genellina, Jul 25, 2007
    #15
  16. On Wed, 25 Jul 2007 00:22:18 +0000, Alex Popescu wrote:

    > "Steven D'Aprano" <> wrote in
    > news:p:
    >
    >> On Tue, 24 Jul 2007 21:35:58 +0000, Alex Popescu wrote:
    >>
    >>> Neil Cerutti <> wrote in
    >>> news::
    >>>
    >>>> On 2007-07-24, Alex Popescu <> wrote:
    >>>>> Bruno Desthuilliers
    >>>>> <> wrote in
    >>>>> news:46a5b2ad$0$18903$:
    >>>>>
    >>>>
    >>>> [snip...]
    >>>>

    >>
    >> Use self.class_list when you want to use inheritance.
    >>

    >
    > As a matter of style, how do you figure out that class_list is a class
    > attribute and not an instance attribute? (I don't remember seeing anything
    > in the PEP describing the coding style).



    The whole point of inheritance is that you don't care where the attribute
    is (the instance, the class, a parent class...) just that it exists.



    --
    Steven.
     
    Steven D'Aprano, Jul 26, 2007
    #16
  17. On Wed, 25 Jul 2007 00:55:17 +0000, Alex Popescu wrote:

    > Neil Cerutti <> wrote in news:eRwpi.36813$G23.28496
    > @newsreading01.news.tds.net:
    >
    >> On 2007-07-25, Alex Popescu <> wrote:
    >>> As a matter of style, how do you figure out that class_list is
    >>> a class attribute and not an instance attribute? (I don't
    >>> remember seeing anything in the PEP describing the coding
    >>> style).

    >>
    >> Check out dir(MyClass) and dir(MyClass()) for some insight, if it
    >> turns out that it matters.

    >
    > I must confess that I am a bit confused by this advise, as both are
    > returning exactly the same thing.
    >
    >> Preferably, the user of a class
    >> doesn't have to really think about it much.
    >>

    > I know that this would be prefered, but in case you are getting 3rd party
    > code and you modify a class attribute without knowing it is a class
    > attribute then you may get into trouble (indeed the real problem is with
    > the designer of the 3rd party code, but still I think it is a valid
    > concern).


    # warning: doesn't consider slots
    if "attribute" in instance.__dict__:
    print "instance attribute"
    elif "attribute" in instance.__class__.__dict__:
    print "class attribute"
    else:
    print "either no attribute at all, or in a parent class"



    --
    Steven.
     
    Steven D'Aprano, Jul 26, 2007
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal Vitecek
    Replies:
    4
    Views:
    998
    Michael Hudson
    Feb 11, 2004
  2. Andrew Bennetts

    Re: __getitem__ and classmethod/staticmethod

    Andrew Bennetts, Jun 25, 2004, in forum: Python
    Replies:
    0
    Views:
    497
    Andrew Bennetts
    Jun 25, 2004
  3. Karl Chen
    Replies:
    0
    Views:
    353
    Karl Chen
    Jun 25, 2004
  4. Robin Becker

    staticmethod vs metaclass

    Robin Becker, Aug 11, 2004, in forum: Python
    Replies:
    0
    Views:
    469
    Robin Becker
    Aug 11, 2004
  5. C Gillespie

    staticmethod and classmethod

    C Gillespie, May 24, 2005, in forum: Python
    Replies:
    5
    Views:
    5,786
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    May 26, 2005
Loading...

Share This Page