Python built-in constants and python Constants

Source: Internet
Author: User

Python built-in constants and python Constants

Introduction

There are not many built-in constants in Python. There are only six constants: True, False, None, NotImplemented, Ellipsis, and _ debug __.

1. True

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

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

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

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

 

Ii. False

1. False is a constant of the bool type used to represent a False value.

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

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

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

 

Iii. None

1. None indicates None, which is the unique value of NoneType.

>>> None # indicates 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. If a function does not have a return statement, it is equivalent to returning None.

>>> Def sayHello (): # define the function print ('hello') >>> sayHello () Hello >>> result = sayHello () hello >>> result >>> type (result) <class 'nonetype '>

 

Iv. NotImplemented

 

1. NotImplemented is a constant of the NotImplementedType.

>>> NotImplementedNotImplemented>>> type(NotImplemented)<class 'NotImplementedType'>

2. Using the bool () function for testing, we can find that NotImplemented is a true value.

>>> bool(NotImplemented)True

3. NotImplemented is not a constant in the absolute sense, because it can be assigned but does not throw a syntax error. 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 often used in some binary special methods (such as _ eq _, _ lt _) as return values, indicating that there is no implementation method, however, when Python returns NotImplemented, it intelligently exchanges two parameters for another attempt.

>>> Class A (object): def _ init _ (self, name, value): self. name = name self. value = value def _ eq _ (self, other): print ('self: ', self. name, self. value) print ('other: ', other. name, other. value) return self. value = other. value # determine whether the values of the two objects are equal >>> a1 = A ('Tom ', 1) >>> a2 = A ('Jay', 1) >>> a1 = a2self: Tom 1 other: Jay 1 True
>>> class A(object):    def __init__(self,name,value):        self.name = name        self.value = value    def __eq__(self,other):        print('self:',self.name,self.value)        print('other:',other.name,other.value)        return NotImplemented>>> a1 = A('Tom',1)>>> a2 = A('Jay',1)>>> a1 == a2self: Tom 1other: Jay 1self: Jay 1other: Tom 1False

When a1 = a2 (call _ eq _ (a1, a2) and return NotImplemented, Python will automatically call _ eq _ (a2, a1 ).

 

V. Ellipsis

 

1. Ellipsis is a constant of the ellipsis type... Is equivalent.

>>> EllipsisEllipsis>>> type(Ellipsis)<class 'ellipsis'>>>> ...Ellipsis>>> ... == EllipsisTrue

2. Using the bool () function for testing, we can find that Ellipsis is a true value.

>>> bool(Ellipsis)True

3. Ellipsis is not a constant in the absolute sense, because it can be assigned, but it will not throw a syntax error. 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 mostly used to represent the cyclic data structure.

>>> 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, [...]]>>>

 

6. _ debug __

 

1. _ debug _ is a constant of the bool type.

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

2. A syntax error is thrown when any value assignment operation is performed on constant _ debug.

>>> __debug__ = FalseSyntaxError: assignment to keyword

3. If Python is not started using the-O option, this constant is the true value; otherwise, it is a false value.

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.