As you know, Python is popular as a language with a low threshold even for programming beginners because of its ease of writing, and there are many people who say that "Python is the first language they come into contact with." However, there are anti-patterns (forbidden hands) that Python should not do, and if a beginner steps on it without knowing it, it will be difficult to deal with it. Therefore, I would like to introduce some of the Python anti-patterns that beginners should pay particular attention to ** "Variable Naming" **, which is especially fatal ** Assignment by variable definition with the same name **.
The code below converts the contents of the tuple variable
tuple to a list type and combines it with the list type variable
At first glance, it's a normal data operation, but it doesn't actually work. I get an error.
>>> list = ["Apple", "Orange", "Grape"] >>> tuple = ("Peach", "Strawberry") >>> list.append(list(tuple))
Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'list' object is not callable
The cause is on the first line.
Overall, Python has a standard built-in function called
list () that allows you to convert an Iterable object given as an argument to a list type.
The contents of ʻappend ()
on the 3rd linelist (tuple)
is trying to call this function, but on the 1st line I defined a list variable with the name **list
. As a result, the reference destination of list
was overwritten. ** ** So ** in this code,list
will now refer to the variables ["Apple", "Orange", "Grape"]
instead of the built-in function list () `**.
list () function can never be used again in this code unless it is reassigned.
There are other names that are also strongly deprecated to use, such as
bool, which are attractive to use as temporary variables. However, if you actually define it, it is dangerous because you will destroy the function with the same name.
Never use these names, even if they are abandoned code.
So my personal recommendation is to ** put these as part of the variable name as a suffix **.
In the above example,
list = ["Apple", "Orange", "Grape"] tuple = ("Peach", "Strawberry")
fruits_list = ["Apple", "Orange", "Grape"] fruits_tuple = ("Peach", "Strawberry")
It is recommended to name ** and
[generic name of elements in the list] _ [data type] ** because you can easily distinguish the content and type of the contained data.
The example given this time is about a function called
list (), but ** Python has the characteristic that any variable other than reserved words can be freely defined and assigned **, and if you are not careful, you can overwrite variables or Variables should be ** "named like no other" ** as much as possible, as you will lose access to the function.
You should be aware of conflicts not only with standard built-in functions, but also with methods that exist in your code or in imported packages.
If you have an editor that has a code suggestion function, it may be effective to ** do not use if candidates with the same name are listed in the suggestion when trying to define a new variable ** (the image below is a suggestion of Visual Studio Code). ..
[generic name of elements in list] _ [data type]as a safe and readable example.
** Paying more attention to variable names will make them much more readable and easier to follow the entire code process. This will increase the probability of early detection of bugs and can be expected to greatly improve productivity. ** **
Review the variable naming as you write your code.
Thank you for reading to the end m (_ _) m I would appreciate it if you could let me know if there are any corrections or additions.