What is self.file = file for?

afrobeard afrobeard at gmail.com
Tue May 13 18:39:25 EDT 2008


If you are familiar to C++ or a similar language, the concept of the
this pointer might not be alien to you. self in this context is
basically a reference to the class itself. Hence self.file is creating
a class member and setting to the input from file.

As Gary pointed out, during initialization, only the latter parameter
i.e. file is being passed to __init__

You can get a brief tutorial from http://docs.python.org/tut/node11.html
about classes in python.

On May 14, 3:08 am, wxPytho... at gmail.com wrote:
> Hello!
>
> I have trouble understanding something in this code snippet:
>
> class TextReader:
>     """Print and number lines in a text file."""
>     def __init__(self, file):
>         self.file = file
>         .
>         .
>         .
>
> When would you do a thing like  self.file = file  ? I really don't
> find an answer on this. Please help me understand this.




More information about the Python-list mailing list