Python built-in constants

Source: Internet
Author: User
Tags define function

Introduction

Python has a few built-in constants, only 6, True, False, None, notimplemented, ellipsis, __debug__.

one. True

1. True is a constant of type bool used to represent truth.

>>> truetrue>>> type (True)<class'bool' >

2. Any assignment to the constant true will throw a syntax error.

>>> True = 1syntaxerror:can't assign to keyword

two. False

1. False is a constant of type bool used to represent false values.

>>> falsefalse>>> type (False)<class'bool' >

2. Any assignment to the constant false will throw a syntax error.

>>> False = 0syntaxerror:can't assign to keyword

three. None

1. None means none, it is the unique value of Nonetype.

>>> none  # means none, no content output >>> type (None)<class' Nonetype '>

2. Any assignment to the constant none will throw a syntax error.

>>> None = 2syntaxerror:can't assign to keyword

3. For a function, if there is no return statement, it is equivalent to returning none.

def SayHello ():  # define function    print('Hello')     >>> sayHello () Hello>>> result = SayHello () Hello>>> result>>> type (Result)<class'nonetype' >

four. notimplemented

1. notimplemented is a constant of type notimplementedtype.

>>> notimplementednotimplemented>>> type (notimplemented)<class ' Notimplementedtype '>

2. Testing with the bool () function reveals that notimplemented is a truth value.

>>> bool (notimplemented) True

3. notimplemented is not a constant in absolute sense, because he can be assigned but does not throw a grammatical error, and we should not assign a value to it, otherwise it will affect the execution result of the program.

>>> bool (notimplemented) True>>> notimplemented = False>>> >>> bool (notimplemented) False

4. notimplemented is used for some binary special methods (such as __eq__, __lt__, etc.) as the return value, indicating that there is no implementation method, and Python will be intelligent when the results return notimplemented to exchange two parameters for another attempt.

>>>classA (object):def __init__(self,name,value): Self.name=name Self.value=valuedef __eq__(self,other):Print('Self :', Self.name,self.value)Print('Other :', Other.name,other.value)returnSelf.value = = Other.value#determines whether the value of 2 objects is equal>>> A1 = A ('Tom', 1)>>> A2 = A ('Jay', 1)>>> A1 = =A2self:tom1Other:jay1True
>>>classA (object):def __init__(self,name,value): Self.name=name Self.value=valuedef __eq__(self,other):Print('Self :', Self.name,self.value)Print('Other :', Other.name,other.value)returnnotimplemented>>> A1 = A ('Tom', 1)>>> A2 = A ('Jay', 1)>>> A1 = =A2self:tom1Other:jay1Self:jay1Other:tom1False

When you execute A1==A2 (that is, call __eq__ (A1,A2)) and return notimplemented, Python automatically swaps the parameters again to invoke __eq__ (A2,A1).

Five. Ellipsis

1. Ellipsis is a constant of type ellipsis, and it is ... is equivalent.

>>> ellipsisellipsis>>> type (ellipsis)<class'  Ellipsis'>>>> ... Ellipsis>>> ... = = Ellipsistrue

2. Testing with the bool () function reveals that ellipsis is a truth value.

>>> bool (ellipsis) True

3. Ellipsis is not a constant in absolute sense, because he can be assigned but does not throw a grammatical error, and we should not assign a value to it, otherwise it will affect the execution result of the program.

>>> bool (ellipsis) True>>> ellipsis = false>>> bool (ellipsis) False 

4. Ellipsis is used to represent the data structure of a loop.

>>> a = [1,2,3,4]>>> a.append (a)>>> a[1, 2, 3, 4, [...] >>> a[1, 2, 3, 4, [...] >>> Len (a)5>>> a[4[1, 2, 3, 4, [...]] >>>

Six. __debug__

1. __debug__ is a constant of type bool.

__debug__ True>>> type (__debug__)<class'bool' >

2. Any assignment to the constant __debug__ will throw a syntax error.

__debug__ = falsesyntaxerror:assignment to keyword

3. If Python is not started with the-o option, this constant is true, otherwise it is a false value.

Python built-in constants

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.