[Tutor] how do you use __dict__ ? (Was: code dangerous?)

Magnus Lycka magnus@thinkware.se
Thu Jan 9 21:23:02 2003


At 12:06 2003-01-10 +1000, Alfred Milgrom wrote:
>Can you suggest any good links or explain how I am supposed to use 
>__dict__ and __speak

It seems a walkthrough of names in python is required.

You can see from the names that __dict__ is some kind
of Python magic thing that you should be able to read
about in Python books or manuals. __speak, on the other
hand, it just a variable name. It's nothing special in
python.

First of all, names in python (names for variables,
functions, classes, modules etc) consists of an
uninterrupted sequence of A-Z, a-z, 0-9 or '_'. In
other words letters in the English alphabet, digits or
underscore. They may not begin with a digit, since the
could lead to confusion with numerical objects.

_ or __ or __speak are allowed names for variables. There
are some special rules and conventions though. Names
starting AND ending with double underscores have some
kind of magic function in Python. Never use double leading
and trailing underscores for your own variables or other
names. An example is __doc__ with identifies the documentation
string as in:
 >>> def x():
...     "This function doesn't do anything."
...     pass
...
 >>> print x.__doc__
This function doesn't do anything.

__dict__ is another example, it contains the attributes in a
class instance. __init__ is the constructor for a class.
 >>> class X:
...     def __init__(self):
...             self.x = 1
...             self._y = 2
...             self.__z = 3
...
 >>> x = X()
 >>> print x.__dict__
{'x': 1, '_X__z': 3, '_y': 2}

As you can see, __dict__ shows the values of the attributes of
x. You can also see that something odd happened with self.__z.
It's represented as _X__z in __dict__. See also below:

 >>> print x.__z
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
AttributeError: X instance has no attribute '__z'
 >>> print x._X__z
3

An attribute name starting with __ will have it's name mangled
as you see. This is how private attributes are handled in Python.
As you see, they aren't extremely private, you can access them if
you really need, but it's not difficult to spot such violations
in the code, so it's  helpful to implement encapsulation without
making introspection impossible.

This special handling of __names only exisits in this context though.
In attributes in class instance. For a local variable, __speak is
no different than sp_ea_k or speak__.

For the global scope, names starting with _, (single underscore is
enough) will not be imported with "from XXX import *".

This is the rules.

There are also a few conventions.

Many people emulate the protection scheme in C++ by using attribute
names like this:

__private
_protected
public

__private are name-mangled like I wrote above, _protected is only
protected by a gentlemens agreement, but that's enought among
friends, right?

Also, when you would like to use a reserved word as a name, such as
if or or or in, you can use if_ or or_ or in_ instead.


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se