how to define a static field of a given class

F

feel_energetic

Hi,

I already knew how to define a static method of a class( using
staticmethod() ),but I find there isn't a built-in func to build a
static field ( something like staticfield() )
can anyone help me on this?
thanks very much for your help :)
 
M

Maric Michaud

Le Vendredi 02 Juin 2006 11:07, feel_energetic a écrit :
Hi,

I already knew how to define a static method of a class( using
staticmethod() ),but I find there isn't a built-in func to build a
static field ( something like staticfield() )
can anyone help me on this?
thanks very much for your help :)
I guess you come from a c++ background, and what you mean by static field is a
variable shared by all instance of the class, right ?

then,

class toto :
VAL=5

but, you can't assign directly via instances of the class as it will override
VAL in the instance :

In [2]: t=toto()

In [3]: t.VAL=4

In [4]: toto.VAL
Out[4]: 5

In [5]: t.__dict__
Out[5]: {'VAL': 4}

You must explicitly modify t.__class__.VAL or toto.VAL :

In [8]: t1, t2 = toto(), toto()

In [9]: t1.__class__.VAL = 4

In [10]: t2.VAL
Out[10]: 4


--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
 
R

Rob Williscroft

feel_energetic wrote in @g10g2000cwb.googlegroups.com in comp.lang.python:
Hi,

I already knew how to define a static method of a class( using
staticmethod() ),but I find there isn't a built-in func to build a
static field ( something like staticfield() )
can anyone help me on this?
thanks very much for your help :)

What you possibly want is just a class attribute:

class MyClass( object ):
static_field = 10

myInstance = MyClass()

print MyClass.static_field, myInstance.static_field

Rob.
 
B

bruno at modulix

feel_energetic said:
Hi,

I already knew how to define a static method of a class( using
staticmethod() ),

FWIW, it's probably one of the most useless construct in Python IMHO.
classmethod are really much more useful to me.
but I find there isn't a built-in func to build a
static field ( something like staticfield() )

Please define "static field", I just don't understand what it could be.

Now if what you want is a class attribute (ie: an attribute that is
shared by all instances of a class), just declare it at the class level:

class MyClass(object):
shared_attrib = 42

# can be accessed via the class
# or via an instance
MyClass.shared_attrib
m = MyClass()
m.shared_attrib

# but you don't want to rebind it via an instance
m.shared_attrib = 33
m.shared_attrib
MyClass.shared_attrib

# note that the problem is only with rebinding - mutating is ok
class MyOtherClass(object):
shared_attrib = [42]
mo = MyOtherClass()
mo.shared_attrib

mo.shared_attrib.append(33)
mo.shared_attrib
MyOtherClass.shared_attrib

HTH
 
M

Maric Michaud

Le Vendredi 02 Juin 2006 11:47, bruno at modulix a écrit :
FWIW, it's probably one of the most useless construct in Python IMHO.
classmethod are really much more useful to me.
+1

I do prefer classmethod, both for the name and behavior (everything should be
intended for polymorphism, after all, classes are just instances of type).

--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
 
F

feel_energetic

thanks for all of your replies

I did this before I posted the subject but got (NameError: global name
'startTime' is not defined)

the most important thing i didn't know is
"the static field should be referred with the qualifier ClassName"

it's a little different from C++ :) (the static field can directly
referred without the ClassName)
 

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

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top