how to create a def that has the behaviour like a built-in keyword...

Steve Holden sholden at holdenweb.com
Fri Oct 5 16:15:16 EDT 2001


"BA" <asdffdsa at bellatlantic.net> wrote in message
news:CAmv7.1117$iL5.54641 at typhoon1.gnilink.net...
> hilo2u,
> thanks for reading this,
>
> i would like to create a function that would behave like the print
> statement.
> for example,
> if i were to exectute
>     print 10
> then
>     i would get a result.
>
> if i write my own def called printit,
> for example,
> def printit(Num):
>     print Num
>
> to call my function it using parenthesis like so,
>     printit(10), would be legal
> but the following seems to be illegal
>     printit 10
> why, oh why, is it illegal and is there a trick to make it legal.
> can i call a function without any parenthesis( ).
>
No. And don't expect this to change. There are many reasons, and for some I
can do no better that to quote one Guido van Rossum:

"""For example, it would render x[y] ambiguous: is it a subscription of
list/table x with index/key y, or is it a call of function x with argument
[y] (a list containing one element, y)? Also, function calls without
arguments would be impossible -- just "f" doesn't call the function f but
yields the function object f."""

In other words, Python's syntax is too precise to allow such usages. I
believe there are languages in which almost any random string has a meaning
but Python isn't one of them :-)

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list