473,403 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

how to define a static field of a given class

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 :)

Jun 2 '06 #1
5 2640
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
Jun 2 '06 #2
feel_energetic wrote in news:1149239221.045268.6170
@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.
--
http://www.victim-prime.dsl.pipex.com/
Jun 2 '06 #3
feel_energetic wrote:
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
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jun 2 '06 #4
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
Jun 2 '06 #5

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)

Jun 2 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

97
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
12
by: Sergey Klementiev | last post by:
Why it's impossible to have a static indexer in C#?
25
by: Sahil Malik [MVP] | last post by:
So here's a rather simple question. Say in an ASP.NET application, I wish to share common constants as static variables in global.asax (I know there's web.config bla bla .. but lets just say I...
6
by: RSH | last post by:
I'm having a bit of trouble understanding Static properties. I have two forms and I need to be able to access a property from from1 in form2. The code below does not work but I'm not sure why...
3
by: RSH | last post by:
If I have a static method that references the HttpContext.Current object is it safe to be in a static method within a class under ASP .Net? I know that static classes are shared by all sessions...
5
by: alan | last post by:
Hello world, I'm wondering if it's possible to implement some sort of class/object that can perform mapping from class types to strings? I will know the class type at compile time, like so:...
3
by: tomPee | last post by:
Hi, I have the following problem: I am trying to make some sort of base class menu that i can then use to derive other menu's from. Those menu's should then be able to interact with each other....
4
by: Jon Skeet [C# MVP] | last post by:
On Aug 11, 5:11 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com> wrote: Exactly. My favourite example is Thread.Sleep. Suppose this code were valid (its equivalent in Java is, for example): ...
5
by: Timothy Madden | last post by:
Hy static members of non-integral type need to be declared in the class, but defined (and constructed or initialized) outside the class. Like this class SystemName { public:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.