简体   繁体   中英

Python: Is there any difference between “del a” and “del(a)”?

As far as I can tell, both del a and del(a) seems to work with the same effect. If that's the case, why would Python allow del to exist both as a statement and a function?

del is always a statement. Using parenthesis doesn't mean you're making a function call, but you're grouping expressions. (1) is just the same as 1 .

Thanks to @Juhana 's comment and @cdonts 's answer. Apparently parentheses here do not represent a function call. Here's another example:

In [40]: a=b=c=1
In [41]: del[a,(b,c)]
In [42]: b
Traceback (most recent call last):
  File "<ipython-input-42-60b725f10c9c>", line 1, in <module>
    b
NameError: name 'b' is not defined

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM