4

In Python it is possible to call either del x or del (x) . I know how to define a function called F(x) , but I do not know how to define a function that cal be called like del, without a tuple as parameters.

What is the difference between F x and F(x), and how can I define a function that can be called without parenthesis ?

>>> a = 10
>>> a
10
>>> del a             <------------ can be called without parenthesis
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> a = 1
>>> del (a)
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> def f(x): 1
... 
>>> f (10)
>>> print f (10)
None
>>> def f(x): return 1
... 
>>> print f (10)
1
>>> f 1                  <------   cannot be called so
  File "<stdin>", line 1
    f 1
      ^
SyntaxError: invalid syntax
>>> 
alinsoar
  • 15,386
  • 4
  • 57
  • 74
  • Consider the following statement, and what it implies about `del`: Function invocation is *always* of the form [`f(..)`](http://docs.python.org/2/reference/expressions.html#calls) in Python. –  Jan 17 '13 at 02:20
  • And how can I define a statement? This is my main misunderstanding in this problem. – alinsoar Jan 17 '13 at 02:24
  • The Python language provides no provision for defining custom statements. The statements that are provided - i.e. `del` - are all reserved words and use rules that are part of the Python grammar specification. –  Jan 17 '13 at 02:25

1 Answers1

9

The main reason is that del is actually a statement and therefore has special behavior in Python. Therefore you cannot actually define these (and this behavior) yourself* - it is a built-in part of the language for a set of reserved keywords.

**I guess you could potentially edit the source of Python itself and build your own in, but I don't think that is what you're after :)*

RocketDonkey
  • 36,383
  • 7
  • 80
  • 84
  • thanks. My question included how can I define a statement either. How can I define a function -- say F , and be able to call it using the syntax `F x`. – alinsoar Jan 17 '13 at 01:58
  • 2
    @alinsoar -- You can't. `statements` are built into the language and can't be created (or overridden). – mgilson Jan 17 '13 at 02:03
  • @alinsoar Sorry, computer went down. I'll update the answer, but as mgilson says, you can't actually do that - it is a built-in feature of the language. – RocketDonkey Jan 17 '13 at 02:04
  • @pst I am probably wording it in a misleading way - basically was trying to convey that it is one of a set of 'special' words that have behavior predefined by Python itself (the more I type the more I realize I am not very good at explaining myself :) ). – RocketDonkey Jan 17 '13 at 02:09
  • After your answers, I think this post is the answer to my question : http://stackoverflow.com/questions/214881/can-you-add-new-statements-to-pythons-syntax – alinsoar Jan 17 '13 at 02:26
  • @alinsoar That first link was a great read, thanks for posting. – RocketDonkey Jan 17 '13 at 04:42