I was addicted to writing a recursive function, so how to deal with it.
Error that occurred
TypeError: 'NoneType' object is not iterable
In addition to the above, it is also useful for causing and dealing with all errors including "None Tpye".
Other error examples
・ Can only concatenate list(not "NoneType") to list
・ Can't multiply sequence by non-int of type 'NoneType'
・ Unsupported operand type(s) for +: 'NoneType' and 'list'
Such,,,
** ① The execution result of the function without return is "Value None, Type None Type". ** **
** ② When assigning a function to a variable, the function must have a return. ** ** └ Not print. (Because it is not the execution result but one process in the middle of execution) └ return should be the final output of the function. └ It doesn't mean that you only need one return somewhere in the function.
Occurs when trying to extract an int from a multidimensional array.
error
arr0 = [1,[2],[[3]]]
def number(arr):
result=[]
if isinstance(arr, int):
result.append(arr)
if isinstance(arr, list):
#Extract elements one by one
for brr in arr:
res = number(brr)
result += res #← This is an error
return result
number(arr0)
#output
TypeError: 'NoneType' object is not iterable
** ▼ A little more detailed ** The function number () is assigned to the variable with "res = number (brr)", but since there is no return value in this function itself, None is stored in the variable and the type is NoneType.
"Result + = res" Because I tried to combine this NoneType variable res with the list type variable result.
It is said that res cannot be combined because it is not iterable.
An error that occurs when no default arguments are passed to the function. The value is None only.
What is (python official) None The only value of type NoneType. None is often used to indicate the absence of a value, such as when no default argument is passed to the function. The assignment to None is invalid and throws a SyntaxError.
** ▼ It looks like this ** When the execution result of the function is empty, the result itself becomes NoneType.
Therefore, if you assign a function whose execution result is empty to a variable, it becomes NoneType.
NoneTpye example
def hello():
pass
x = hello()
print(type(x))
print(x)
#output
<class 'NoneType'>
None
None Type for print
def hello():
print("Hello")
x = hello()
print(type(x))
#output
Hello
<class 'NoneType'>
It is output is "Hello", but one that is displayed in the execution process of the function, but does not replace the function itself.
python
def hello():
return ("Hello")
x = hello()
print(type(x))
print(x)
#output
<class 'str'>
Hello
x is replaced with the string "Hello".
It doesn't mean that you only need one or more returns in a function. It is important that a return is returned as the final result of the execution.
▼ An example where return is described but becomes None Type
Example of becoming None Type
def hello(n):
if n > 2:
return ("Hello")
x = hello(1)
print(type(x))
print(x)
#output
<class 'NoneType'>
None
Since 1 <2, the output result of the function is empty. (Does not go through the return process)
python
def hello(n):
if n > 2:
return ("Hello")
x = hello(3)
print(type(x))
print(x)
#output
<class 'str'>
Hello
function
def hello():
print("Hello")
print(hello)
#output
<function hello at 0x00000222AE142160>
Assign function
def hello():
return ("Hello")
x = hello #← No parentheses required
print(type(x))
print(x)
#output
<class 'function'>
<function hello at 0x00000222AE1423A0>
python
def hello(n):
if n > 2:
return ("Hello")
x= hello
print(x(3))
#output
Hello
hello () = x ().
** Set return appropriately so that the value assigned to the variable does not become a function. ** **
python
def number(arr):
result=[]
if isinstance(arr, int):
result.append(arr)
if isinstance(arr, list):
#Extract elements one by one
for brr in arr:
res = number(brr)
result += res
return result #← Set result as the return value of function number
It worked safely.
Recommended Posts