id' is a bad variable name in Python
id()
is a fundamental built-in:
Help on built-in function
id
in module
__builtin__
:id(...)
id(object) -> integer
Return the identity of an object. This is guaranteed to be unique among
simultaneously existing objects. (Hint: it's the object's memory
address.)
In general, using variable names that eclipse a keyword or built-in function in any language is a bad idea, even if it is allowed.
Is `id` a keyword in python?
id
is not a keyword in Python, but it is the name of a built-in function.
The keywords are:
and del from not while
as elif global or with
assert else if pass yield
break except import print
class exec in raise
continue finally is return
def for lambda try
Keywords are invalid variable names. The following would be a syntax error:
if = 1
On the other hand, built-in functions like id
or type
or str
can be shadowed:
str = "hello" # don't do this
Is it a bad practice to name a instance variable after a built-in
I would say it's ok.
Grepping through Python's standard library you can see plenty of places where attributes are named the same as built-ins:
For example:
:~/cpython/Lib$ egrep -R "\.set[^a-zA-Z0-9_]" | wc -l
583
:~/cpython/Lib$ egrep -R "\.type[^a-zA-Z0-9_]" | wc -l
319
Is it bad practice to use a built-in function name as an attribute or method identifier?
It won't confuse the interpreter but it may confuse people reading your code. Unnecessary use of builtin names for attributes and methods should be avoided.
Another ill-effect is that shadowing builtins confuses syntax highlighters in most python-aware editors (vi, emacs, pydev, idle, etc.) Also, some of the lint tools will warn about this practice.
ID, id, or Id?
I do what I feel like. In general, your best practice is to err in favor of readability vs. compliance with some abstract standard.
Just be consistent.
Is giving an argument the same name as the kwarg parameter bad practice in Python?
It's unlikely to be considered bad practice, given that it is used more often than not in the major python packages; see e.g. a random pandas example here (takes a while to jump to the right line).
As for readability I would instead consider it beneficial to have identical names in both scopes whenever you are referring to the same thing. If appropriate, you can still use more specific names to refer to more specific concepts outside the function, such as the production environment file (e.g. dotenv_prod_path = r"./../.env.prod"; load_dotenv(dotenv_path=dotenv_prod_path)
).
If you are concerned about reduced readability as it might be unclear which scope the name belongs to, I would suggest to keep methods/functions short and not to use global and nonlocal variables.
Related Topics
Detecting When a Child Process Is Waiting for Input
How to "Test" Nonetype in Python
Error: Command 'Gcc' Failed with Exit Status 1 While Installing Eventlet
Combine Several Images Horizontally with Python
Combine Two Pandas Data Frames (Join on a Common Column)
Which Key/Value Store Is the Most Promising/Stable
Financial Charts/Graphs in Ruby or Python
How to Write to a CSV Line by Line
Python and Regular Expression with Unicode
Importing Variables from Another File
How to Limit Memory Usage Within a Python Process
How to Print a String at a Fixed Width
Is Shared Readonly Data Copied to Different Processes for Multiprocessing