In-depth understanding of built-in constants in Python

黄舟
Release: 2017-05-21 14:05:18
Original
1533 people have browsed it

This article mainly introduces to you the relevant information about the built-in constants in Python. The introduction in the article is very detailed and has certain reference and learning value for everyone. Friends who need it Let’s take a look together below.

Preface

We all know that Python does not have many built-in constants, only 6, namely True, False, None, NotImplemented, Ellipsis, debug. Let’s take a look at the detailed introduction:

1. True

1. True is a bool type used to represent true values constant.

>>> True
True
>>> type(True)
Copy after login

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

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

2. False

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

>>> False
False
>>> type(False)
Copy after login

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

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

3. None

1. None means none, which is the only value of NoneType.

>>> None #表示无,没有内容输出
>>> type(None)
Copy after login

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

>>> None = 2
SyntaxError: can't assign to keyword
Copy after login

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

>>> def sayHello(): #定义函数
 print('Hello')

 
>>> sayHello()
Hello
>>> result = sayHello()
Hello
>>> result
>>> type(result)
Copy after login

4. NotImplemented

1. NotImplemented is a constant of type NotImplementedType.

>>> NotImplemented
NotImplemented
>>> type(NotImplemented)
Copy after login

2. Use the bool() function to test and you can find that NotImplemented is a true value.

>>> bool(NotImplemented)
True
Copy after login

3. NotImplemented is not a constant in the absolute sense, because it can be assigned without throwing a syntax error, and we should not assign it, otherwise it will affect the execution results of the program.

>>> bool(NotImplemented)
True
>>> NotImplemented = False
>>> 
>>> bool(NotImplemented)
False
Copy after login

4. NotImplemented is mostly used as the return value in some binary special methods (such as eq, lt, etc.), indicating that the method is not implemented, and Python will smartly exchange the two parameters when the result returns NotImplemented. Another try.

>>> 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 #判断2个对象的value值是否相等

>>> a1 = A('Tom',1)
>>> a2 = A('Jay',1)
>>> a1 == a2
self: Tom 1
other: Jay 1
True
Copy after login
>>> 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 == a2
self: Tom 1
other: Jay 1
self: Jay 1
other: Tom 1
False
Copy after login

When executing a1==a2 (that is, calling eq(a1,a2)) and returning NotImplemented, Python will automatically exchange parameters and call eq(a2,a1) again.

5. Ellipsis

1. Ellipsis is a constant of ellipsis type, which is equivalent to...

>>> Ellipsis
Ellipsis
>>> type(Ellipsis)

>>> ...
Ellipsis
>>> ... == Ellipsis
True
Copy after login

2. Use the bool() function to test and you can find that Ellipsis is a true value.

>>> bool(Ellipsis)
True
Copy after login

3. Ellipsis is not a constant in the absolute sense, because it can be assigned a value without throwing a syntax error. We should not assign a value to it, otherwise it will affect the execution results of the program.

>>> bool(Ellipsis)
True
>>> Ellipsis = False
>>> bool(Ellipsis)
False
Copy after login

4. Ellipsis is mostly used to represent loop data structures.

>>> a = [1,2,3,4]
>>> a.append(a)
>>> a
[1, 2, 3, 4, [...]]
>>> a
[1, 2, 3, 4, [...]]
>>> len(a)
>>> a[4]
[1, 2, 3, 4, [...]]
>>>
Copy after login

6. debug

1. debug is a bool type constant.

>>> debug
True
>>> type(debug)
Copy after login

2. Any assignment operation to constant debug will throw a syntax error.

>>> debug = False
SyntaxError: assignment to keyword
Copy after login

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

Summarize

The above is the detailed content of In-depth understanding of built-in constants in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!