f



What is self.file = file for?

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.
0
wxPythoner (11)
5/13/2008 10:08:26 PM
comp.lang.python 77058 articles. 6 followers. Post Follow

6 Replies
1129 Views

Similar Articles

[PageSpeed] 7

wxPythoner@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
>   

If you know about Object-Oriented Programming this should make sense.  
If you don't, then you have some reading/learning to do first.

When someone wants to create an object of type TextReader, they must 
supply a value
   ob = TextReader(v)
That calls the __init__ constructor with the supplied value of v in the 
variable named file.
If the object being created wants to record the value for future use, 
then the line
  self.file = file
does just that.   "self" is the name of the object being created, 
"self.file" is an attribute named "file" of that object, and the 
assignment stores the supplied value there.

Gary Herron


0
gherron (537)
5/13/2008 10:25:16 PM
> 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.

This is a standard object oriented programming idiom. You might find
it useful to ask around on the 'tutor' mailing list of python --
http://mail.python.org/mailman/listinfo/tutor -- where you'll get
detailed explanations on basic OOP and python topics.

Cheers,
Daniel
-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown
0
fetchinson (371)
5/13/2008 10:34:03 PM
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=A0am, wxPytho...@gmail.com wrote:
> Hello!
>
> I have trouble understanding something in this code snippet:
>
> class TextReader:
> =A0 =A0 """Print and number lines in a text file."""
> =A0 =A0 def __init__(self, file):
> =A0 =A0 =A0 =A0 self.file =3D file
> =A0 =A0 =A0 =A0 .
> =A0 =A0 =A0 =A0 .
> =A0 =A0 =A0 =A0 .
>
> When would you do a thing like =A0self.file =3D file =A0? I really don't
> find an answer on this. Please help me understand this.

0
afrobeard (12)
5/13/2008 10:39:25 PM
Chester wrote:
> I see. A very good explanation indeed. Thank you for it. But why would
> anyone want to do this anyway?
>   

In a single sentence, OOP (Object Oriented Programming) is a way to 
organize a program around your data and the operations on that data.  
Many books and college courses are dedicated to OOP.  Years could be 
spent learning to apply its concepts.  A web search would find a flood 
of such information.

Gary Herron
>
>
>
> On Wed, May 14, 2008 at 12:25 AM, Gary Herron
> <gherron@islandtraining.com> wrote:
>   
>> wxPythoner@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
>>>
>>>
>>>       
>>  If you know about Object-Oriented Programming this should make sense.  If
>> you don't, then you have some reading/learning to do first.
>>
>>  When someone wants to create an object of type TextReader, they must supply
>> a value
>>   ob = TextReader(v)
>>  That calls the __init__ constructor with the supplied value of v in the
>> variable named file.
>>  If the object being created wants to record the value for future use, then
>> the line
>>   self.file = file
>>  does just that.   "self" is the name of the object being created,
>> "self.file" is an attribute named "file" of that object, and the assignment
>> stores the supplied value there.
>>
>>  Gary Herron
>>
>>
>>
>>     

0
gherron (537)
5/14/2008 5:03:39 AM
afrobeard a �crit :

(top-post corrected. Please, do not top-post).

> On May 14, 3:08 am, wxPytho...@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.
> 
> 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.

Nope. It's a reference to the instance.

> Hence self.file is creating
> a class member

Nope. It's setting an instance attribute.

> 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__

Nope. Obviously, both parameters are passed - else it just wouldn't 
work. Given an object 'obj' instance of class 'Cls', you can think of 
obj.method(arg) as a convenient shortcut for Cls.method(obj, arg).
0
5/14/2008 7:26:54 AM
On May 14, 2:26=A0am, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalid> wrote:
> afrobeard a =E9crit :
>
> (top-post corrected. Please, do not top-post).
>
>
>
>
>
> > On May 14, 3:08 am, wxPytho...@gmail.com wrote:
> >> Hello!
>
> >> I have trouble understanding something in this code snippet:
>
> >> class TextReader:
> >> =A0 =A0 """Print and number lines in a text file."""
> >> =A0 =A0 def __init__(self, file):
> >> =A0 =A0 =A0 =A0 self.file =3D file
> >> =A0 =A0 =A0 =A0 .
> >> =A0 =A0 =A0 =A0 .
> >> =A0 =A0 =A0 =A0 .
>
> >> When would you do a thing like =A0self.file =3D file =A0? I really don'=
t
> >> find an answer on this. Please help me understand this.
>
> > 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.
>
> Nope. It's a reference to the instance.
>
> > Hence self.file is creating
> > a class member
>
> Nope. It's setting an instance attribute.
>
> > 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__
>
> Nope. Obviously, both parameters are passed - else it just wouldn't
> work. Given an object 'obj' instance of class 'Cls', you can think of
> obj.method(arg) as a convenient shortcut for Cls.method(obj, arg).- Hide q=
uoted text -
>
> - Show quoted text -

I am at the point of open-source, and I agree.
0
castironpi (1589)
5/14/2008 4:58:46 PM
Reply: