Python naming common mistakes

I always feel that I have studied for a long time and pythonwill not make common mistakes again. However, just a few days ago, I was severely beaten in the face. A naming error cost me 20 minutes. Here are some common naming errors.
1. pythonKeyword used in variable name

a = 3
b = 4
sum = 5
print(sum(a + b))
"""
TypeError: 'int' object is not callable
"""

We see here that we have sumassigned a value 5, and sumwe know that pythonit BIF(build-in-functions)is a keyword in the middle, which sumitself is a function that can be called, but because we assigned a value to it 5, the sumcompiler at this time is considered to be a variable stored Integer type 5, and the value we want to call the sumsum function to calculate at this time a+bwill make the compiler very confused, giving a type error, and the integer type cannot be called. In other words, our sumassignment operation shields the sumfunction of the summation function itself.
This problem can be solved very well if we use classes, the code is as follows:

class Debug:
    def __init__(self):
        self.sum = 5
        self.list = [2, 3]
        
    def mainProgram(self):
        print(sum(self.list, self.sum))


if __name__ == "__main__":
    main = Debug()
    main.mainProgram()

We can see that sumand listare pythonthe keywords in, but it will not affect our sumcall to the function itself, because we will define sumand listvariables as Debugattributes of this class, and the compiler can distinguish the two at this time. But in order to develop a good habit, this method is still not recommended, self.sum1or it self._summay be better to use.
2. Variable names start with a number

a = [3]
b = 4
3Q = sum(a, b)
print(3Q)
"""
SyntaxError: invalid syntax
"""

We see a prompt syntax error, variable names cannot start with a number
3. Script .pyfiles are not recommended to start with a number.
This is because the main program, that is, the current program, we can name it with a number at the beginning. For example, we can Named 2.py. But if we want to import this file named starting with a number in another program, an error will be reported.

import 2.py
"""
SyntaxError: invalid syntax
"""

We can see that the compiler will prompt a syntax error. Therefore, naming script files starting with a number should be prohibited and not recommended.
4. The script file name conflicts with the installed module name.
We know that users can publish the package by themselves. If we name our file as an installed module, the compiler will be confused when we import the installed module Which file should be used, it is very likely that the file with the same name defined by us is referenced, and the installed module cannot be imported normally. It's like a real monkey king, with exactly the same length and the same name, so there is no way to distinguish it.
5. The class name conflicts with the variable name.
This is also the last time I wasted 20 minutes. I couldn't find the problem at first. The code is as follows:

class image3:
    def __init__(self, image3):
        self.image3 = image3
        
    def mainProgram(self):
        print(self.image3)


if __name__ == "__main__":
    image3 = [2, 3]
    main = image3(image3)
    main.mainProgram()
"""
 'list' object is not callable
"""

We can see here I will name a class and a variable name reused, it will generate an error, the error here is very clear, because I if __name__ == "__main__":define a list image3, then this image3will block out the class name image3, so that when When I want to initialize a class image3object, tell me that the list object cannot be called. Of course you may find it ridiculous, but when the code grows up and the parameter call is not so direct, the error is not so image3easy to find out. When the code is running, when I call it, the program will be directed to a stranger Location, sometimes there is no error message. Since my original code is relatively long, I won't send it out here, but when we define the class, we must avoid conflicts with the variable names we want to define.
If you find it useful, please raise your hand to give a like and let me recommend it for more people to see~

Guess you like

Origin blog.csdn.net/u011699626/article/details/108577517