__init__ & attributes clarification

Alex Martelli aleax at aleax.it
Sun Feb 2 23:36:26 CET 2003

Afanasiy wrote:
> This is unusual in my experience... So, if an object writes to a member
> with the same name as a class member, does it change that class member for
> the class, all instances of the class, all future instances, or just self?

"writes to" is ambiguous -- can mean "rebind" or "modify" (e.g.,
call the .append method on a list object) -- two totally different
cases, of course.

Assuming you do mean rebind:

class X:
    foo = 23
    def rebindinself(self):
        self.foo = 34
    def rebindinclass(self):
        X.foo = 45

is there any possible doubt left about which method rebinds
the attribute of class X, and which one instead rebinds a
(per-instance) attribute of self...?


More information about the Python-list mailing list