strange problem with def in class

Discussion in 'Python' started by Johan, Aug 18, 2004.

  1. Johan

    Johan Guest

    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





    ---
    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
     
    Johan, Aug 18, 2004
    #1
    1. Advertising

  2. Johan

    Peter Hansen Guest

    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
     
    Peter Hansen, Aug 18, 2004
    #2
    1. Advertising

  3. 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
     
    James Henderson, Aug 18, 2004
    #3
  4. 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
     
    James Henderson, Aug 18, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jiong Feng
    Replies:
    0
    Views:
    929
    Jiong Feng
    Nov 19, 2003
  2. Sean Ross
    Replies:
    3
    Views:
    142
    Aredridel
    Dec 25, 2003
  3. Replies:
    7
    Views:
    178
  4. planetthoughtful

    Newbie: def must come before call to def?

    planetthoughtful, Mar 12, 2007, in forum: Ruby
    Replies:
    4
    Views:
    144
    Pit Capitain
    Mar 12, 2007
  5. Kyung won Cheon
    Replies:
    0
    Views:
    230
    Kyung won Cheon
    Nov 21, 2008
Loading...

Share This Page