What is equivalent of *this = that in python?

Terry Reedy tjreedy at udel.edu
Fri Jun 1 19:00:40 CEST 2007


"Emin.shopper Martinian.shopper" <emin.shopper at gmail.com> wrote in message 
news:32e43bb70706010830t38e69cb1t732e5dbdaff6cd1b at mail.gmail.com...
| How do I reassign self to another object?

Exactly the way you give below.

|For example, I want something like
|
| class foo:
|    def Update(self,other):

Self and other are two local names.
Self is presumed to be bound to an instance of foo.
Other can be bound to anything, even the same thing as self.
But presumably, the other object should be at least foo-compatible.

| # make this object the same as other or make this object a copy of other

'same as' is a bit ambiguous.  'a copy' slightly less so.

|        self = other # This won't work.

Yes is does.  It reassigns the name 'self' to another object,
which is what you requested at the top ;-)
To copy attributes, you need self.attribute = other.attribute.
Also look into the copy module for cloning objects.

| What I really want is *this = other in C++ terminology.

Strictly speaking, this is meaningless in Python terms (names, collection 
slots, attributes, and objects).

Terry Jan Reedy









More information about the Python-list mailing list