'self' an unknown variable???

Alex Martelli aleax at aleax.it
Mon Jan 14 05:03:22 EST 2002


"Giorgi Lekishvili" <gleki at gol.ge> wrote in message
news:3C432616.8A5E3CB4 at gol.ge...
> Hello all!
>
> I have written a class, with the function write. The latter is defined
> as follows:
>
> def write(self, filename, matr, cols=self.Npred, format='asc'):
>     blablabla
>     return res
>
> This doesn't work. Then I changed cols=N, previously defined
> N=self.Npred.
> Now things ar ok.
>
> Why doesn't function self.some_attr in the function declaration?

The default values of any function are evaluated once, at the
time the def statement executes, NOT when the function is called.

At the time the def statement executes (presumably as a part
of the execution of a class statement, specifically in the
body of the class), there is no binding for name 'self' (and
if there were, it would have nothing to do with the binding
that this name will have later when the function is *called*).


Alex






More information about the Python-list mailing list