strange problem with def in class

James Henderson james at
Wed Aug 18 03:08:05 CEST 2004

A couple of quick corrections.

James Henderson wrote:

> Hi Johan
> Other variables defined in a class are in a scope that cannot be 
> accessed directly from within a method (Python newish nested scoping 
> only works for functions inside functions).

Actually it works for classes defined inside functions too, but that's 
not relevant here. :)

> Personally I would move your readline() function outside the class to 
> the module level.

This still stands!

> If you want to keep it in the class then either give it a self parameter 
> or add "readline = staticmethod(readline)" after the definition of 
> readline().  Then you can call it from within __init__() as 
> self.readline(filename).

Looking at your code again the static method option is not viable.  Your 
readline() method needs a self parameter because the code you intend to 
put into it refers to self.


More information about the Python-list mailing list