Skip to content Skip to sidebar Skip to footer

Why Does Del (x) With Parentheses Around The Variable Name Work?

Why does this piece of code work the way it does? x = 3 print(dir()) #output indicates that x is defined in the global scope del (x) print(dir()) #output indicates that x is no

Solution 1:

The definition of the del statement is:

del_stmt ::=  "del" target_list

and from the definition of target_list:

target_list ::=  target ("," target)* [","]
target      ::=  identifier
                 | "(" target_list ")"
                 | "[" [target_list] "]"
                 | ...

you can see that parentheses around the list of targets are allowed.

For example, if you define x,y = 1,2, all of these are allowed and have the same effect:

del x,y
del (x,y)
del (x),[y]del[x,(y)]del ([x], (y))

Post a Comment for "Why Does Del (x) With Parentheses Around The Variable Name Work?"