variable scope of class objects

Nagy László Zsolt gandalf at shopzeus.com
Tue Oct 20 01:49:08 EDT 2015


> My questions are:
> What is the scope of class variables?
In Python, you bind values (objects) to names. It is conceptually
different from "setting the value of a variable". In Python, scope
applies to names, not variables.

When you say "class variable", what do you mean?

This may help:

A namespace is just a dictionary that binds names to objects. AFAIK
there are three kinds of namepaces: local, global and namespaces of
other programming tools that can define their own namespaces. Namely:
eache modul and class defines its own namespace. So if you bind a name
to an object at module level, then that name will be put into the
module's namespace. When you bind a name inside a class, for example:

class A(object):
    example_name = 5

then the name "example_name" will be placed in the namespace of the
class, and it can be accessed using the class. E.g. "A.example_name".

The so called local namespace belongs to functions and methods being
called. Any name that is assigned locally inside a method will be a
local name of that method, and it will be visible from that method only
- except if you explicitely tell that it should be global using the
"global" keyword. Formal parameters of methods are also local names, but
they get assigned a value when the method is called. The process of
assigning actucal objects for formal parameters is called binding. When
binding is done, the parameters will become "actual parameters" or
"arguments" that have an actual object assigned. Please note the
difference between formal parameters and arguments:

  * Formal parameters are syntactical. They do not have an object
    (value) assigned.
  * Arguments are objects bound to formal parameters. They do have an
    object assigned, but arguments only exist while the method is being
    called (or ACTIVE, meaning: they are part of the call chain).


Methods of objects and classes always have an implicit parameter, that
is bound to the object or class the method was called on. In the case of
classmethods, it will be a class. In the case of objects, it will be an
object. (In case of static methods, there is no implicit parameter.)

Apart from being an implicit parameter, there is nothing special about
the first parameter of a method.


> does the self. prefix modify this scope?
It should be clear now, that in this case:

def pressure_calc(self):

the name "self" is a formal parameter of the "pressure_calc" method, because it is in the formal parameter list. When this method gets called, the name "self" is assigned to the object the method was called on. Because the name "self" is a parameter, it will be a local name of the method, and it will be accessible from inside that method only.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20151020/e19fad5e/attachment.html>


More information about the Python-list mailing list