overriding built-ins

Alex Martelli aleax at aleax.it
Sun Sep 28 22:44:13 CEST 2003


dan wrote:

> I really have two related questions.
> 
> 1) Is it possible (without recompiling and changing the core Python
> behavior) to write functions that utilize the same syntax as built-in
> functions?

Yes, but don't confuse functions with statements.  It seems you
are systematically doing just that.

>  IE, can I create a function that does this:
> 
>>>>printCaps "hello"          #note no parentheses
> HELLO

No.  You are thinking of "imitating" print, which is a STATEMENT,
*NOT* a built-in function.

Examples of built-in functions are input, hex, max.  What they have
in common, which sharply distinguishes them from statements: they
use perfectly normal function syntax, and their names are not in any
way reserved.

> 2) Is it possible to do this with the built-in keywords themselves, ie
> override functions such as print?

print is a statement, not a function.  No, there is no way to use
any Python keyword except as Python uses it.


Alex





More information about the Python-list mailing list