By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,187 Members | 1,629 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,187 IT Pros & Developers. It's quick & easy.

What is self.file = file for?

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
wx********@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
Jun 27 '08 #2

P: n/a
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
Jun 27 '08 #3

P: n/a
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...@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.
Jun 27 '08 #4

P: n/a
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
<gh*****@islandtraining.comwrote:
>wx********@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

Jun 27 '08 #5

P: n/a
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).
Jun 27 '08 #6

P: n/a
On May 14, 2:26*am, Bruno Desthuilliers <bruno.
42.desthuilli...@websiteburo.invalidwrote:
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).- Hide quoted text -

- Show quoted text -
I am at the point of open-source, and I agree.
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.