What is self.file = file for?

Banibrata Dutta banibrata.dutta at gmail.com
Wed May 14 01:30:02 EDT 2008


__init__() is the object-constructor for TextReader class, accepting an
argument 'file' (reference to an Object). TextReader has a member variable /
attribute, called 'file' too (same name as the argument to __init__()). The
constructor is invoked when an object of TextReader class is being create.
At that point, the construct binds the reference of the passed file object,
to it's own instance-variable.

'self' is the equivalent of 'this' pointer in C++ as others have mentioned.
So 'self.file' is referring to the instance variable, on the left-hand-side
of the assignment operation, and the 'file' on the right-hand-side is the
method argument.

On 5/14/08, wxPythoner at gmail.com <wxPythoner 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.
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
regards,
Banibrata
http://www.linkedin.com/in/bdutta
http://octapod.wordpress.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080514/0cb5e41e/attachment-0001.html>


More information about the Python-list mailing list