what is happening at __init__

Emile van Sebille emile at fenx.com
Mon Mar 4 06:51:47 EST 2002


"Benjamin Tai"
[snip]
> class A:
>     x = 100
>
> class B(A):
>
>     def __init__(self):
>         #  Q1
>         print 'inside constructor self is ', self
>         self = 300

Self is a container.  Assign to its attributes, ie, self.x = 300.
You've just created a new thing called self as a local variable within
__init__.

>         # Q2
>         print 'inside constructor self is ', self

Again, as a local variable this is 300.  Make the change above then try
printing self.x instead.

>         # Q3
>         return None

No need to do this.  All functions return None when not explicity
returning something else.

>
> b = 200

This is immediately changed with the following statement, and has no
effect.

> b = B()
> print 'after constructor instance b is ', b
> # Q4
> print 'instance b contains attribute x, valued ', b.x

B has not defined an x attribute, so this comes from A.

Work through the appropriate tutorial sections again.

HTH,

--

Emile van Sebille
emile at fenx.com




More information about the Python-list mailing list