473,670 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2646
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__.VA L = 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.go oglegroups.com in comp.lang.pytho n:
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.stat ic_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(ob ject):
shared_attrib = [42]
mo = MyOtherClass()
mo.shared_attri b

mo.shared_attri b.append(33)
mo.shared_attri b
MyOtherClass.sh ared_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
27773
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
12
24786
by: Sergey Klementiev | last post by:
Why it's impossible to have a static indexer in C#?
25
5155
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 wanna use global.asax) --- Would you declare your static var as --- public static int x ;
6
2039
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 not. And also why is it necessary to declare the variable bIshShown twice? Form1 code:
3
3398
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 at a given time, so Im not sure if this can be a static class. public static SessionManager Instance {
5
2570
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: const char *s = string_mapper<thetype>(); However I do not know the string to be associated with the type at compile time, and will need a way to set up the mapping, to be created at run time, possibly like so: void foo(char*...
3
2044
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. And, i have most of the idea figured out and I thought out how i want to do it. But when i started coding i found a slight... difficulty. It might be easy to overcome, but google let me down :( and my own imagination made one happy jump, but...
4
1909
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): ThreadStart ts = SomeMethodToRun; Thread thread = new Thread(ts); thread.Start(); thread.Sleep(500);
5
7226
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
8384
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8901
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8813
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8591
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7412
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6212
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4388
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2799
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1791
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.