Using eval, or something like it...

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Nov 24 03:25:35 EST 2008


En Fri, 21 Nov 2008 13:58:59 -0200, r0g <aioe.org at technicalbloke.com>
escribió:

> I hadn't really appreciated the consequences of this till now though
> e.g. that an instance might do a = a + 1 without affecting it's siblings
> but that b.append("fish") would affect b for everyone. I don't know if I
> will find any uses for that kind of behaviour but it doesn't hurt to
> understand it :-)
>
> Isn't Python's behaviour a little peculiar in this respect though,
> compared to classes in other languages? i.e. Are instances in other OO
> languages like Smalltalk, C++ fully independent copies or do their
> attribute names just point to one common object until reassigned like in
> python? (Or have I still not it at all?!)

In C++, it depends on how you define the container object. It may contain
completely the other object inside it, or it may just hold a
pointer/reference (that is: Other o, Other* o, Other& o are valid
alternatives). Unlike Python, the assignment `=` is an operator, and a=b
is an operation performed over the object `a` with argument `b` (because
`a` is a variable, pre-existent and pre-declared to be of a certain type).
In Object Pascal / Delphi, instance variables hold references to objects,
like Python (but basic types like integer, double, string aren't objects,
and you have to explicitely destroy objects when not needed anymore) [1].
a=b stores in `a`, a reference to the object `b`; you have no control over
its behavior.
In Smalltalk, things are like in Python I believe (my Smalltalk is
becoming more and more rusty). Objects hold references to other objects
(instance variables) - but encapsulation is strictly enforced, and they
can't be changed from the outside. Everything is resolved using message
passing between objects, except assignment: a:=b assigns the object `b` to
instance variable `a` directly.

[1] There was a way to declare objects which were not references (using
the 'object' keyword instead of 'class') but I'm not sure it is still
supported

-- 
Gabriel Genellina




More information about the Python-list mailing list