Take the 2-minute tour ×

Is it possible to make a user-defined Python function act like a statement? In other words, I'd like to be able to say:


rather than:


and have it get called anyway -- the way that, say, print would.

I can already hear you all composing responses about how this is a horrible thing to do and I'm stupid for asking it and why do I want to do this and I really should do something else instead, but please take my word that it's something I need to do to debug a problem I'm having and it's not going to be checked in or used for, like, an air traffic control system.

share|improve this question
I'm curious, what situation could possibly require you to do that? –  David Z Jan 28 '09 at 21:46
Why don't you show us, what you have to debug, and we'll show you how to debug without creating statements. –  hop Jan 28 '09 at 21:46
Python only has 20 or so statements (like print) are you trying to replace one of those 20 statements as part of debugging? If so, which one. What specific problem do you have? –  S.Lott Jan 28 '09 at 22:02
I'm also very curious about this. I don't think statements like that are an abomination or anything; I just can't think of any situation where myfunc() wouldn't do exactly the same thing. –  DNS Jan 28 '09 at 22:08
can you fix the question. All user-defined functions behave like built-in functions. You appear to want a function to behave like a statement. –  S.Lott Jan 29 '09 at 1:39

7 Answers 7

No, it is not possible.

As you can see from the Language Reference, there is no room left for extensions of the list of simple statements in the specification.

Moreover, print as a statement no longer exists in Python 3.0 and is replaced by the print() builtin function.

share|improve this answer

If what you're looking for is to add a new statement (like print) to Python's language, then this would not be easy. You'd probably have to modify lexer, parser and then recompile Python's C sources. A lot of work to do for a questionable convenience.

share|improve this answer

I would not implement this, but if I was implementing this, I would give code with myfunc a special extension, write an import hook to parse the file, add the parenthesis to make it valid Python, and feed that into the interpreter.

share|improve this answer
Which means you'd make a preprocessor. Nice idea +1. He won't be able to use it interactively during debugging though, if that's what the requirements are. Well you could hook all compile()/exec() etc. too maybe... –  Jürgen Strobel Oct 21 '11 at 10:52

Not if you want to pass in arguments. You could do something build an object that ABUSES the __str__ method, but it is highly not recommended. You can also use other operators like overload the << operator like cout does in C++.

share|improve this answer
Sounds good. How would that work? –  mike Jan 28 '09 at 21:56
Wouldn't it work in the interpreter only? –  DzinX Jan 28 '09 at 21:58
@Mike, look at creating an object and overriding the lshift method. This is the method that corrosponds to the << operator. It takes in one argument which is the element being "shifted" into it. @DzinX, no it would work anywhere. –  Evan Fosmark Jan 28 '09 at 22:04
How about abusing the str method? Would that only work with "print myfunc" as opposed to just "myfunc" on its own? –  mike Jan 28 '09 at 22:17
wouldn't even work in the interactive interpreter –  hop Jan 28 '09 at 23:12

In Python 2.x print is not a function it is a statement just as if, while and def are statements.

share|improve this answer

Not possible in a planned way, or without a lot of work.

If you are bold and adventurous, read this wikipedia article about meta circular evaluation. Python has pretty good inspection and reflection on its own compiler/evaluater objects, you may be able to cobble something together along these lines.

"""Meta-circular implementations are suited to extending the language they are written in. They are also useful for writing tools that are tightly integrated with the programming language, such as sophisticated debuggers. A language designed with a meta-circular implementation in mind is often more suited for building languages in general, even ones completely different from the host language."""

I believe pypy is doing something similarily, you might want to look into it.

share|improve this answer

This probably isn't going to cover your problem, but I'll mention it anyway. If myfunc is part of a module, and you are using it like this:

from mymodule import myfunc
myfunc # I want this to turn into a function call

Then you could instead do this:

import mymodule
mymodule.myfunc # I want this to turn into a function call

You could then remove myfunc from mymodule and overload the module so it calls a particular function each time the myfunc member is requested.

share|improve this answer

Your Answer


