strange problem with def in class

J

Johan

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

(e-mail address removed)
 
P

Peter Hansen

Johan said:
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
 
J

James Henderson

Johan said:
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
 
J

James Henderson

A couple of quick corrections.

James said:
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
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top