Init a table of functions
Andrew Dalke
adalke at mindspring.com
Sat Oct 9 23:33:25 EDT 2004
Paulo da Silva wrote:
> class foo:
> def f1():
> print "f1"
> f1=staticmethod(f1)
...
> K={"f1" : foo.f1, \
> "f1" : foo.f2 \
> }
The name 'foo' doesn't exist as a global variable
until after the class definition has been fully
executed. To make what you want work do
class foo:
def f1():
print "f1"
f1=staticmethod(f1)
def f2():
print "f2"
f2 = staticmethod(f2)
K={"f1" : f1,
"f2" : f2,
}
This works because the class definition creates
its own scope, and 'f1' and 'f2' are valid variables
in that scope.
NOTE: I removed the unneeded "\" characters for
stylistic reasons. If there's a "{" (or "(" or "[")
then the interpreter doesn't assume that a newline
means the end of the statement until after the
closing character.
The only time it's really needed is for the print
statement. In almost every other case you should
use some sort of parenthesis, brackets, etc. For
example, instead of
a = this_is_a_very_long_variable_name + \
and_here_is_another_long_name
you should use
a = (this_is_a_very_long_variable_name +
and_here_is_another_long_name)
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list