Setting Class Attributes

Ron Adam rrr at ronadam.com
Wed Oct 26 01:05:20 EDT 2005



the.theorist wrote:

> So that it'll be easier to remember the next time I find myself in the
> same situation on a different task, I'll extend the discussion
> somewhat.
> 
> Coming from C, I had expected that I'd get a new empty dict every time
> the __init__ function ran. Guido (or some other benevolent) had decided
> to implement things a little bit differently in Python. I understand
> that most everything is a pointer in Python. (which gives us cool stuff
> like recursive data structures) So I was wondering, they could have
> made the behavior C-like, but chose not to. The decision to bind
> everything in the function to the same default args must be a
> reflection of the Python Ideology. Philosophically, why was it done
> this way, and how does it fit in with Python's design as a language.

I didn't read too closely the thread leading up to this point so I may 
be stating something already stated or that is already obvious to you.

There is a difference between a pointer and name binding that you should 
keep in mind.  When using pointers in C you can feasibly have a chain of 
pointers.  In Python, it doesn't work that way unless you explicitly 
define a pointer type object to be used in such a way.  The normal 
behavior is for an expression to bind a name with the object that is 
bound to the other name.  So...

    a = 1   # a binds to 1
    b = a   # b binds to 1
    c = b   # c binds to 1

So if you change what 'a' is bound to, 'b' and 'c' are still each bound 
to 1.

Some of the places where you get pointer type behavior is in container 
objects.  Look at the following sequence of instructions.

    a = 1               # a binds to 1
    b = [a]             # first item in list is bound to 1,
                             b binds to list.
    c = b               # c bind to same list b is bound to.
    a = 2               # a binds to 2
    print b[0]  ->  1   # print first item in list b is bound to.
    b[0] = 3            # change first item in list b is bound to to 3
    print c[0]  ->  3   # print first item in list c is bound to,
                        #   (same list as b is bound to)

If you can follow this easily you are well on your way to understanding 
Pythons storage model and philosophy.  Notice, a name isn't bound to 
another name.

This relationship is all over Python including the name binding of 
functions and class methods.  And also in class's as well.

Keep in mind the class body is executed at the time it is defined, and 
the methods are defined but not executed until they are used.  In the 
case of the __init__ it is when the class is called after an instance 
object is created.  When instances are created they share any class 
objects that were bound to names when the class was defined.  They don't 
share any objects that get bound to instance attribute names in methods 
later.

Does this help any?

Cheers,
    Ron



> (hopefully, reasons will help me remeber why things are the way they
> are, so I don't forget in the future)
> -----------------
> I've only been using Python for a few weeks. (Chose it over Perl,
> because Python syntax is cleaner). I really like Python (over C), as it
> makes coding and debugging much faster and easier.
> 



More information about the Python-list mailing list