473,397 Members | 1,950 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,397 software developers and data experts.

strange problem with def in class

I don't get it:
I'm trying to write a "readfile" function which would replace some code from
a __init__ function:

1 import string
2 class landdict:
3 def __init__(self, file):
4 # Laadt de dictionnary van landen.
5 # Kijk in de actiefspel-file voor de file van waaruit je
moet laden.
6 self.ld = {}
7 f = open(file, "r")
8 line = f.readline()
9 if line[0] == "#":
10 line = f.readline()
11 while line != "":
12 key = line[0:3]
13 self.ld[key] = string.split(line[4:], ':')
14 line = f.readline()
15 print self.ld

So I would like to replace the code from line 7 to line 14 with

readfile(filename)

while

def readfile(filename) is another function in the class

but I can't define this function.... when I call the function the
interpreter throws an exception of unknown function
what am I doing wrong?
--
__________________________________________________ __
Johan Potums
Vaartstraat 67
3000 Leuven

jo**********@student.kuleuven.ac.be

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.737 / Virus Database: 491 - Release Date: 11/08/2004
Jul 18 '05 #1
3 1400
Johan wrote:
I don't get it:
I'm trying to write a "readfile" function which would replace some code from [snip] but I can't define this function.... when I call the function the
interpreter throws an exception of unknown function what am I doing wrong?


Not posting the actual traceback including the real error that
you are getting. It will include the lines of source that
were involved and will help us help you...

-Peter
Jul 18 '05 #2
Johan wrote:
I don't get it:
I'm trying to write a "readfile" function which would replace some code from
a __init__ function:

1 import string
2 class landdict:
3 def __init__(self, file):
4 # Laadt de dictionnary van landen.
5 # Kijk in de actiefspel-file voor de file van waaruit je
moet laden.
6 self.ld = {}
7 f = open(file, "r")
8 line = f.readline()
9 if line[0] == "#":
10 line = f.readline()
11 while line != "":
12 key = line[0:3]
13 self.ld[key] = string.split(line[4:], ':')
14 line = f.readline()
15 print self.ld

So I would like to replace the code from line 7 to line 14 with

readfile(filename)

while

def readfile(filename) is another function in the class

but I can't define this function.... when I call the function the
interpreter throws an exception of unknown function
what am I doing wrong?


Hi Johan

Other variables defined in a class are in a scope that cannot be
accessed directly from within a method (Python newish nested scoping
only works for functions inside functions).

Personally I would move your readline() function outside the class to
the module level.

If you want to keep it in the class then either give it a self parameter
or add "readline = staticmethod(readline)" after the definition of
readline(). Then you can call it from within __init__() as
self.readline(filename).

HTH,
James

Jul 18 '05 #3
A couple of quick corrections.

James Henderson wrote:
Hi Johan

Other variables defined in a class are in a scope that cannot be
accessed directly from within a method (Python newish nested scoping
only works for functions inside functions).
Actually it works for classes defined inside functions too, but that's
not relevant here. :)
Personally I would move your readline() function outside the class to
the module level.
This still stands!
If you want to keep it in the class then either give it a self parameter
or add "readline = staticmethod(readline)" after the definition of
readline(). Then you can call it from within __init__() as
self.readline(filename).


Looking at your code again the static method option is not viable. Your
readline() method needs a self parameter because the code you intend to
put into it refers to self.

J

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Anon Email | last post by:
Hi people, I'm playing around with Bartosz Milewski's code at the moment, and I got the following strange results upon execution of the code included further below. Please be aware that I...
6
by: Eric Boutin | last post by:
Hi ! I have a strange problem with a std::ostringstream.. code : #include <sstream> /*...*/ std::ostringstream ss(); ss << "\"\"" << libpath << "\"\" \"\"" << argfilename << "\"\"...
6
by: WindAndWaves | last post by:
Hi Gurus The page below has a strange error. It seems to be working very well, just when you enter 8 or 9 for day, month or year then you get an error. I really have no idea where that is...
5
by: cody | last post by:
I have a very funny/strange effect here. if I let the delegate do "return prop.GetGetMethod().Invoke(info.AudioHeader, null);" then I get wrong results, that is, a wrong method is called and I...
8
by: Spam Trap | last post by:
I am getting strange resizing problems when using an inherited form. Controls are moving themselves seemingly randomly, but reproducibly. "frmBase" is my base class (a windows form), and...
4
by: Praveen_db2 | last post by:
Hi All I am getting strange errors in my db2diag.log can any one tell me what these errors mean?? Following is the code from my db2diag.log...
7
by: Paul Czubilinski | last post by:
Hello, I have a problem with code like this (PHP 5.1.4): fila A.php: ======= include(B.php); class document extends obj { .........
2
by: openbysource | last post by:
I wrote this program: #include <iostream> using namespace std; class sample { private: int age; float salary; char status;
2
by: zacks | last post by:
I am developing an app in VS2005 (actually in VB.NET but this question, I believe, would apply to any .NET language) that is used to design the contents of an XML file. One of potential items that...
2
by: scdowney | last post by:
First and foremost, thank you in advance for any attempts to help me out. I am working on a project with work, and it requires I use CSS selectors to locate elements within a webpage. For the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.