classmethod & staticmethod

  • Thread starter Bruno Desthuilliers
  • Start date
B

Bruno Desthuilliers

Alex Popescu a écrit :
in

[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.
 
J

james_027

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
 
M

Marc 'BlackJack' Rintsch

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
 
B

Bruno Desthuilliers

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).
 
J

james_027

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
 
B

Bruno Desthuilliers

james_027 a écrit :
hi,


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

Alex Popescu

in
[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
 
N

Neil Cerutti

in
[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.
 
A

Alex Popescu

Bruno Desthuilliers <[email protected]>
wrote in
[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
 
S

Steven D'Aprano

Bruno Desthuilliers <[email protected]>
wrote in
[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.
 
N

Neil Cerutti

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

Alex Popescu

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
 
N

Neil Cerutti

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().
 
G

Gabriel Genellina

En Tue, 24 Jul 2007 21:55:17 -0300, Alex Popescu
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())
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'}
 
S

Steven D'Aprano

Bruno Desthuilliers



[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.
 
S

Steven D'Aprano

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

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"
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top