473,411 Members | 2,530 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,411 software developers and data experts.

Class Variables (Python 2.2/2.3)

Hello Members,
I do not see any direct mention of class variables in new style
classes. Only class methods. Have I missed or is it trivial? If not,
how to define/implement class variables for new style classes?

Regards,
Srijit
Jul 18 '05 #1
2 1543
sr****@yahoo.com wrote:
Hello Members,
I do not see any direct mention of class variables in new style
classes. Only class methods. Have I missed or is it trivial? If not,
how to define/implement class variables for new style classes?


From my practical point of view, there are no differences between old and
new style classes in this respect:

def printThem():
for t in t1, t2, t3:
print t.name, "=", t.color
print

#the same goes for class Test(object): ...
class Test:
color = "blue"
def __init__(self, name):
self.name = name

t1 = Test("t1")
t2 = Test("t2")
t3 = Test("t3")

# class atttribute are accessed like instance attributes
printThem() #blue, blue, blue

# class attributes are shaded by instance attributes,
# which makes them good default values
t1.color = "red"
printThem() #red, blue blue

# class attributes are not copied on instantiation
Test.color = "green"
printThem() #red, green, green

And now let the experts speak up on the subtler aspects :-)

Peter
Jul 18 '05 #2
sr****@yahoo.com wrote:
Hello Members,
I do not see any direct mention of class variables in new style
classes. Only class methods. Have I missed or is it trivial? If not,
how to define/implement class variables for new style classes?


From my practical point of view, there are no differences between old and
new style classes in this respect:

def printThem():
for t in t1, t2, t3:
print t.name, "=", t.color
print

#the same goes for class Test(object): ...
class Test:
color = "blue"
def __init__(self, name):
self.name = name

t1 = Test("t1")
t2 = Test("t2")
t3 = Test("t3")

# class atttribute are accessed like instance attributes
printThem() #blue, blue, blue

# class attributes are shaded by instance attributes,
# which makes them good default values
t1.color = "red"
printThem() #red, blue blue

# class attributes are not copied on instantiation
Test.color = "green"
printThem() #red, green, green

And now let the experts speak up on the subtler aspects :-)

Peter
Jul 18 '05 #3

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

Similar topics

34
by: SeeBelow | last post by:
I see the value of a class when two or more instances will be created, but Python programmers regularly use a class when there will only be one instance. What is the benefit of this? It has a...
4
by: Neil Zanella | last post by:
Hello, I would like to know whether it is possible to define static class methods and data members in Python (similar to the way it can be done in C++ or Java). These do not seem to be mentioned...
6
by: Brian Jones | last post by:
I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = def __init__(self): print 'called a'
3
by: syd | last post by:
Hello all, In my project, I have container classes holding lists of item classes. For example, a container class myLibrary might hold a list of item classes myNation and associated variables...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
32
by: Matias Jansson | last post by:
I come from a background of Java and C# where it is common practise to have one class per file in the file/project structure. As I have understood it, it is more common practice to have many...
37
by: minkoo.seo | last post by:
Hi. I've got a question on the differences and how to define static and class variables. AFAIK, class methods are the ones which receives the class itself as an argument, while static methods...
16
by: Donn Ingle | last post by:
Hello, Is there a way I can, for debugging, access the instance variable name from within a class? E.g: Class X: def debug(self): print "My instance var is %s" % (some magic Python stuff) ...
2
by: alefajnie | last post by:
class A: this_is_original_variable_only_for_one_inctance = 0 def __init__(self, v): self.this_is_original_variable_only_for_one_inctance = v class B: this_is_common_for_all_instances =
0
by: Calvin Spealman | last post by:
Please re-evaluate your "need" for nesting classes in the first place. On Tue, Aug 12, 2008 at 1:06 PM, Cousson, Benoit <b-cousson@ti.comwrote: -- Read my blog! I depend on your acceptance...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
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.