Python judges that it is empty

In actual work, we will inevitably have to deal with null values. I believe many beginners will write the following code:

ifaisNone:
dosomething.else:
dotheotherthing.

This looks good, but it will actually cause problems. Generally speaking, Python treats the following situations as null values:

None
False
0,0.0,0 L
'',(),[],{}

The special feature of None is that it is neither the value 0 nor the null value of a certain data structure, and it is itself a null value object. Its type is NoneType, it follows the single

Example mode, that is, all None in the same namespace are essentially the same null value object.

id(None)1795884240
 None==0
False
 None==''
False
 a=None
 id(a)1795884240
 a==None
True

The above judgment obviously does not meet our expectations: only when a is displayed as None, a==None is True.

So, what should we do for the more generalized None value judgment in Python?

 a=''#Here is only an empty string as an example, other empty values are also applicable
 ifa:...print'aisnotempty'...else:...print'aisaemptystring''aisaemptystring.'

It can be seen that the judgment method of if a has reached the result we want, so what kind of process is the judgment method of if a?

If a will first call nonzero() of a to determine whether a is empty, and return True/False. If an object does not define nonzero(), then call its len()

Make a judgment (here the return value is 0 represents empty), if an object does not define the above two methods, the result of if a will always be True

Next, verify the above statement:

classA(object):...def__nonzero__(self):...print'runningonthe__nonzero__'...returnTrue
 classB(object):...def__len__(self):...print'runningonthe__len__'...returnFalse
 a,b=A(),B()
 ifa:...print'Yep'...else:...print'Nop'
runningonthe__nonzero__
Yep
 ifb:...print'Yep'...else:...print'Nop'
runningonthe__len__
Nop

Content expansion:

How to judge whether the python function returns empty

I don't know what bai you mean here is None or ``

I will tell you all about:

None is an empty object, which means there is nothing.

And "is a string object, representing an empty string

If the return value is None, you use if None: judgment

If it returns ``, you use if len('') == 0: judge

Netizens share:

You can directly bai put the function after the if as du as the condition, if it is empty, the judgment result dao is false, for example:

deftest():
returnNone

iftest():
printTrue
else:
printFalse

So far, this article on the sharing of examples of python judgment is empty is introduced. For more related content on how python judges it is empty, please search the previous articles of ZaLou.Cn or continue to browse related articles below. Hope you will support more in the future. ZaLou.Cn!

Recommended Posts

Python judges that it is empty
Python 3.9 is here!
Where is the IDE that comes with Python
Why python is popular
Python is short-crawling music
Is python an interpreted language
Python is short-world epidemic map
Python is short _SVM test
Is python code case sensitive
What is introspection in python
What is object-oriented in python
What is Python variable scope