From: Costin Gament on
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
<steve(a)remove-this-cybersource.com.au> 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
>
From: Tim Harig on
On 2010-08-08, Costin Gament <costin.gament(a)gmail.com> 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
>>>
From: Jesse Jaggars on
On Sun, Aug 8, 2010 at 10:01 AM, Tim Harig <usernet(a)ilthio.net> wrote:
> On 2010-08-08, Costin Gament <costin.gament(a)gmail.com> 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.
From: Costin Gament on
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 <usernet(a)ilthio.net> 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
>>>>
From: Costin Gament on
That looks just like my code. What's the problem?

On Sun, Aug 8, 2010 at 6:13 PM, Jesse Jaggars <jhjaggars(a)gmail.com> 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.
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4
Prev: Renaming of files in OS directory
Next: DjangoCon 2010