Python error messages are specific and easy to understand "ga" (... AAA yyy BBB)

Purpose

I don't have enough experience to compare with anything, ** Python error messages are concrete and easy to understand ** I feel that. Therefore, the error is not only a quick glance, To read the contents seriously I feel that it is a shortcut for error resolution.

This is an article to show such feelings.

... AAA yyy BBB The supplementary ** ... AAA yyy BBB ** means that the order of the elements that cause the error is displayed seriously. I wrote it in the sense that it will be done.

As I will show later, simply

TypeError: unsupported operand type(s) for *: 'generator' and 'float'

It refers to the display like (the latter half).

Example error message

The code shown here is for issuing an error message. It's not code that can make exactly the same mistakes. I repeat. This is the code to issue an error message. (However, I've seen the same error in a slightly more confusing code.)

Error 1

This is the case where you get a type error ** TypeError **.

The code is below.

gen_erro1.py


def my_range(first=0, last=10, step=1):
    number = first
    while number < last:
        yield number
        number += step
ranger = my_range(1, 5)
ranger2 = ranger * 2.0 #<--Incomprehensible code to give an error
for x in ranger:
    print(x)

The error display is as follows.

C:\_temp_work\error1>python gen_error1.py
Traceback (most recent call last):
  File "gen_error1.py", line 7, in <module>
    ranger2 = ranger * 2.0
TypeError: unsupported operand type(s) for *: 'generator' and 'float'

... AAA yyy BBB In that sense To'generator'and'float' I tried to make * work You can see that it is not supported **. ** ** For a little more complicated code, **'generator' When 'float' With the information that they are calling in this order ** You'll probably notice in a short amount of time what's wrong (such as what's disappointing).

Error 2

This is the case where you get a type error ** TypeError **.

The code is below.

gen_error2.py


def my_range(first=0, last=10, step=1):
    number = first
    while number < last:
        yield number
        number += step
ranger = my_range(1, 5)
ranger2 = 2.0 * ranger #<--Incomprehensible code to give an error
for x in ranger:
    print(x)

The content of the error is as follows.

C:\_temp_work\error1>python gen_error2.py
Traceback (most recent call last):
  File "gen_error2.py", line 7, in <module>
    ranger2 = 2.0 * ranger
TypeError: unsupported operand type(s) for *: 'float' and 'generator'

C:\_temp_work\error1>

... AAA yyy BBB In that sense To'float'and'generator' I tried to make * work You can see that it is not supported **. ** ** **'float' When 'generator' With the information that they are calling in this order ** You'll probably notice in a short amount of time what's wrong (such as what's disappointing).

Summary

I think it's a natural result in terms of composing the error message, but I felt it was easy to understand. .. ..

Related (person)

Things that are not directly related

Use python without stress! (Become familiar with generator. It seems to be since1975.)

Use python without stress! (In Python, everything is implemented as an object) Use python without stress! (Close to Pylint) Use python without stress! (Expression and Statement) Learn Python carefully using both English and Japanese.

from now on

If you have any comments, please let us know. : candy: Will study,,,,

Recommended Posts

Python error messages are specific and easy to understand "ga" (... AAA yyy BBB)
Python error messages are specific and easy to understand "ga" (before that, a colon (:) and a semicolon (;))
Python error messages are concrete and easy to understand "ga" (SyntaxError on the closing side of triple "" "comments)
Python error messages are concrete and easy to understand "ga" (IndexError: list index out of range with element [0])
Regular expressions that are easy and solid to learn in Python
Python list comprehensions that are easy to forget
Send messages to Skype and Chatwork in Python
Allow vim quickfix to jump to Python error messages
It's not easy to write Python, it's easy to write numpy and scipy
Easy to use Nifty Cloud API with botocore and python
Tips for coding short and easy to read in Python
Five useful Python data types that are easy to forget
Introduction to Effectiveness Verification Chapters 4 and 5 are written in Python
PyArmor ~ Easy way to encrypt and deliver python source code ~