473,549 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1692
wx********@gmai l.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...@gmai l.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*****@island training.comwro te:
>wx********@gmai l.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...@gmai l.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.i nvalidwrote:
afrobeard a écrit :

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


On May 14, 3:08 am, wxPytho...@gmai l.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
2569
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 2.3 - but it's REALLY slow. Basically, you specify two directed line segments in the coordinate system of a raster image and use the difference...
1
2372
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 rest of my application (read this as for any imported module) if I import in in a 'higher-level' module. For example: A.py ====
26
3439
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 record contains a header line for describing the sequence that begins with a '>' (right-angle bracket) followed by lines that contain the actual...
3
1822
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 "D:/a/Utilities/python/ptyhon22/test.py", line 60, in main crit = Critter(crit_name) File "D:/a/Utilities/python/ptyhon22/test.py", line 8, in __init__ self.feed = feed ...
0
1350
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 LD_LIBRARY_PATH=:/usr/lib:/usr/local/lib:/opt/sfw/lib:/usr/sfw/lib:/usr/local/lib/python2.3/site-packages/roundup:/usr/lib/lwp:/usr/local/lib/python2.3:/usr/local/lib/python2.3/site-packages/roundup/scripts...
2
1538
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
1334
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
2853
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 example) or is doing some long calculations.
27
5298
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 iterator. I have: class Parrot(object): def __iter__(self): return self def __init__(self): self.next = self._next()
0
7548
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7743
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7986
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7504
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6074
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5114
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3518
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.