[Tutor] The trap of the year

Izz ad-Din Ruhulessin izzaddin.ruhulessin at gmail.com
Tue Jan 25 22:08:08 CET 2011


Or the internal memory id or whatever it's called.

2011/1/25 Izz ad-Din Ruhulessin <izzaddin.ruhulessin at gmail.com>

> I think it has something to do with the physical id of the object
>
> 2011/1/25 Karim <karim.liateni at free.fr>
>
>
>> Hello All,
>>
>> Just to share on rageous bug I encounter where 2 lists which "should" to
>> be different (same id) because hold by different instances of the same class
>> are not in fact DIFFERENT, see below:
>>
>> >>> class Device():
>> ...     def __init__(self, parameters=[]):
>> ...         self.parameters = parameters
>> ...     def param(self):
>> ...         print(id(self.parameters))
>> ...
>> >>> a=Device()
>> >>> b=Device()
>> >>> a.param()
>> 140559202956568
>> >>> b.param()
>> 140559202956568
>>
>> When I discovered that I was puzzled because at the prompt:
>>
>> >>> a = []
>> >>> b = []
>> >>> id(a)
>> 140559202956496
>> >>> id(b)
>> 140559202957000
>>
>> I am not really understanding why my init in the class made it refers to
>> the same list object.
>> What is the difference with 2nd example directly at the prompt?
>>
>> By the way, this one is ok:
>>
>> >>> class Device():
>> ...     def __init__(self,parameters=None):
>> ...         self.parameters = None
>> ...         self.parameters = []
>> ...     def param(self):
>> ...         print(id(self.parameters))
>> ...
>> >>> a=Device()
>> >>> b=Device()
>> >>> b.param()
>> 140559202956496
>> >>> a.param()
>> 140559202956568
>>
>> Karim
>>
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110125/12f6dd7d/attachment.html>


More information about the Tutor mailing list