overriding built-ins

John Roth newsgroups at jhrothjr.com
Sun Sep 28 16:42:21 EDT 2003


"dan" <danbmil99 at yahoo.com> wrote in message
news:fbf8d8f2.0309281120.564ec9d5 at posting.google.com...
> 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?  IE, can I create a function that does this:
>
> >>>printCaps "hello"          #note no parentheses
> HELLO

There is no special syntax for built-in functions.
If you really meant your example the way you wrote it,
that's a statement, not a function, in which case the answer
is a categorical no.

Built-in functions are simply functions that are in a special
dictionary that Python searches after not finding the requested
function anywhere else. If you want to replace one, feel free.
Then don't feel slighted when nobody else wants to deal with
your program... [grin.]

The Python developers are considering making at least some
built-ins immutable and not overridable, though. That is, not changable at
run time.
The reason is performance - a large number of built-ins are
simply calls to magic methods, so there's a performance gain
to avoiding the search and extra method invocation required
to go through the built-in dictionary for functions like len().

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

No.

John Roth
>
> thanks in advance -






More information about the Python-list mailing list