What is self.file = file for?

W

wxPythoner

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.
 
G

Gary Herron

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

Daniel Fetchinson

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
 
A

afrobeard

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.
 
G

Gary Herron

Chester said:
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
 
B

Bruno Desthuilliers

afrobeard a écrit :

(top-post corrected. Please, do not top-post).
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).
 
C

castironpi

afrobeard a écrit :

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







Nope. It's a reference to the instance.


Nope. It's setting an instance attribute.



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 quoted text -

- Show quoted text -

I am at the point of open-source, and I agree.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top