How to define a function with an empty body?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Sep 13 19:54:02 EDT 2009


On 2009年9月13日 15:30:52 +0200, Christian Heimes wrote:
> Peng Yu schrieb:
>> Hi,
>>>> I want to define a function without anything in it body. In C++, I can
>> do something like the following because I can use "{}" to denote an
>> empty function body. Since python use indentation, I am not sure how to
>> do it. Can somebody let me know how to do it in python?
>> Python has two possibilities to create an empty function. The others
> already told you aber the pass statement. The other way is a function
> with a doc string
>> def func():
> """No op function
> """

There are more than just two possibilities. Here are three trivial 
variations on the same technique:
def func():
 return
def func():
 return None
lambda: None
and two slightly more complex variations:
new.function(compile("", "", "exec"), {})
# given some existing function f:
type(f)(compile("", "", "exec"), {})
-- 
Steven


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /