[Tutor] Default parameter in class method

Daniele d.conca at gmail.com
Tue Oct 7 16:08:32 CEST 2008


Hi all,
I know very little about OOP in Python, I'm working on it but, for the
time being, here's my problem:
I want to create a method of a class with a default value. The problem
is that this default value should be an instance field of that same
class. For example:

class Test():
  def __init__(self):
    self.field='Default'

  def myMethod(self, parameter=self.field):
    pass

I'm getting an error like "name 'self' is not defined"; it seems I
cannot access self.field in the method definition. Is there a
work-around (or maybe just the proper way to do it)?

Thank you very much,
Daniele


More information about the Tutor mailing list