Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

classmethod & staticmethod

10 views
Skip to first unread message

james_027

unread,
Jul 23, 2007, 11:19:05 PM7/23/07
to
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

Marc 'BlackJack' Rintsch

unread,
Jul 24, 2007, 3:58:21 AM7/24/07
to
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

Bruno Desthuilliers

unread,
Jul 24, 2007, 4:05:06 AM7/24/07
to
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).

james_027

unread,
Jul 24, 2007, 6:44:50 AM7/24/07
to
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

Bruno Desthuilliers

unread,
Jul 24, 2007, 6:57:54 AM7/24/07
to
james_027 a écrit :

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.

Message has been deleted

Neil Cerutti

unread,
Jul 24, 2007, 1:04:27 PM7/24/07
to
On 2007-07-24, Alex Popescu <nospam.th...@gmail.com> wrote:
> Bruno Desthuilliers <bruno.42.de...@wtf.websiteburo.oops.com> wrote
> in news:46a5b2ad$0$18903$426a...@news.free.fr:
>
>
>>
>> [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

Alex Popescu

unread,
Jul 24, 2007, 5:35:58 PM7/24/07
to pytho...@python.org
Neil Cerutti <hor...@yahoo.com> wrote in
news:slrnfaccgl....@FIAD06.norwich.edu:

> On 2007-07-24, Alex Popescu <nospam.th...@gmail.com> wrote:
>> Bruno Desthuilliers <bruno.42.de...@wtf.websiteburo.oops.com>
>> wrote in news:46a5b2ad$0$18903$426a...@news.free.fr:
>>
>
> [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.

Steven D'Aprano

unread,
Jul 24, 2007, 7:21:22 PM7/24/07
to

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.

Alex Popescu

unread,
Jul 24, 2007, 8:22:18 PM7/24/07
to pytho...@python.org
"Steven D'Aprano" <st...@REMOVE.THIS.cybersource.com.au> wrote in
news:pan.2007.07.24....@REMOVE.THIS.cybersource.com.au:

> On Tue, 24 Jul 2007 21:35:58 +0000, Alex Popescu wrote:
>
>> Neil Cerutti <hor...@yahoo.com> wrote in
>> news:slrnfaccgl....@FIAD06.norwich.edu:
>>
>>> On 2007-07-24, Alex Popescu <nospam.th...@gmail.com> wrote:
>>>> Bruno Desthuilliers
>>>> <bruno.42.de...@wtf.websiteburo.oops.com> wrote in
>>>> news:46a5b2ad$0$18903$426a...@news.free.fr:
>>>>
>>>
>>> [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,

Neil Cerutti

unread,
Jul 24, 2007, 8:31:06 PM7/24/07
to
On 2007-07-25, Alex Popescu <nospam.th...@gmail.com> 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

Alex Popescu

unread,
Jul 24, 2007, 8:55:17 PM7/24/07
to pytho...@python.org
Neil Cerutti <hor...@yahoo.com> wrote in news:eRwpi.36813$G23.28496
@newsreading01.news.tds.net:

> On 2007-07-25, Alex Popescu <nospam.th...@gmail.com> 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).

Bruno Desthuilliers

unread,
Jul 21, 2007, 7:47:23 AM7/21/07
to
Alex Popescu a écrit :

> Bruno Desthuilliers <bruno.42.de...@wtf.websiteburo.oops.com> wrote
> in news:46a5b2ad$0$18903$426a...@news.free.fr:
>
>
>
>>[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.

Neil Cerutti

unread,
Jul 25, 2007, 7:26:32 AM7/25/07
to
On 2007-07-25, Alex Popescu <nospam.th...@gmail.com> wrote:
> Neil Cerutti <hor...@yahoo.com> wrote in news:eRwpi.36813$G23.28496
> @newsreading01.news.tds.net:
>
>> On 2007-07-25, Alex Popescu <nospam.th...@gmail.com> 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

Gabriel Genellina

unread,
Jul 25, 2007, 8:18:44 AM7/25/07
to pytho...@python.org
En Tue, 24 Jul 2007 21:55:17 -0300, Alex Popescu
<nospam.th...@gmail.com> escribió:

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


>> On 2007-07-25, Alex Popescu <nospam.th...@gmail.com> 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

Steven D'Aprano

unread,
Jul 25, 2007, 9:06:37 PM7/25/07
to


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

unread,
Jul 25, 2007, 9:12:01 PM7/25/07
to

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

0 new messages