473,320 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
Jun 27 '08 #1
6 1676
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Jason | last post by:
Hey, I'm an experience programmer but new to Python. I'm doing a simple implementation of a field morphing techinique due to Beier and Neely (1992) and I have the simple case working in Python...
1
by: Andrew James | last post by:
All, I'm having some trouble with understanding python's importing behaviour in my application. I'm using psyco to optimise part of my code, but I'm not sure whether it inherits throughout the...
26
by: Chris Lasher | last post by:
Hello, I have a rather large (100+ MB) FASTA file from which I need to access records in a random order. The FASTA format is a standard format for storing molecular biological sequences. Each...
3
by: neutrinman | last post by:
I cannot find out why the following code generates the error: Traceback (most recent call last): File "D:/a/Utilities/python/ptyhon22/test.py", line 97, in ? main() File...
0
by: ewitkop90 | last post by:
This is the box and the variables. ewitkop $ uname -a SunOS remnssdtoolsp01 5.9 Generic_118558-03 sun4u sparc SUNW,UltraAX-i2 ...
2
by: Johnny Lee | last post by:
Here is the source: #! /bin/python #@brief This is a xunit test framework for python, see TDD for more details class TestCase: def setUp(self): print "setUp in TestCase"
9
by: Karlo Lozovina | last post by:
Here is it: --- class Human: def __init__(self, eye_one, eye_two): self.eye_one = eye_one self.eye_two = eye_two class Population: def __init__(self):
6
by: seb | last post by:
Hi, I am using pygtk for the first times. I am wondering what would be the best "pattern" to interface pygtk with a thread. The thread is collecting informations (over the network for...
27
by: Steven D'Aprano | last post by:
I thought that an iterator was any object that follows the iterator protocol, that is, it has a next() method and an __iter__() method. But I'm having problems writing a class that acts as an...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.