Classes and keywords

Rainer Deyke root at rainerdeyke.com
Mon Sep 18 16:04:03 EDT 2000


"Michael Husmann" <Michael.Husmann at t-online.de> wrote in message
news:39C66B1F.8EC5F9B4 at t-online.de...
> Thank you Bjorn.
>
> I have found that too. I assumed that the constructor function
> (__init__) would have made that variable public for the class.
> I think this kind of workaround is not very elegant, but I fear there is
> no other way.

You misunderstand.  Default arguments are evaluated when they are
encountered in the function definition.  For example, try the following:

def f():
  print 'In function f.'
  return 5

def g(i = f()):
  print 'In function g.'

print 'Calling function g with default argument.'
g()


This should print 'In function f' *before* it prints 'Calling function g
with default argument.'.  f is called exactly once, no matter how often g is
called.  So when you have something like this...

class C:
  def f(self, w=self.k):
    return w*w

...the program will try to evaluate self.k before any instance of class C is
created.


--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games           -           http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor




More information about the Python-list mailing list