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

Rich Harkins rharkins at thinkronize.com
Fri Oct 5 22:11:43 CEST 2001


The difference is that print is a Python statement, not a function in the
Python sense.  Python treats print, while, for, if, et. al. as special
reserved words.  You may have noticed also that "print=5" and "def
print(x):" will produce a most spectacular syntax error.

In Python *ALL* functions require parenthesis.  The behaviour you are
demonstrating would be possible in Perl (and I think Ruby), but Python
doesn't treat the language syntax and functions as interchangable.  Not
being able to create functions to masquerade as statements is occasionally
inconvenient but far more often than not is a good thing IMHO.

Hope this helps!
Rich

> -----Original Message-----
> From: python-list-admin at python.org
> [mailto:python-list-admin at python.org]On Behalf Of BA
> Sent: Friday, October 05, 2001 2:02 PM
> To: python-list at python.org
> Subject: how to create a def that has the behaviour like a built-in
> keyword...
>
>
> 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( ).
>
> thanks,
> dave
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>





More information about the Python-list mailing list