Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between F(x) and F x in Python

Tags:

python

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
>>> 
like image 337
alinsoar Avatar asked Jan 15 '23 11:01

alinsoar


1 Answers

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 :)*

like image 70
RocketDonkey Avatar answered Jan 22 '23 08:01

RocketDonkey