[Tutor] self (again)

John Aherne johna at johnaherne.co.uk
Sun Aug 13 10:27:46 CEST 2006

I have been following the thread on self because for the past month I 
have been driven potty trying to be sure that I understand the 
ramifications of how self works.

I have tried to create some code that tries to prove how self, 
variables, assignment and namespace gel together. I can't convince 
myself that I have it right.

I think it works as follows. I just need to know what is right.

Class variables are 'global' for the class and all instances of the class.

Using self makes the assignment 'local' to the instance. This is used to 
create variables that will retain the assigned value for the duration of 
the instance. This variable is 'global' for the instance. It can be 
accessed by all the functions in the class/instance without being passed 
as a parameter from function to function.

Self is used if you want to create a class where you store data for a 
'long' period of time. Not like local variables that only have duration 
for the execution of the function.

Within the def functions inside the class, assignments inside the 
function are local.

If the def function inside a class takes a set of parameters that are 
passed in from outside e.g.
class one:
     def func1(parm1,parm2,parm3):

These parameters are passed by reference. So they refer to values held 
If I use the names inside the function what problem will I give myself. 
Are they local, global.
e.g. if parm1=='somevalue': How long can I trust the value in the passed 
parameter - parm1

What namespace are they in.

I run all this under modpython on windows, where one python interpreter 
runs all the instances so I have multiple copies of the code running in 
the same interpreter. What is likely to happen to these values being 
passed by reference if several people log in at once and run the code.

Hope I have explained myself so people can understand what I mean.

Looking forward to the light of understanding.


John Aherne

More information about the Tutor mailing list