Why does del (x) with parentheses around the variable name work?

2024/10/13 5:17:11

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 not defined in the global scope

My understanding is that del is a keyword in Python, and what follows del should be a name. (name) is not a name. Why does the example seem to show that del (name) works the same as del name?

Answer

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))
https://en.xdnf.cn/q/69572.html

Related Q&A

How to concisely represent if/else to specify CSS classes in Django templates

In a Django template, Id like to add CSS classes to a DIV based on certain "conditions", for example:<div class="pkg-buildinfo {% if v.release.version == pkg.b.release.version %}activ…

LabelEncoder: How to keep a dictionary that shows original and converted variable

When using LabelEncoder to encode categorical variables into numerics, how does one keep a dictionary in which the transformation is tracked?i.e. a dictionary in which I can see which values became wh…

How to find hidden files inside image files (Jpg/Gif/Png) [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, argum…

How to open a simple image using streams in Pillow-Python

from PIL import Imageimage = Image.open("image.jpg")file_path = io.BytesIO();image.save(file_path,JPEG);image2 = Image.open(file_path.getvalue());I get this error TypeError: embedded NUL char…

SyntaxError: Non-UTF-8 code starting with \x82 [duplicate]

This question already has answers here:"SyntaxError: Non-ASCII character ..." or "SyntaxError: Non-UTF-8 code starting with ..." trying to use non-ASCII text in a Python script(7 an…

How to identify the CPU core ID of a process on Python multiprocessing?

I am testing Pythons multiprocessing module on a cluster with SLURM. I want to make absolutely sure that each of my tasks are actually running on separate cpu cores as I intend. Due to the many possibi…

Finding highest values in each row in a data frame for python

Id like to find the highest values in each row and return the column header for the value in python. For example, Id like to find the top two in each row:df = A B C D 5 9 8 2 4 …

Using pytest_addoptions in a non-root conftest.py

I have a project that has the following structure: Project/ | +-- src/ | | | +-- proj/ | | | +-- __init__.py | +-- code.py | +-- tests/ | | | +-- __init_…

How to count distinct values in a combination of columns while grouping by in pandas?

I have a pandas data frame. I want to group it by using one combination of columns and count distinct values of another combination of columns.For example I have the following data frame:a b c …

Set Environmental Variables in Python with Popen

I want to set an environmental variable in linux terminal through a python script. I seem to be able to set environmental variables when using os.environ[BLASTDB] = /path/to/directory .However I was in…