Python built-in type (1) -- true value test, python built-in

Source: Internet
Author: User

Python built-in type (1) -- true value test, python built-in

 

Any object in python can directly test the true/false value. It is used for conditional judgment of if or while statements and can also be used as the operands of Boolean logical operators.

Any object in python can directly test the true/false value without extra type conversion.

This is different from other languages. For example, C # is a non-bool object. If you want to make a logical judgment, you must first forcibly convert the type or call the object's own judgment method for determination.

var message = " ";if (message.IsNullOrEmpty()){    ...}

In python, you can directly perform logical judgment on objects without type conversion.

message = " "if message :    pass

The result of the True/False value test of the object is either True or False.

Boolean values are only True and False. Therefore, no matter what type the object is, the test result of its True value must be one of True and False. In python, you can call the built-in function bool to confirm the test result of the real value.

>>> bool(' ')True>>> bool('')False
The following built-in objects are treated as False values.
  • None
    >>> bool(None)False
  • False
    >>> bool(False)False
  • Zero of any numeric type, for example, 0, 0.0, 0j
    >>> bool(0),bool(0.0),bool(0j)(False, False, False)
  • Any empty sequence, such as, ", (), []
    >>> bool(''),bool(()),bool([])(False, False, False)
  • Any blank ing, for example ,{}
    >>> bool({})False
    All built-in objects except the preceding values are true values.
Constants NotImplemented, Ellipsis, and True are True values.
>>> bool(NotImplemented)True>>> bool(Ellipsis)True>>> bool(True)True
The real value of a user-defined class depends on whether the class is defined. __bool__()Or __len__()And the values returned by the two methods.

If the custom class is not defined__bool__()And__len__()Method, the True value of the Instance Object of this class is always True.

>>> class A:    pass>>> a =  A()>>> bool(a)True

If the custom class only defines__bool__()Method, the real value test result of the Instance Object of this class is__bool__()Result returned by the method

>>> class B():    def __init__(self,name):        self.name = name    def __bool__(self):        return bool(self.name)>>> b1 = B('')>>> bool(b1)False>>> b2 = B('jay')>>> bool(b2)True

If the custom class only defines__len__()Method, the real value test result of the Instance Object of this class is__len__()Whether the result returned by the method is an integer 0

>>> class C():    def __init__(self,name):        self.name = name    def __len__(self):        return len(self.name)>>> c1 = C('')>>> bool(c1)False>>> c2 = C('jay')>>> bool(c2)True

If the custom class is defined at the same time__bool__()And__len__()Method, the real value test result of the Instance Object of this class is__bool__()The result returned by the method, that is__bool__()Method priority is higher__len__()Method.

>>> class D():    def __len__(self):        return 1    def __bool__(self):        return False>>> d = D()>>> bool(d)False>>> class E():    def __len__(self):        return 0    def __bool__(self):        return True>>> e = E()>>> bool(e)True

Welcome to public account discussion:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.