Dumb python questions

Andrew Dalke dalke at acm.org
Thu Aug 16 11:01:51 EDT 2001


Alex Martelli wrote:
>A warning would *DEFINITELY* be needed if default arguments
>*WEREN'T* evaluated as part of the def statement -- it would be
>absolutely astonishing to me (and to anybody else coming from
>C++, I guess); what language, if any, does this kind of 'lazy
>*repeated* evaluation'...?

[dalke at pw600a ~]$ cat spam.C
#include <iostream.h>
class X {
public:
  X(void) { cout << "Here I am." << endl; }
};
X default_x = X();

void f(X x = X()) {
  cout << "Called f" << endl;
}
void g(X x = default_x) {
  cout << "Called g" << endl;
}
main() {
  f();
  f();
  g();
  g();
}
[dalke at pw600a ~]$ g++ spam.C
[dalke at pw600a ~]$ ./a.out
Here I am.    <--- this is the default_x
Here I am.    <--- this is the first call to f()
Called f
Here I am.    <--- this is the second call to f()
Called f
Called g
Called g
[dalke at pw600a ~]$

                    Andrew







More information about the Python-list mailing list