Recursive class | can you modify self directly?

Russel Walker russ.pobox at gmail.com
Wed Jul 10 16:09:27 EDT 2013


On Wednesday, July 10, 2013 9:33:25 PM UTC+2, Terry Reedy wrote:
> On 7/10/2013 4:58 AM, Russel Walker wrote:
> 
> 
> 
> > There is the name x and the class instance (the object) which exists
> 
> > somewhere in memory that x points to. self is just another name that
> 
> > points to the same object (not self in general but the argument
> 
> > passed to the self parameter when a method is called). However if the
> 
> > code inside the method reassigns self to some other object, it
> 
> > doesn't change the fact that x still refers to the original object.
> 
> > So self is just a local variable (an argument).
> 
> 
> 
> Yes, parameters *names* are the initial local names of the function. 
> 
> Calling the first parameter of instancemethods 'self' is a convention, 
> 
> not a requirement.
> 
> 
> 
>  > The name self has no
> 
> > relevance to to the name x other than the fact that they point to the
> 
> > same object. So reassigning self has no effect on x. But modifying
> 
> > the object that self points to, does affect x because it points to
> 
> > the same object. Is this correct?
> 
> 
> 
> Yes. Multiple names for one objects are 'aliases'. Being able to modify 
> 
> a object with multiple names in different namespaces is both a boon and 
> 
> bug bait.
> 
> 
> 
> > So when you call x.somemethod() it's not passing x as the self
> 
> 
> 
> Python does not pass'x': it is 'call-by-object', not 'call-by-name'.
> 
> 
> 
> > argument, it's actually passing the object that x points to as the
> 
> > self argument. And that object has know knowledge of the fact that x
> 
> > points to it, or does it?
> 
> 
> 
> Some objects (modules, classes, functions) have definition names 
> 
> (.__name__ attributes) that are used in their representations (as in 
> 
> tracebacks). But they have no knowledge of their namespace names.
> 
> 
> 
> 
> 
> 
> 
> -- 
> 
> Terry Jan Reedy

Thanks Terry, I did read all that twice just to make sure it sunk in and it was really clear and helpful.



More information about the Python-list mailing list