Classes and keywords

Bjorn Pettersen bjorn at roguewave.com
Mon Sep 18 15:24:31 EDT 2000


The problem you're seeing is that self (in the set method) is not
defined until you get to the method _body_. A possible workaround is:

  def set(self, w=None):
      if w is None:
          w = self.k
      return w * w

-- bjorn

Michael Husmann wrote:
> 
> The following code is refused by python and I wonder why:
> 
> import sys
> 
> class Foo:
>     def __init__(self):
>         self.k = 0
> 
>     def set(self, w=self.k):
>         return w * w
> 
> def main():
>     k = Foo()
>     print k.set(9)
> 
> if __name__ == "__main__": main()
> 
> Traceback (most recent call last):
>   File "<stdin>", line 11, in ?
>   File "<stdin>", line 15, in Foo
> NameError: There is no variable named 'self'
> 
> Is there someone who can help me using such a keyword argument like I
> have done in that 'set' function.
> --
> http://www.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list