[Tutor] self (again)

Alan Gauld alan.gauld at freenet.co.uk
Mon Aug 14 01:44:46 CEST 2006

> 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.

Correct. Classes are containers and they contain the class
variables and method definitions shared by all instances of
the class (and its subclasses).

> Using self makes the assignment 'local' to the instance.

Sort of, but self is just a reference to an instance of a class.
There is no magic involved (or very little), when you call a
method on an instance you simply execute the function
stored in the class object but pass in a reference to the
called instance as the first argument.

> create variables that will retain the assigned value for the 
> duration of the instance.

Correct, the instance is also a container that holds a reference
to the defining class and all the instance variables.

> This variable is 'global' for the instance.

I'd prefer to say local to the instance. Its not shared outside
the instance and can only be accessed in methods via the
self reference

> accessed by all the functions in the class/instance without being 
> passed as a parameter from function to function.

No, it can only be accessed by the methods via the self reference
that is passed implicitly in each method call.

> 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.

No, self is purely about allowing the methods stored in a
class object to access the instance variables of the specific
instance that invoked the method at any given point in time.

There are many ways to store data over a long period that
do not use self (global variables, lists, dictionaries, tuples,
files, databases). Even class attributes can do that,
regardless of whether any instances are created.

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

As they are in any other functuion, again nothing magic is
happening here.

> 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 elsewhere.

Again just like any other function, yes.

> If I use the names inside the function what problem will I give 
> myself.

None, thats what they are there for. They are your mechanism
for comunicating between the world outside the function and the
world (or namespace) inside.

> Are they local, global.

They are local. The name of the parameter bears norelation to what
that value is called (or referenced by) outsidfe the function.

> e.g. if parm1=='somevalue': How long can I trust the value in the 
> passed parameter - parm1

Until you change it or exit the function.

def f(a,b):
    if a > b: a = b
    return a+b

f(x,4)  -> 7
print x   -> 3
f(x,3)  -> 6
print x   -> 3
f(x,2)  -> 4
print x   -> 3

So even in the last case x remains equal to 3 but inside
the function the value of a was changed to 2. This is standard
function behaviour and there is nothing different about methods
in a class.

> What namespace are they in.

The parameters are in the local namespace of the method.

> 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.

I know nothing about mod_python but assuming it uses threading
then you need to avoid changing global variables or class variables
without using locks or semaphores. But instances created in a
thread should remain independant from each other - a good reason
for using OOP for multi threaded code!

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


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list