473,387 Members | 1,365 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,387 software developers and data experts.

class members vs instance members

Ive spent a few days going thru a couple of Python tutorials. No
problem until I got to classes. I guess my java mindset is blocking my
vision. I've borrowed another thread's code snippet and cannot explain
the results:
class MyClass:
list = []
myvar = 10

def add(self, x):
self.list.append(x)
self.myvar = x

def printer(self):
print self.list
print self.myvar

a = MyClass()
b = MyClass()

for n in range(20):
a.add(n)

print "list in a:"
a.printer()
print "list in b:"
b.printer()

This produces:
list in a:
list in a:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
19
list in b:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
10

WHY do the "class members" list and myvar seem to behave differently?
Given the way that list[] is supposedly shared why doesnt myvar exhibit
the same behavior? It seems that myvar is acting like a true "instance
member". Is this simply because of the underlying types?
TIA

Jul 9 '06 #1
2 1655
hdixon a écrit :
Ive spent a few days going thru a couple of Python tutorials. No
problem until I got to classes. I guess my java mindset is blocking my
vision.
Then http://dirtsimple.org/2004/12/python-is-not-java.html
I've borrowed another thread's code snippet and cannot explain
the results:
class MyClass:
class MyClass(object):
list = []
This may not be too harmful in the given context, nut shadowing builtin
types may not be a good idea.
myvar = 10

def add(self, x):
self.list.append(x)
"list" is a class attribute. It's shared by all instances of MyClass.
This is a FAQ AFAICT.
self.myvar = x
This creates an instance attribute named myvar that shadows the class
attribute of the same name

(snip expected results)
WHY do the "class members" list and myvar seem to behave differently?
cf above. And notice that in Python, binding (aka 'assignement') and
name lookup on objects are different beasts. There have been quite a few
threads about this recently.
Given the way that list[] is supposedly shared why doesnt myvar exhibit
the same behavior?
Because binding to self.myvar creates the instance attribute "myvar".
Notice that you have not rebound "list".
It seems that myvar is acting like a true "instance
member".
It is one.
Is this simply because of the underlying types?
Nope, it's because in the first case (MyClass.list, accessed as
self.list) you just call methods on the list object, which has no effect
on the binding.
Jul 9 '06 #2

Bruno Desthuilliers wrote:
hdixon a écrit :
Ive spent a few days going thru a couple of Python tutorials. No
problem until I got to classes. I guess my java mindset is blocking my
vision.

Then http://dirtsimple.org/2004/12/python-is-not-java.html
I've borrowed another thread's code snippet and cannot explain
the results:
class MyClass:

class MyClass(object):
list = []

This may not be too harmful in the given context, nut shadowing builtin
types may not be a good idea.
myvar = 10

def add(self, x):
self.list.append(x)

"list" is a class attribute. It's shared by all instances of MyClass.
This is a FAQ AFAICT.
self.myvar = x

This creates an instance attribute named myvar that shadows the class
attribute of the same name

(snip expected results)
WHY do the "class members" list and myvar seem to behave differently?

cf above. And notice that in Python, binding (aka 'assignement') and
name lookup on objects are different beasts. There have been quite a few
threads about this recently.
Given the way that list[] is supposedly shared why doesnt myvar exhibit
the same behavior?

Because binding to self.myvar creates the instance attribute "myvar".
Notice that you have not rebound "list".
It seems that myvar is acting like a true "instance
member".

It is one.
Is this simply because of the underlying types?

Nope, it's because in the first case (MyClass.list, accessed as
self.list) you just call methods on the list object, which has no effect
on the binding.
ok - i "think" its beginning to dawn on me. BTW - the blog above was a
good read for me. I _am_ trying to bring java into the mix and its
screwing me up big time. I'll go back over the chapters that dicuss
binding/name lookups.
Thanks

Jul 9 '06 #3

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

Similar topics

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...
5
by: kuvpatel | last post by:
Hi I want to refer a class called LogEvent, and use one of its methods called WriteMessage without actually having to create an instance of Logevent. I have tried using the word sealed with...
5
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
9
by: thomson | last post by:
Hi all, Would you please explain me where will be the heap stored if it is declared inside the Class, As class is a reference type, so it gets stored on the heap, but struct is a value...
8
by: kevin | last post by:
I have a form and in the form I have a sub that uses a class I instantiate using visual basic code: Public oCP As New Rs232 'instantiate the comm port I need to share this sub with...
19
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the...
13
by: learning | last post by:
Hi I have a static class written by other team which encapsulates a database instance. but I need to extend it to incldue other things. I know that C# static class is sealed and can;t be inherited...
19
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit...
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
3
by: puzzlecracker | last post by:
Would you quickly remind me the difference between, regular class, static class, and nested class? Thanks
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.