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

Static class attributes

0 views
Skip to first unread message

Timothy Grant

unread,
Dec 8, 2002, 7:38:17 PM12/8/02
to
I've been working on a project done entirely in Perl over the last year
and have not kept up with all the happenings in the Python universe.

Is there now a good way to create class static attributes? (e.g., an
attribute that is shared across all classes of a given type?) In the
past I have used Module level variables to simulate this behaviour, but
It looks as if staticmethod, classmethod and property may change the way
I approach this problem, but I'm not yet sure.

Pointers would be appreciated. Thank you.

--
Stand Fast,
tjg.

Timothy Grant
www.craigelachie.org

signature.asc

Erik Max Francis

unread,
Dec 8, 2002, 10:32:42 PM12/8/02
to
Timothy Grant wrote:

> Is there now a good way to create class static attributes? (e.g., an
> attribute that is shared across all classes of a given type?) In the
> past I have used Module level variables to simulate this behaviour,
> but
> It looks as if staticmethod, classmethod and property may change the
> way
> I approach this problem, but I'm not yet sure.

If you want class static _attributes_, that's easy, and has been in
Python for a long, long time. Just do an assignment in class scope:

>>> class C:
... S = 'hello'
... def __init__(self, x):
... self.x = x
...
>>> C.S # the class attribute
'hello'
>>> c1 = C('one')
>>> c2 = C('two')
>>> c1.S # can access it through the instances, too
'hello'
>>> c2.S
'hello'
>>> c2.S = 'goodbye' # can even override it in one of the instances
>>> C.S
'hello'
>>> c1.S
'hello'

staticmethod is used when you don't want a first argument and want the
method callable either from instances or from the class itself:

>>> class D:
... def f(x, y): # note: no self argument
... print x, y
... f = staticmethod(f) # this is how you transform the method
...
>>> D.f(1, 2) # you can call it from the class
1 2
>>> d = D()
>>> d.f(3, 4) # or from an instance
3 4

classmethod is similar to staticmethod in that it allows the method to
be called either from an instance or the class itself, but it supports
an implicit first argument which is the _class_ involved, rather than
the instance:

>>> class E:
... def g(cls, x): # note first argument will be the class
... print repr(cls), x
... g = classmethod(g)
...
>>> E.g(1) # can call from the class
<class __main__.E at 0x815ce8c> 1
>>> e = E()
>>> e.g(2) # or the instance
<class __main__.E at 0x815ce8c> 2

--
Erik Max Francis / m...@alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/ \ The main thing in life is not to be afraid of being human.
\__/ Pablo Casals
Kepler's laws / http://www.alcyone.com/max/physics/kepler/
A proof of Kepler's laws.

Terry Hancock

unread,
Dec 9, 2002, 12:44:29 AM12/9/02
to
On Sunday 08 December 2002 05:24 pm, Timothy Grant wrote:
> Is there now a good way to create class static attributes? (e.g., an
> attribute that is shared across all classes of a given type?) In the

All "class"es have the same "type": "class". If we were using Python
terminology, so I guess you must not be. If you instead mean, all
"instances" of the same "class" then the answer is trivial -- you just use a
class attribute:

class my_class:
this_is_a_class_attrib = 'spam'
def __init__(self, ham):
self.this_is_an_instance_attrib = ham

I'm not sure what else you might mean.

Cheers,
Terry

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com

"Python takes the pain out of programming ...
... and Zope puts it back again."

Duncan Booth

unread,
Dec 9, 2002, 4:33:53 AM12/9/02
to
Terry Hancock <han...@anansispaceworks.com> wrote in
news:mailman.103941283...@python.org:

> All "class"es have the same "type": "class". If we were using Python
> terminology, so I guess you must not be.

Monday morning pedantry, not all classes have the same type:

>>> class C: pass

>>> class D(object): pass

>>> type(C)
<type 'class'>
>>> type(D)
<type 'type'>
>>> class meta(object): pass

>>> class E:
__metaclass__ = meta


>>> type(E)
<class '__main__.meta'>
>>>

--
Duncan Booth dun...@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?

0 new messages