473,387 Members | 1,569 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 properties and object properties

Learning python I was rewriting some of my old programs to see the
pros and cons of python when a steped in some weird (at least for me)
behavior.

Here it is simplified

The code:
>>class Test1:
myList = [4 for n in range(4)]
myInt = 4
>>a = Test1()
b = Test1()
a.myList
[4, 4, 4, 4]
>>a.myInt
4
>>b.myList
[4, 4, 4, 4]
>>b.myInt
4
>>b.myList[2] = 3
b.myInt = 3
b.myList
[4, 4, 3, 4]
>>b.myInt
3
>>a.myList
[4, 4, 3, 4]
>>a.myInt
4
I would not expect the second a.myList to have changed as it did
since, for me, I have only changed b.myList. And also, why only the
list changed and not the integer?

One thing i tried was:
>>class Test2:
myList = []
myInt = 4
def __init__(self):
self.myList = [4 for n in range(4)]
>>a = Test2()
b = Test2()
a.myList
[4, 4, 4, 4]
>>b.myList
[4, 4, 4, 4]
>>b.myList[2] = 3
b.myList
[4, 4, 3, 4]
>>a.myList
[4, 4, 4, 4]
And as you see it worked as I expected.

Now the question, why?
Oct 6 '08 #1
4 1029
SuperZE wrote:
Learning python I was rewriting some of my old programs to see the
pros and cons of python when a steped in some weird (at least for me)
behavior.

Here it is simplified

The code:
>>>class Test1:
myList = [4 for n in range(4)]
myInt = 4
>>>a = Test1()
b = Test1()
a.myList
[4, 4, 4, 4]
>>>a.myInt
4
>>>b.myList
[4, 4, 4, 4]
>>>b.myInt
4
>>>b.myList[2] = 3
b.myInt = 3
b.myList
[4, 4, 3, 4]
>>>b.myInt
3
>>>a.myList
[4, 4, 3, 4]
>>>a.myInt
4
I would not expect the second a.myList to have changed as it did
since, for me, I have only changed b.myList. And also, why only the
list changed and not the integer?

One thing i tried was:
>>>class Test2:
myList = []
myInt = 4
def __init__(self):
self.myList = [4 for n in range(4)]
>>>a = Test2()
b = Test2()
a.myList
[4, 4, 4, 4]
>>>b.myList
[4, 4, 4, 4]
>>>b.myList[2] = 3
b.myList
[4, 4, 3, 4]
>>>a.myList
[4, 4, 4, 4]
And as you see it worked as I expected.

Now the question, why?
Because you declare myList to be a *class*-level variable, which means *all*
instances of that class (a and b in your case) *share* it. Python does not
declare *instance* variables the way you do.

Instead, do this:

class Foo(object):
def __init__(self):
self.myList = []

And to avoid a common pitfall you are likely to hit next: default arguments
in functions (and methods) are evaluated only *once*, when defined - not
later!

So

def foo(some_arg=[]):
some_arg.append("a")
print some_arg

foo()
foo()

will result in

['a']
['a', 'a']

being printed out.

Diez
Oct 6 '08 #2
Because you declare myList to be a *class*-level variable, which means *all*
instances of that class (a and b in your case) *share* it. Python does not
declare *instance* variables the way you do.

Instead, do this:

class Foo(object):
* * def __init__(self):
* * * * self.myList = []

Interesting, but that does not explain the difference in the behavior
of myList and myInt

Both were class-level variables, as far as I can see, and therefor a
and b should also share it
And good remind on default arguments :)
Oct 6 '08 #3
On Mon, Oct 6, 2008 at 9:38 AM, SuperZE <al**********@gmail.comwrote:
Interesting, but that does not explain the difference in the behavior
of myList and myInt

Both were class-level variables, as far as I can see, and therefor a
and b should also share it
They did share it, until you assigned an instance variable in b, which
shadowed the class variable. Example:
>>class Test1:
myInt = 4

>>a = Test1()
b = Test1()
a.myInt
4
>>b.myInt
4
>>Test1.myInt
4
>>b.myInt = 3
a.myInt
4
>>b.myInt
3
>>Test1.myInt
4

As soon as you bound the name b.myInt to a new value, it created an
instance variable. That hides the value of Test1.myInt.

--
Jerry
Oct 6 '08 #4
TYVM Diez and Jerry

Now I understand how this works
Oct 6 '08 #5

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

Similar topics

1
by: BCC | last post by:
Hi, This is kind of a followup to a previous question... I have a container class that has a load of variables (~100). For example, I have groupings like this: class CContainer {...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
7
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
14
by: Lee Franke | last post by:
I can't seem to figure this one out. Here is my class structure namespace name { public class foo { } }
8
by: Joel Reinford | last post by:
I would like to build a class that has properties which can be accessed by string names or index numbers in the form of MyClass.Item("LastName"). The string names or item index values would be...
16
by: Dennis | last post by:
I have a class named "myclass" and an arraylist containing elements of type "MyClass". I want to get the value of a property of "MyClass" (a string type) for one of the arraylist elements. I...
9
by: David A. Osborn | last post by:
I have a set of classes that each have an enumeration in them, and based on dynamic input I need to access a different enumeration. For example Three classes Class_A, Class_B, and Class_C that...
17
by: Jef Driesen | last post by:
Suppose I have a datastructure (actually it's a graph) with one template parameter (the property P for each edge and vertex): struct graph<P>; struct vertex<P>; struct edge<P>; I also have...
3
by: Simon Hart | last post by:
Hi, I am trying to implement some functionality as seen in MS CRM 3.0 whereby a basic Xml is deserialized into an object which contains properties. What I want to do from here is; cast the basic...
15
by: Anthony Greene | last post by:
This is probably a very introductory object-oriented question, but it has been nagging me for years, and since I've never been able to find the right answer, I've had to work around it with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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
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.