Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to define a function with an empty body?

9,721 views
Skip to first unread message

Peng Yu

unread,
Sep 12, 2009, 11:37:01 PM9/12/09
to pytho...@python.org
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?

void f() {
}

Regards,
Peng

Chris Rebert

unread,
Sep 12, 2009, 11:38:47 PM9/12/09
to Peng Yu, pytho...@python.org

Use the no-op `pass` keyword:

def f():
pass

Cheers,
Chris
--
http://blog.rebertia.com

Mel

unread,
Sep 12, 2009, 11:49:02 PM9/12/09
to
Peng Yu wrote:

Syntactically, there has to be something in the function definition. It can
be a `pass` statement, or a doc string, though:


Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
... "empty function"
...
>>> f()
>>> print f()
None
>>> def g():
...
File "<stdin>", line 2

^
IndentationError: expected an indented block
>>> def h():
... pass
...
>>> print h()
None
>>>

Cheers, Mel.

Hendrik van Rooyen

unread,
Sep 13, 2009, 6:16:37 AM9/13/09
to pytho...@python.org
On Sunday 13 September 2009 05:37:01 Peng Yu wrote:
> 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?

def rubbish_do_nothing():
pass

- Hendrik

Christian Heimes

unread,
Sep 13, 2009, 9:30:52 AM9/13/09
to pytho...@python.org
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
"""

Albert Hopkins

unread,
Sep 13, 2009, 10:04:13 AM9/13/09
to pytho...@python.org
On Sat, 2009-09-12 at 22:37 -0500, Peng Yu wrote:
> 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?
>
> void f() {
> }
>

Surprised no one has mentioned this yet, but since it's a function, and
in python all functions return values (whether explicitly or implicitly,
a simple "return" works and, to me, does much more to inform the reader
that "this function does nothing".

def f():
return


David

unread,
Sep 13, 2009, 11:35:38 AM9/13/09
to
Il Sat, 12 Sep 2009 22:37:01 -0500, Peng Yu ha scritto:

> Hi,
>
> I want to define a function without anything in it body.

[...]

I usually define void functions as:

def myfunction():
raise NotImplementedError("You forgot to implement me!")


so I keep safe from leaving orphaned functions all over my code.

Steven D'Aprano

unread,
Sep 13, 2009, 7:54:02 PM9/13/09
to


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

0 new messages