Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Class initialization

Reply
Thread Tools

Re: Class initialization

 
 
Costin Gament
Guest
Posts: n/a
 
      08-08-2010
Thank you for your answer, but it seems I didn't make myself clear.
Take the code:
class foo:
a = 0
b = 0
c1 = foo()
c1.a = 5
c2 = foo()
print c2.a
5

Somehow, when I try to acces the 'a' variable in c2 it has the same
value as the 'a' variable in c1. Am I missing something?

On Sun, Aug 8, 2010 at 4:59 PM, Roald de Vries <(E-Mail Removed)> wrote:
>
> Your problem probably is that a and b are class variables; c1 and c2 are
> different objects (in your terminology: they point to different instances).
>
> See http://docs.python.org/tutorial/clas...#class-objects for more
> info.
>
> Cheers, Roald
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      08-08-2010
On Sun, 08 Aug 2010 17:14:08 +0300, Costin Gament wrote:

> Thank you for your answer, but it seems I didn't make myself clear. Take
> the code:
> class foo:
> a = 0
> b = 0
> c1 = foo()
> c1.a = 5
> c2 = foo()
> print c2.a
> 5


Incorrect.

>>> class foo:

.... a = 0
.... b = 0
....
>>> c1 = foo()
>>> c1.a = 5
>>> c2 = foo()
>>> print c2.a

0



--
Steven
 
Reply With Quote
 
 
 
 
Costin Gament
Guest
Posts: n/a
 
      08-08-2010
Apparently, the code I've given here does, in fact, work. Still, I am
encountering a similar problem in a much larger class (it is in a
separate module, if that is any help). Also, the variable I am having
trouble with is itself another class. I don't think it's appropriate
to paste so much code in here, so if anybody has some knowledge about
similar problems...

On Sun, Aug 8, 2010 at 5:49 PM, Steven D'Aprano
<(E-Mail Removed)> wrote:
> On Sun, 08 Aug 2010 17:14:08 +0300, Costin Gament wrote:
>
>> Thank you for your answer, but it seems I didn't make myself clear. Take
>> the code:
>> class foo:
>> ┬* a = 0
>> ┬* b = 0
>> c1 = foo()
>> c1.a = 5
>> c2 = foo()
>> print c2.a
>> 5

>
> Incorrect.
>
>>>> class foo:

> ... ┬* a = 0
> ... ┬* b = 0
> ...
>>>> c1 = foo()
>>>> c1.a = 5
>>>> c2 = foo()
>>>> print c2.a

> 0
>
>
>
> --
> Steven
> --
> http://mail.python.org/mailman/listinfo/python-list
>

 
Reply With Quote
 
Tim Harig
Guest
Posts: n/a
 
      08-08-2010
On 2010-08-08, Costin Gament <(E-Mail Removed)> wrote:
> Thank you for your answer, but it seems I didn't make myself clear.
> Take the code:
> class foo:
> a = 0
> b = 0
> c1 = foo()
> c1.a = 5
> c2 = foo()
> print c2.a
> 5
>
> Somehow, when I try to acces the 'a' variable in c2 it has the same
> value as the 'a' variable in c1. Am I missing something?


Others have told you that at a and b belong to the class object rather then
to the instance objects. Perhaps this will demonstrate the difference:

>>> class foo():

.... def __init__(self):
.... self.a = 0
.... self.b = 0
....
>>> c1 = foo()
>>> c1.a = 5
>>> c2 = foo()
>>> print c2.a

0
>>>

 
Reply With Quote
 
Jesse Jaggars
Guest
Posts: n/a
 
      08-08-2010
On Sun, Aug 8, 2010 at 10:01 AM, Tim Harig <(E-Mail Removed)> wrote:
> On 2010-08-08, Costin Gament <(E-Mail Removed)> wrote:
>> Thank you for your answer, but it seems I didn't make myself clear.
>> Take the code:
>> class foo:
>> * a = 0
>> * b = 0
>> c1 = foo()
>> c1.a = 5
>> c2 = foo()
>> print c2.a
>> 5
>>
>> Somehow, when I try to acces the 'a' variable in c2 it has the same
>> value as the 'a' variable in c1. Am I missing something?

>
> Others have told you that at a and b belong to the class object rather then
> to the instance objects. *Perhaps this will demonstrate the difference:
>
>>>> class foo():

> ... * * def __init__(self):
> ... * * * * * * self.a = 0
> ... * * * * * * self.b = 0
> ...
>>>> c1 = foo()
>>>> c1.a = 5
>>>> c2 = foo()
>>>> print c2.a

> 0
>>>>

> --
> http://mail.python.org/mailman/listinfo/python-list
>


Is it possible that you are using a mutable class object? A common
gotcha is to do something like this:

>>> class foo(object):

.... x = []
....
>>> a = foo()
>>> b = foo()
>>> a.x.append(123)
>>> b.x

[123]

And expect b.x to be an empty list.
 
Reply With Quote
 
Costin Gament
Guest
Posts: n/a
 
      08-08-2010
So you're saying I should just use __init__? Will that get me out of
my predicament?
No, I don't quite understand the difference between my exemple and
using __init__, but I will read the docs about it.

On Sun, Aug 8, 2010 at 6:01 PM, Tim Harig <(E-Mail Removed)> wrote:
>
> Others have told you that at a and b belong to the class object rather then
> to the instance objects. ┬*Perhaps this will demonstrate the difference:
>
>>>> class foo():

> ... ┬* ┬* def __init__(self):
> ... ┬* ┬* ┬* ┬* ┬* ┬* self.a = 0
> ... ┬* ┬* ┬* ┬* ┬* ┬* self.b = 0
> ...
>>>> c1 = foo()
>>>> c1.a = 5
>>>> c2 = foo()
>>>> print c2.a

> 0
>>>>

 
Reply With Quote
 
Costin Gament
Guest
Posts: n/a
 
      08-08-2010
That looks just like my code. What's the problem?

On Sun, Aug 8, 2010 at 6:13 PM, Jesse Jaggars <(E-Mail Removed)> wrote:
>
> Is it possible that you are using a mutable class object? A common
> gotcha is to do something like this:
>
>>>> class foo(object):

> ... ┬* x = []
> ...
>>>> a = foo()
>>>> b = foo()
>>>> a.x.append(123)
>>>> b.x

> [123]
>
> And expect b.x to be an empty list.

 
Reply With Quote
 
Tim Harig
Guest
Posts: n/a
 
      08-08-2010
On 2010-08-08, Costin Gament <(E-Mail Removed)> wrote:
> So you're saying I should just use __init__? Will that get me out of
> my predicament?
> No, I don't quite understand the difference between my exemple and
> using __init__, but I will read the docs about it.


It is not so much using __init__() that makes the difference as it what
scope the variables are assigned to. If you define them as you where, then
the variables are associated with the class object itself. If the variable
is a mutable type, and you change it in one instance, it actually changes
it in the class object which means it also changes for all of the
instances.

I used the constructor because it gives me a reference to the newly created
instance object "self". I then assign the variables to self, which
assignes them to the newly created instance object. Then each instance has
its own separate a and b variables that will not change when the variables
are changed inside of another instance object.
 
Reply With Quote
 
Costin Gamen╚Ť
Guest
Posts: n/a
 
      08-08-2010
Thanks a lot. I'll try give it a go and see if it helps.

On Sun, Aug 8, 2010 at 6:28 PM, Tim Harig <(E-Mail Removed)> wrote:
> It is not so much using __init__() that makes the difference as it what
> scope the variables are assigned to. ┬*If you define them as you where, then
> the variables are associated with the class object itself. ┬*If the variable
> is a mutable type, and you change it in one instance, it actually changes
> it in the class object which means it also changes for all of the
> instances.
>
> I used the constructor because it gives me a reference to the newly created
> instance object "self". ┬*I then assign the variables to self, which
> assignes them to the newly created instance object. ┬*Then each instance has
> its own separate a and b variables that will not change when the variables
> are changed inside of another instance object.

 
Reply With Quote
 
Tim Harig
Guest
Posts: n/a
 
      08-08-2010
On 2010-08-08, Tim Harig <(E-Mail Removed)> wrote:
> On 2010-08-08, Costin Gament <(E-Mail Removed)> wrote:
>> So you're saying I should just use __init__? Will that get me out of
>> my predicament?
>> No, I don't quite understand the difference between my exemple and
>> using __init__, but I will read the docs about it.

>
> It is not so much using __init__() that makes the difference as it what
> scope the variables are assigned to. If you define them as you where, then
> the variables are associated with the class object itself. If the variable
> is a mutable type, and you change it in one instance, it actually changes
> it in the class object which means it also changes for all of the
> instances.
>
> I used the constructor because it gives me a reference to the newly created
> instance object "self". I then assign the variables to self, which
> assignes them to the newly created instance object. Then each instance has
> its own separate a and b variables that will not change when the variables
> are changed inside of another instance object.


Maybe I can make that a little clearer yet. When you define a class in
python you actually create a class object. This object is basically used
as a template to create instance objects. When you define a variable
attached to the class object that is mutable, the instance objects receive
the exact same reference that was given to the instance object. Since it
is mutable, any changes made using that reference will affect all of the
instances that also point to that reference. You wouldn't have seen this
effect using your simplified examle because number are immutable objects.
When you change the value for one of the instance objects, it receives a
new reference, rather then making the change in place. The other instances
do not reflect this change because their variables still point back to the
original reference given to the class.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
array initialization in initialization list. toton C++ 5 09-28-2006 05:13 PM
Initialization of non-integral type in initialization list anongroupaccount@googlemail.com C++ 6 12-11-2005 09:51 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Initialization via ctor vs. initialization via assignment Matthias Kaeppler C++ 2 07-18-2005 04:25 PM
Default Initialization Vs. Value Initialization JKop C++ 10 09-22-2004 07:26 PM



Advertisments