What is self.file = file for?

Gary Herron gherron at islandtraining.com
Wed May 14 01:03:39 EDT 2008


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 at islandtraining.com> wrote:
>   
>> 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
>>>
>>>
>>>       
>>  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
>>
>>
>>
>>     




More information about the Python-list mailing list