Shorthand for defining Python functions that are the same?
If I have many variables I want to initialize with the same value, I use:
a = b = c = ""
Is there a similar shorthand for defining ma开发者_运维知识库ny functions that do the same thing? That is, neither of the following works:
def x() = y() = z():
def x() = def y() = def z():
You can simply assign functions like you would assign variables.
def z():
whatever
x = y = z
You can't do it in one line if you want to use def
, but if you can make it into a lambda
you can make a one-liner:
x = y = z = lambda foo: bar()
How about:
def z():
pass
x = y = z
Uhm... functions are also objects, so you can do this:
def foo():
pass
bar = foo
As you can see, the syntax is the same.
Why would you WANT to, hell if I know.
you can do soemthing like this:
def X():
pass
Y = Z = X
精彩评论