I touched on Past a little, but I found an article that explains it in a more understandable way, so I also included the meaning of review. Reprinted from the blog of Great Senior, aka all credit belongs to our legendary cs alumni
Traceback message
Traceback (most recent call last):
File "<pyshell#29>", line 3 in <module>
result = buggy(5)
File <pyshell#29>", line 5 in buggy
return f + x
TypeError: unsupported operand type(s) for +: 'function' and 'int'
The traceback message prints the error in chronological order. The bottom is the newest. There are two traceback messages in a set. The first File" <pyshell # 29> ", line 3 in <module>
contains the name of the file containing the error, where (the line) the error is, and the error. The three function names that exist are represented in the format File" <file name> ", line <number>, in <function>
. The second result = buggy (5)
remains as it is, but the actual code is displayed.
Error messages
The error message is written on the last line. error message <error type>: <error message>
is described in the error type (syntaxerror, typeerror etc) and error details format.
Debugging
def foo(x):
"""A random function.
>>> foo(4)
4
>>> foo(5)
5
"""
From the terminal python3 -m doctest file.py
You can use python3 -m doctest file.py -v
to tell you which test passed and which got stuck.
As a rule, the first line specifically describes what the function does, leaving a blank line. Detailed explanation is here.
If the target of doctest
is single statement (= a single line that doesn't end in a:
), use the doctest
function called "run_docstring_examples". This function has three arguments. The first is the name of the function you want to test, the second is global ()
(= a built-in function that returns the global environment), and the third is True
(to indicate that we would like" verbose "output: a catalog of all tests run.). By the way, testing a single statement is called unit test.
Exhaustive unit testing is a hallmark of good program design. --_ Good program design is guaranteed by a comprehensive unit test. _
Besides that, you can create your own test function or create a new doctest.
Always run these doctests. Submitting an assignment without running doctests even once is practically throwing points away.
Are you serious. I didn't know that (laughs)
def foo(x):
result = some_function(x)
return result // 5
If you don't know where the error is even if you write the code, try print
the result for the time being.
def foo(x):
result = some_function(x)
print('result is', result)
return other_function(result)
And if possible, don't forget to add a word instead of just print
print(tmp) # harder to keep track
print('tmp was this:', tmp) # easier
It is also effective to put it after the while loop
i = 0
while i < n:
i += func(i)
print('i is', i)
Don't forget to remove the print
when the test is over
Long-term debugging
When it comes to project units, sometimes it is better not to debug instantly. In such a case, it is convenient to do as follows
debug = True
def foo(n):
i = 0
while i < n:
i += func(i)
if debug:
print('i is', i)
If you don't like the results being displayed every time, change it to false
. Variables that use this way are called flag
.
Types of error
Syntax Error Cause:
--There is a problem with the code
File "file name", line number
def incorrect(f)
^
SyntaxError: invalid syntax
^
indicates code that contains the wrong syntax. It doesn't show what's wrong, but it tells you where the cause lies. Python first checks for this syntax error
before running the code.
Indentation Error Cause:
--There is a problem with the code line indentation
File "file name", line number
print('improper indentation')
IndentationError: unindent does not match any outer indentation level
It shows where the indentation is done improperly. Make a clear distinction between space and tab functionality.
TypeError
Cause:
--When the operands (max, mul etc ..) and primitive operators (numbers, etc.) are not used properly.
TypeError: unsupported operand type(s) for +: 'function' and 'int'
--Functions and objects are messed up.
>>> square = 3
>>> square(3)
Traceback (most recent call last):
...
TypeError: 'int' object is not callable`
--There is a problem with the number of arguments.
>>> add(3)
Traceback (most recent call last):
...
TypeError: add expected 2 arguments, got 1
NameError
Cause: --Using undefined variables or functions
File "file name", line number
y = x + 3
NameError: global name 'x' is not defined
notes: The reason for the global name
here is that Python will eventually enter the global area from the local area of the function containing the error.
IndexError
Cause: --The limit of the number of sequences (tuple, list, string) that can be passed is exceeded.
File "file name", line number
x[100]
IndexError: tuple index out of range
simple spelling mistakes
hello
≠ Hello
≠ HELLO
. When you see NameError
, there may be a mistake in spelling. Or you may have defined a variable or function with the wrong spelling.
Missing Parentheses Forgetting to put the parentheses, etc.
def fun():
return foo(bar() # missing a parenthesis here
fun()
In this case, python calls it SyntaxError
and points to the line after the line with the parenthesis problem.
Missing close quotes It's easy to find because python will tell you specifically.
File "file name", line "number"
return 'hi
^
SyntaxError: EOL while scanning string literal
EOL = "end of line"
= (assignment) vs. == (equality test)
=
is used when passing variables, and ==
is used to check if variables are equal. Typical error is
if x = 3: # when should be "==" instead
Infinite Loops
The while loop keeps spinning until the condition is achieved, so it's easy to fall into it.
i = 0
while i < 10:
print(i)
i, n = 0, 0
while i < 10:
print(i)
n += 1
Recommended Posts