Do I always have to write "self." ?

Laurent POINTAL pointal at lure.u-psud.fr
Fri Apr 28 08:25:28 EDT 2000


On Fri, 28 Apr 2000 11:51:34 GMT, "Louis M. Pecora"
<pecora at anvil.nrl.navy.mil> wrote:
 
>: Martijn,
>: 
>: Thanks for the very nice tutorial.  Your example brings up some typical
>: stumbling blocks for us beginners.  Namely, references to mutable and
>: immutable objects.
>: 
>: In the above I would describe things as follows (am I right or wrong?):
>: 
>: (1) data and self.data point to the same object, a list, so when a
>: method is called it is the method that works on that instance of a list
>: and either data or self.data will append 'foo' to the same list object.
>: 
>: (2) when you do more=more+1 an new object (the more+1 value) is created
>: and more then points to it and no longer to the old value of self.more. 
>: The latter still exists and points to the original 10 object.
>: 
>: (3) Similar story here for interesting where a new object
>: interesting[10:] is created and interesting is "switched" to point to
>: it.  self.interesting still points tohe "foo" object.
>: 
>: I hope I got that right, because they have tripped me up a few times.
>: 
>: Thanks, again.

*** Note: taking references copies of the self members is the same
problem as importing * from a module: its only reference copying... 

*** Note2: there may be a hack (not tested), with copying
automatically members into locals:
	def copyself(self,loc) :
		for m in self.__dict__.keys():
			loc[m] = eval("self."+m)
	def mymethod(self) :
		self.copyself(locals())
		# Then you can use directly all members.

But this is still only a reference copying... if you wants to change
of referenced object in a member, you must use self.xxxxxx. 

A+

Laurent.

---
Laurent POINTAL - CNRS/LURE - Service Informatique Experiences
Tel/fax: 01 64 46 82 80 / 01 64 46 41 48
email  : pointal at lure.u-psud.fr  ou  lpointal at planete.net 



More information about the Python-list mailing list