Hướng dẫn where are python built in functions stored? - python được xây dựng trong các chức năng được lưu trữ ở đâu?

Đã hỏi 5 năm, 1 tháng trước 5 years, 1 month ago

Đã xem 2k lần 2k times

3

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Giống như chúng ta có các mô-đun Python trong thư viện tiêu chuẩn mà chúng ta có thể nhập các phương thức và sử dụng chúng, cũng có một mô-đun trong đó tất cả các hàm tích hợp được xác định? Nếu có, làm thế nào tôi có thể xem mô -đun đó?

Đã hỏi ngày 1 tháng 9 năm 2017 lúc 16:36Sep 1, 2017 at 16:36

Hướng dẫn where are python built in functions stored? - python được xây dựng trong các chức năng được lưu trữ ở đâu?

6

Mô -đun

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
4 (Python 3) hoặc
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
5 (Python 2) cung cấp quyền truy cập vào chúng.

Điều này thậm chí còn hữu ích đôi khi nếu bạn viết lại tên của một bản tích hợp, ví dụ

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
6. Bạn thường không nên làm điều đó, nhưng nếu bạn làm bạn vẫn có thể truy cập vào hàm tạo danh sách tích hợp là
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
7.

Đã trả lời ngày 1 tháng 9 năm 2017 lúc 16:39Sep 1, 2017 at 16:39

Peter DeGlopperPeter DeglopperPeter DeGlopper

354K7 Huy hiệu vàng89 Huy hiệu bạc83 Huy hiệu đồng7 gold badges89 silver badges83 bronze badges

2

Trên Python3,

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
8 hoặc
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
9 cho các phiên bản cũ hơn

Bạn có thể kiểm tra bất kỳ nội dung mô -đun nào với hàm

class C:
    @classmethod
    def f(cls, arg1, arg2): ...
0

Đã trả lời ngày 1 tháng 9 năm 2017 lúc 16:38Sep 1, 2017 at 16:38

JbernardojbernardoJBernardo

31.2K10 Huy hiệu vàng87 Huy hiệu bạc112 Huy hiệu đồng10 gold badges87 silver badges112 bronze badges

Giống như chúng ta có các mô-đun Python trong thư viện tiêu chuẩn mà chúng ta có thể nhập các phương thức và sử dụng chúng, cũng có một mô-đun trong đó tất cả các hàm tích hợp được xác định? Nếu có, làm thế nào tôi có thể xem mô -đun đó?

một cái gì đó như thế này? in (Dir (__ xây dựng __)) in (trợ giúp (module_name))

Hướng dẫn where are python built in functions stored? - python được xây dựng trong các chức năng được lưu trữ ở đâu?

Renuka Chhalotre Nhập nhập Math PRIN (Math .__ File__) Điều này sẽ cung cấp cho bạn đường dẫn nhị phân mô -đun, sau đó tìm kiếm các tệp python trong thư mục cha để xác định vị trí các mô -đun

Hướng dẫn where are python built in functions stored? - python được xây dựng trong các chức năng được lưu trữ ở đâu?

Trình thông dịch Python có một số chức năng và loại được tích hợp trong nó luôn có sẵn. Chúng được liệt kê ở đây theo thứ tự bảng chữ cái.

Chức năng tích hợp sẵn

Một

class C:
    @classmethod
    def f(cls, arg1, arg2): ...
1

class C:
    @classmethod
    def f(cls, arg1, arg2): ...
2

class C:
    @classmethod
    def f(cls, arg1, arg2): ...
3

class C:
    @classmethod
    def f(cls, arg1, arg2): ...
4

class C:
    @classmethod
    def f(cls, arg1, arg2): ...
5

class C:
    @classmethod
    def f(cls, arg1, arg2): ...
6

B

class C:
    @classmethod
    def f(cls, arg1, arg2): ...
7

class C:
    @classmethod
    def f(cls, arg1, arg2): ...
8

class C:
    @classmethod
    def f(cls, arg1, arg2): ...
9

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
0

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
1

C

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
2

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
3

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
4

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
5

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
6

D

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
7

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
8

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
9

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
0

E

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
1

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
2

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
3

F

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
4

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
5

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
6

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
7

G

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
8

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
9

H

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
0

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
1

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
2

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
3

Tôi

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
4

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
5

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
6

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
7

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
8

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
9

L

>>> x = 1
>>> eval('x+1')
2
0

>>> x = 1
>>> eval('x+1')
2
1

>>> x = 1
>>> eval('x+1')
2
2

M

>>> x = 1
>>> eval('x+1')
2
3

>>> x = 1
>>> eval('x+1')
2
4

>>> x = 1
>>> eval('x+1')
2
5

>>> x = 1
>>> eval('x+1')
2
6

N

>>> x = 1
>>> eval('x+1')
2
7

O

>>> x = 1
>>> eval('x+1')
2
8

>>> x = 1
>>> eval('x+1')
2
9

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
0

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
1

P

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
2

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
3

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
4

R

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
5

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
6

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
7

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
8

S

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
9

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
00

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
01

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
02

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
03

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
04

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
05

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
06

T

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
07

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
08

V

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
09

Z

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
10

_

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
11

________ 112 (x)(x)

Trả về giá trị tuyệt đối của một số. Đối số có thể là một số nguyên, số điểm nổi hoặc một đối tượng thực hiện

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
13. Nếu đối số là một số phức, độ lớn của nó được trả về.

________ 114 (async_iterable) ¶(async_iterable)

Trả về một iterator không đồng bộ cho một điều không thể đồng bộ. Tương đương với việc gọi

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
15.asynchronous iterator for an asynchronous iterable. Equivalent to calling
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
15.

Lưu ý: Không giống như

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
9,
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
2 không có biến thể 2 đối số.

Mới trong phiên bản 3.10.

________ 118 (có thể lặp lại) ¶(iterable)

Trả về

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
19 Nếu tất cả các yếu tố của IT có thể là đúng (hoặc nếu có thể trống). Tương đương với:

def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True

đang chờ ________ 120 (async_iterator [, mặc định]) ¶(async_iterator[, default])

Khi được chờ đợi, hãy trả lại mục tiếp theo từ trình lặp không đồng bộ đã cho hoặc mặc định nếu được đưa ra và trình lặp bị cạn kiệt.asynchronous iterator, or default if given and the iterator is exhausted.

Đây là biến thể không đồng bộ của

>>> x = 1
>>> eval('x+1')
2
7 tích hợp và hoạt động tương tự.

Điều này gọi phương thức

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
22 của async_iterator, trả về một sự chờ đợi. Đang chờ điều này trả về giá trị tiếp theo của trình lặp. Nếu mặc định được đưa ra, nó sẽ được trả về nếu trình lặp bị cạn kiệt, nếu không
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
23 được nâng lên.awaitable. Awaiting this returns the next value of the iterator. If default is given, it is returned if the iterator is exhausted, otherwise
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
23 is raised.

Mới trong phiên bản 3.10.

________ 124 (có thể lặp lại) ¶(iterable)

Trả về

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
19 Nếu bất kỳ yếu tố nào của IT có thể là đúng. Nếu có thể trống, hãy trả lại
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
26. Tương đương với:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False

________ 127 (đối tượng) ¶(object)

Như

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
6, trả về một chuỗi chứa một biểu diễn có thể in của một đối tượng, nhưng thoát khỏi các ký tự không ASCII trong chuỗi được trả về bởi
sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
6 bằng cách sử dụng
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
30,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
31 hoặc
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
32 thoát ra. Điều này tạo ra một chuỗi tương tự như được trả về bởi
sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
6 trong Python 2.

________ 134 (x)(x)

Chuyển đổi số nguyên thành chuỗi nhị phân được đặt trước với 0b 0b. Kết quả là một biểu thức python hợp lệ. Nếu X không phải là đối tượng Python

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
35, thì nó phải xác định một phương thức
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
36 trả về một số nguyên. Vài ví dụ:

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'

Nếu tiền tố là 0b 0b, hay không, bạn có thể sử dụng một trong những cách sau.

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')

Xem thêm

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
6 để biết thêm thông tin.

Lớp ________ 138 ([x])([x])

Trả về một giá trị boolean, tức là một trong

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
19 hoặc
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
26. X được chuyển đổi bằng quy trình kiểm tra sự thật tiêu chuẩn. Nếu x là sai hoặc bị bỏ qua, điều này sẽ trả về
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
26; Nếu không, nó trả về
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
19. Lớp
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
43 là một lớp con của
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
35 (xem các loại số - int, float, phức tạp). Nó không thể được phân lớp hơn nữa. Các trường hợp duy nhất của nó là
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
26 và
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
19 (xem các giá trị Boolean).truth testing procedure. If x is false or omitted, this returns
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
26; otherwise, it returns
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
19. The
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
43 class is a subclass of
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
35 (see Numeric Types — int, float, complex). It cannot be subclassed further. Its only instances are
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
26 and
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
19 (see Boolean Values).

Đã thay đổi trong phiên bản 3.7: x hiện là tham số chỉ có vị trí.x is now a positional-only parameter.

________ 147 (*args, ** kws) ¶(*args, **kws)

Chức năng này thả bạn vào trình gỡ lỗi tại trang web cuộc gọi. Cụ thể, nó gọi

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
48, vượt qua
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
49 và
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
50 trực tiếp. Theo mặc định,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
48 gọi
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
52 mong đợi không có đối số. Trong trường hợp này, nó hoàn toàn là một hàm tiện lợi, vì vậy bạn không phải nhập rõ ràng
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
53 hoặc nhập càng nhiều mã để nhập trình gỡ lỗi. Tuy nhiên,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
48 có thể được đặt thành một số chức năng khác và
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
9 sẽ tự động gọi nó, cho phép bạn rơi vào trình gỡ lỗi lựa chọn. Nếu
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
48 không thể truy cập được, chức năng này sẽ tăng
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
57.

Tăng một sự kiện kiểm toán

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
58 với đối số
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
59.auditing event
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
58 with argument
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
59.

Mới trong phiên bản 3.7.

Lớp ________ 160 ([Nguồn [, mã hóa [, lỗi]]]))([source[, encoding[, errors]]])

Trả về một mảng byte mới. Lớp

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
61 là một chuỗi số nguyên có thể thay đổi trong phạm vi 0Mutable Sequence Types, as well as most methods that the
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
62 type has, see Bytes and Bytearray Operations.

Tham số nguồn tùy chọn có thể được sử dụng để khởi tạo mảng theo một vài cách khác nhau:

  • Nếu đó là một chuỗi, bạn cũng phải cung cấp các tham số mã hóa (và tùy chọn, lỗi);

    >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    0 sau đó chuyển đổi chuỗi thành byte bằng
    def any(iterable):
        for element in iterable:
            if element:
                return True
        return False
    
    64.

  • Nếu đó là một số nguyên, mảng sẽ có kích thước đó và sẽ được khởi tạo bằng các byte null.

  • Nếu nó là một đối tượng phù hợp với giao diện bộ đệm, bộ đệm chỉ đọc của đối tượng sẽ được sử dụng để khởi tạo mảng byte.buffer interface, a read-only buffer of the object will be used to initialize the bytes array.

  • Nếu nó là một điều khác, nó phải là một số nguyên của các số nguyên trong phạm vi

    def any(iterable):
        for element in iterable:
            if element:
                return True
        return False
    
    65, được sử dụng làm nội dung ban đầu của mảng.

Không có đối số, một mảng có kích thước 0 được tạo.

Xem thêm Các loại chuỗi nhị phân - Các đối tượng byte, bytearray, memoryView và bytearray.Binary Sequence Types — bytes, bytearray, memoryview and Bytearray Objects.

Lớp ________ 166 ([Nguồn [, mã hóa [, lỗi]]]))([source[, encoding[, errors]]])

Trả về một đối tượng mới của Byte Byte, đó là một chuỗi số nguyên bất động trong phạm vi

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
65.
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
62 là phiên bản bất biến của
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
61-nó có cùng các phương pháp không cắt và cùng một hành vi lập chỉ mục và cắt.

Theo đó, các đối số cấu trúc được giải thích là đối với

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
0.

Các đối tượng byte cũng có thể được tạo bằng chữ, xem chuỗi và byte theo nghĩa đen.String and Bytes literals.

Xem thêm Các loại chuỗi nhị phân - byte, bytearray, bộ nhớ, các đối tượng byte và các hoạt động byte và bytearray.Binary Sequence Types — bytes, bytearray, memoryview, Bytes Objects, and Bytes and Bytearray Operations.

________ 171 (đối tượng) ¶(object)

Trả về

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
19 Nếu đối số đối tượng xuất hiện có thể gọi được,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
26 nếu không. Nếu điều này trả về
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
19, vẫn có thể cuộc gọi không thành công, nhưng nếu đó là
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
26, đối tượng cuộc gọi sẽ không bao giờ thành công. Lưu ý rằng các lớp có thể gọi được (gọi một lớp trả về một thể hiện mới); Các trường hợp có thể gọi được nếu lớp của họ có phương thức
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
76.

Mới trong phiên bản 3.2: Chức năng này lần đầu tiên được xóa trong Python 3.0 và sau đó được đưa trở lại trong Python 3.2.This function was first removed in Python 3.0 and then brought back in Python 3.2.

________ 177 (i)(i)

Trả về chuỗi đại diện cho một ký tự có điểm mã unicode là số nguyên i. Ví dụ:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
78 Trả về chuỗi
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
79, trong khi
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
80 trả về chuỗi
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
81. Đây là nghịch đảo của
sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
1.

Phạm vi hợp lệ cho đối số là từ 0 đến 1.114.111 (0x10ffff trong cơ sở 16).

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
83 sẽ được nâng lên nếu tôi ở ngoài phạm vi đó.

________ 184 ________ 185¶

Chuyển đổi một phương thức thành một phương thức lớp.

Một phương thức lớp nhận được lớp như một đối số đầu tiên ngầm, giống như một phương thức thể hiện nhận được thể hiện. Để khai báo một phương thức lớp, hãy sử dụng thành ngữ này:

class C:
    @classmethod
    def f(cls, arg1, arg2): ...

Mẫu

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
86 là một trình trang trí chức năng - xem các định nghĩa chức năng để biết chi tiết.decorator – see Function definitions for details.

Một phương thức lớp có thể được gọi là trên lớp (chẳng hạn như

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
87) hoặc trên một thể hiện (chẳng hạn như
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
88). Ví dụ bị bỏ qua ngoại trừ lớp của nó. Nếu một phương thức lớp được gọi cho một lớp dẫn xuất, đối tượng lớp dẫn xuất được truyền dưới dạng đối số đầu tiên ngụ ý.

Phương pháp lớp khác với các phương thức tĩnh C ++ hoặc Java. Nếu bạn muốn những thứ đó, xem

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
03 trong phần này. Để biết thêm thông tin về các phương thức lớp, hãy xem hệ thống phân cấp loại tiêu chuẩn.The standard type hierarchy.

Đã thay đổi trong phiên bản 3.9: Các phương thức lớp hiện có thể kết thúc các mô tả khác như

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
4.Class methods can now wrap other descriptors such as
sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
4.

Đã thay đổi trong phiên bản 3.10: Các phương thức lớp hiện kế thừa các thuộc tính phương thức (

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
91,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
92,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
93,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
94 và
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
95) và có thuộc tính
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
96 mới.Class methods now inherit the method attributes (
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
91,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
92,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
93,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
94 and
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
95) and have a new
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
96 attribute.

________ 197 (Nguồn, tên tệp, chế độ, cờ = 0, dont_inherit = false, tối ưu hóa =- 1) ¶(source, filename, mode, flags=0, dont_inherit=False, optimize=- 1)

Biên dịch nguồn vào một mã hoặc đối tượng AST. Các đối tượng mã có thể được thực thi bởi

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
3 hoặc
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
2. Nguồn có thể là một chuỗi bình thường, chuỗi byte hoặc đối tượng AST. Tham khảo tài liệu mô -đun
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
00 để biết thông tin về cách làm việc với các đối tượng AST.

Đối số tên tệp sẽ đưa ra tệp mà mã được đọc; Truyền một số giá trị dễ nhận biết nếu nó không được đọc từ một tệp (

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
01 thường được sử dụng).

Đối số chế độ chỉ định loại mã phải được biên dịch; Nó có thể là

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
02 nếu nguồn bao gồm một chuỗi các câu lệnh,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
03 nếu nó bao gồm một biểu thức duy nhất hoặc
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
04 nếu nó bao gồm một câu lệnh tương tác duy nhất (trong trường hợp sau, các câu lệnh biểu thức đánh giá một cái gì đó khác với
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
05 sẽ được in ).

Các cờ đối số tùy chọn và điều khiển dont_inherit nào tùy chọn trình biên dịch nên được kích hoạt và các tính năng nào trong tương lai nên được cho phép. Nếu không có mặt (hoặc cả hai đều bằng không), mã được biên dịch với cùng các cờ ảnh hưởng đến mã đang gọi

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
5. Nếu đối số cờ được đưa ra và dont_inherit không (hoặc bằng không) thì các tùy chọn trình biên dịch và các câu lệnh trong tương lai được chỉ định bởi đối số cờ được sử dụng cùng với các tùy chọn sẽ được sử dụng. Nếu dont_inhinha là một số nguyên khác không thì đối số cờ là nó-các cờ (tính năng trong tương lai và tùy chọn trình biên dịch) trong mã xung quanh bị bỏ qua.compiler options should be activated and which future features should be allowed. If neither is present (or both are zero) the code is compiled with the same flags that affect the code that is calling
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
5. If the flags argument is given and dont_inherit is not (or is zero) then the compiler options and the future statements specified by the flags argument are used in addition to those that would be used anyway. If dont_inherit is a non-zero integer then the flags argument is it – the flags (future features and compiler options) in the surrounding code are ignored.

Tùy chọn trình biên dịch và các câu lệnh trong tương lai được chỉ định bởi các bit có thể được ghép lại với nhau để chỉ định nhiều tùy chọn. Bitfield cần thiết để chỉ định một tính năng trong tương lai đã cho có thể được tìm thấy dưới dạng thuộc tính

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
07 trên thể hiện
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
08 trong mô -đun
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
09. Cờ trình biên dịch có thể được tìm thấy trong mô -đun
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
00, với tiền tố
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
11.Compiler flags can be found in
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
00 module, with
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
11 prefix.

Đối số tối ưu hóa chỉ định mức tối ưu hóa của trình biên dịch; Giá trị mặc định của

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
12 chọn mức tối ưu hóa của trình thông dịch như được đưa ra bởi các tùy chọn
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
13. Các mức rõ ràng là
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
14 (không tối ưu hóa;
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
15 là đúng),
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
16 (xác nhận được loại bỏ,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
15 là sai) hoặc
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
18 (DocStrings cũng bị loại bỏ).

Hàm này tăng

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
19 nếu nguồn được biên dịch không hợp lệ và
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
83 nếu nguồn chứa byte null.

Nếu bạn muốn phân tích mã Python vào đại diện AST của nó, hãy xem

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
21.

Tăng một sự kiện kiểm toán

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
22 với các đối số
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
23 và
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
24. Sự kiện này cũng có thể được nêu ra bằng cách tổng hợp ngầm.auditing event
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
22 with arguments
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
23 and
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
24. This event may also be raised by implicit compilation.

Ghi chú

Khi biên dịch một chuỗi có mã đa dòng ở chế độ

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
04 hoặc
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
03, đầu vào phải được chấm dứt bởi ít nhất một ký tự dòng mới. Điều này là để tạo điều kiện phát hiện các tuyên bố không đầy đủ và hoàn chỉnh trong mô -đun
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
27.

Cảnh báo

Có thể làm hỏng trình thông dịch python với một chuỗi đủ/phức tạp đủ khi biên dịch thành một đối tượng AST do các giới hạn độ sâu xếp chồng trong trình biên dịch AST Python.

Đã thay đổi trong phiên bản 3.2: Được phép sử dụng Windows và Mac Newlines. Ngoài ra, đầu vào trong chế độ

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
02 không phải kết thúc trong một dòng mới nữa. Đã thêm tham số tối ưu hóa.Allowed use of Windows and Mac newlines. Also, input in
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
02 mode does not have to end in a newline anymore. Added the optimize parameter.

Thay đổi trong phiên bản 3.5: Trước đây,

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
29 đã được nâng lên khi gặp phải các byte null trong nguồn.Previously,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
29 was raised when null bytes were encountered in source.

Mới trong phiên bản 3.8:

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
30 hiện có thể được truyền trong các cờ để cho phép hỗ trợ cho cấp cao nhất
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
31,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
32 và
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
33.
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
30 can now be passed in flags to enable support for top-level
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
31,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
32, and
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
33.

Lớp ________ 234 ([real [, hình ảnh]]) ¶([real[, imag]])

Trả về một số phức với giá trị real + hình ảnh*1j hoặc chuyển đổi một chuỗi hoặc số thành một số phức. Nếu tham số đầu tiên là một chuỗi, nó sẽ được hiểu là một số phức và hàm phải được gọi mà không cần tham số thứ hai. Tham số thứ hai không bao giờ có thể là một chuỗi. Mỗi đối số có thể là bất kỳ loại số (bao gồm cả phức tạp). Nếu hình ảnh bị bỏ qua, nó mặc định là 0 và hàm tạo đóng vai trò chuyển đổi số như

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
35 và
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
36. Nếu cả hai đối số bị bỏ qua, trả về
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
37.

Đối với một đối tượng Python chung

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
38,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
39 đại biểu cho
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
40. Nếu
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
41 không được xác định thì nó sẽ trở lại
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
42. Nếu
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
42 không được xác định thì nó sẽ trở lại
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
36.

Ghi chú

Khi biên dịch một chuỗi có mã đa dòng ở chế độ

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
04 hoặc
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
03, đầu vào phải được chấm dứt bởi ít nhất một ký tự dòng mới. Điều này là để tạo điều kiện phát hiện các tuyên bố không đầy đủ và hoàn chỉnh trong mô -đun
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
27.

Cảnh báoNumeric Types — int, float, complex.

Có thể làm hỏng trình thông dịch python với một chuỗi đủ/phức tạp đủ khi biên dịch thành một đối tượng AST do các giới hạn độ sâu xếp chồng trong trình biên dịch AST Python.Grouping digits with underscores as in code literals is allowed.

Đã thay đổi trong phiên bản 3.2: Được phép sử dụng Windows và Mac Newlines. Ngoài ra, đầu vào trong chế độ

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
02 không phải kết thúc trong một dòng mới nữa. Đã thêm tham số tối ưu hóa.Falls back to
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
36 if
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
41 and
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
42 are not defined.

Thay đổi trong phiên bản 3.5: Trước đây,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
29 đã được nâng lên khi gặp phải các byte null trong nguồn.(object, name)

Mới trong phiên bản 3.8:

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
30 hiện có thể được truyền trong các cờ để cho phép hỗ trợ cho cấp cao nhất
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
31,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
32 và
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
33.

Lớp ________ 234 ([real [, hình ảnh]]) ¶(**kwarg) class
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
58(mapping, **kwarg) class
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
58(iterable, **kwarg)

Trả về một số phức với giá trị real + hình ảnh*1j hoặc chuyển đổi một chuỗi hoặc số thành một số phức. Nếu tham số đầu tiên là một chuỗi, nó sẽ được hiểu là một số phức và hàm phải được gọi mà không cần tham số thứ hai. Tham số thứ hai không bao giờ có thể là một chuỗi. Mỗi đối số có thể là bất kỳ loại số (bao gồm cả phức tạp). Nếu hình ảnh bị bỏ qua, nó mặc định là 0 và hàm tạo đóng vai trò chuyển đổi số như

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
35 và
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
36. Nếu cả hai đối số bị bỏ qua, trả về
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
37.Mapping Types — dict for documentation about this class.

Đối với một đối tượng Python chung

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
38,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
39 đại biểu cho
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
40. Nếu
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
41 không được xác định thì nó sẽ trở lại
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
42. Nếu
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
42 không được xác định thì nó sẽ trở lại
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
36.

Khi chuyển đổi từ một chuỗi, chuỗi không được chứa khoảng trắng xung quanh toán tử
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
45 hoặc
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
46. Ví dụ,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
47 là tốt, nhưng
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
48 tăng
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
83.([object])

Loại phức tạp được mô tả trong các loại số - int, float, phức tạp.

Đã thay đổi trong phiên bản 3.6: Nhóm các chữ số có dấu gạch dưới như trong các chữ viết được cho phép.

Đã thay đổi trong phiên bản 3.8: rơi trở lại

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
36 nếu
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
41 và
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
42 không được xác định.

Cơ chế

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
9 mặc định hoạt động khác nhau với các loại đối tượng khác nhau, vì nó cố gắng tạo ra thông tin phù hợp nhất, thay vì hoàn chỉnh:

  • Nếu đối tượng là một đối tượng mô -đun, danh sách chứa tên của các thuộc tính mô -đun.

  • Nếu đối tượng là một loại hoặc đối tượng lớp, danh sách chứa tên của các thuộc tính của nó và đệ quy các thuộc tính của các cơ sở của nó.

  • Mặt khác, danh sách chứa các thuộc tính của đối tượng, tên của các thuộc tính, tên của các thuộc tính lớp của nó và đệ quy các thuộc tính của các lớp cơ sở lớp của nó.

Danh sách kết quả được sắp xếp theo thứ tự bảng chữ cái. Ví dụ:

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']

Ghi chú

Bởi vì

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
9 được cung cấp chủ yếu như một sự tiện lợi để sử dụng tại một dấu nhắc tương tác, nên nó cố gắng cung cấp một bộ tên thú vị nhiều hơn là cố gắng cung cấp một bộ tên được xác định nghiêm ngặt hoặc nhất quán và hành vi chi tiết của nó có thể thay đổi qua các bản phát hành. Ví dụ: các thuộc tính Metaclass không nằm trong danh sách kết quả khi đối số là một lớp.

________ 277 (a, b)(a, b)

Lấy hai số (không phức tạp) làm đối số và trả về một cặp số bao gồm thương số và phần còn lại của chúng khi sử dụng phân chia số nguyên. Với các loại toán hạng hỗn hợp, các quy tắc cho các toán tử số học nhị phân áp dụng. Đối với các số nguyên, kết quả giống như

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
78. Đối với số điểm nổi, kết quả là
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
79, trong đó q thường là
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
80 nhưng có thể ít hơn 1. Trong mọi trường hợp
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
81 rất gần với A, nếu
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
82 không khác, nó có cùng dấu hiệu với B và
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
83.

________ 284 (có thể lặp lại, bắt đầu = 0) ¶(iterable, start=0)

Trả về một đối tượng liệt kê. Có thể lặp lại phải là một chuỗi, một trình lặp hoặc một số đối tượng khác hỗ trợ lặp lại. Phương thức

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
85 của trình lặp được trả về bởi
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
1 trả về một tuple chứa một số lượng (từ bắt đầu mặc định đến 0) và các giá trị thu được từ việc lặp lại hơn lặp đi lặp lại.iterator, or some other object which supports iteration. The
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
85 method of the iterator returned by
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
1 returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over iterable.

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]

Tương đương với:

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1

________ 287 (Biểu thức [, Globals [, người dân địa phương]])(expression[, globals[, locals]])

Các đối số là một chuỗi và toàn cầu và người dân địa phương tùy chọn. Nếu được cung cấp, Globals phải là một từ điển. Nếu được cung cấp, người dân địa phương có thể là bất kỳ đối tượng ánh xạ.

Đối số biểu thức được phân tích cú pháp và được đánh giá là biểu thức Python (về mặt kỹ thuật, một danh sách điều kiện) sử dụng từ điển toàn cầu và người dân địa phương là không gian tên toàn cầu và địa phương. Nếu từ điển toàn cầu có mặt và không chứa giá trị cho khóa

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
88, một tham chiếu đến từ điển của mô-đun tích hợp
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
89 được chèn dưới phím đó trước khi biểu thức được phân tích cú pháp. Bằng cách đó, bạn có thể kiểm soát những gì tích hợp có sẵn cho mã được thực thi bằng cách chèn từ điển
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
88 của riêng bạn vào các thế giới trước khi chuyển nó cho
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
2. Nếu từ điển người dân địa phương bị bỏ qua, nó mặc định là từ điển toàn cầu. Nếu cả hai từ điển bị bỏ qua, biểu thức được thực hiện với toàn cầu và người dân địa phương trong môi trường nơi
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
2 được gọi. Lưu ý, eval () không có quyền truy cập vào phạm vi lồng nhau (không phải địa phương) trong môi trường kèm theo.nested scopes (non-locals) in the enclosing environment.

Giá trị trả về là kết quả của biểu thức được đánh giá. Lỗi cú pháp được báo cáo là ngoại lệ. Thí dụ:

>>> x = 1
>>> eval('x+1')
2

Hàm này cũng có thể được sử dụng để thực thi các đối tượng mã tùy ý (chẳng hạn như các đối tượng được tạo bởi

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
5). Trong trường hợp này, chuyển một đối tượng mã thay vì một chuỗi. Nếu đối tượng mã đã được biên dịch với
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
02 là đối số chế độ, giá trị trả về của ____ 62 sẽ là
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
05.

Gợi ý: Thực hiện động của các câu lệnh được hỗ trợ bởi hàm

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
3. Các hàm
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
9 và
>>> x = 1
>>> eval('x+1')
2
2 trả về từ điển toàn cầu và địa phương hiện tại, tương ứng, có thể hữu ích để vượt qua để sử dụng bởi
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
2 hoặc
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
3.

Nếu nguồn đã cho là một chuỗi, thì các không gian và tab dẫn đầu và các tab bị loại bỏ.

Xem

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
02 để biết một hàm có thể đánh giá một cách an toàn các chuỗi với các biểu thức chỉ chứa các chữ.

Tăng một sự kiện kiểm toán

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
03 với đối tượng mã làm đối số. Các sự kiện biên dịch mã cũng có thể được nêu ra.auditing event
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
03 with the code object as the argument. Code compilation events may also be raised.

________ 304 (Object [, Globals [, người dân địa phương]])(object[, globals[, locals]])

Hàm này hỗ trợ thực thi động của mã python. Đối tượng phải là một chuỗi hoặc một đối tượng mã. Nếu đó là một chuỗi, chuỗi được phân tích cú pháp như một bộ câu lệnh Python sau đó được thực thi (trừ khi xảy ra lỗi cú pháp). 1 Nếu đó là một đối tượng mã, nó chỉ được thực thi. Trong mọi trường hợp, mã được thực thi dự kiến ​​sẽ hợp lệ dưới dạng đầu vào tệp (xem phần đầu vào tệp trong hướng dẫn tham chiếu). Xin lưu ý rằng các câu lệnh

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
05,
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
06 và
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
07 có thể không được sử dụng bên ngoài các định nghĩa chức năng ngay cả trong bối cảnh mã được chuyển đến hàm
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
3. Giá trị trả về là
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
05.File input in the Reference Manual). Be aware that the
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
05,
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
06, and
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
07 statements may not be used outside of function definitions even within the context of code passed to the
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
3 function. The return value is
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
05.

Trong mọi trường hợp, nếu các phần tùy chọn bị bỏ qua, mã được thực thi trong phạm vi hiện tại. Nếu chỉ có Globals được cung cấp, nó phải là một từ điển (và không phải là một lớp từ điển), sẽ được sử dụng cho cả các biến toàn cầu và các biến cục bộ. Nếu toàn cầu và người dân địa phương được đưa ra, chúng được sử dụng cho các biến toàn cầu và địa phương, tương ứng. Nếu được cung cấp, người dân địa phương có thể là bất kỳ đối tượng ánh xạ. Hãy nhớ rằng ở cấp độ mô -đun, Globals và người dân địa phương là cùng một từ điển. Nếu EXEC nhận được hai đối tượng riêng biệt là Globals và người dân địa phương, mã sẽ được thực thi như thể nó được nhúng trong một định nghĩa lớp.

Nếu từ điển toàn cầu không chứa giá trị cho khóa

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
88, một tham chiếu đến từ điển của mô-đun tích hợp
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
89 được chèn dưới khóa đó. Bằng cách đó, bạn có thể kiểm soát những gì tích hợp có sẵn cho mã được thực thi bằng cách chèn từ điển
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
88 của riêng bạn vào các thế giới trước khi chuyển nó đến
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
3.

Tăng một sự kiện kiểm toán

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
03 với đối tượng mã làm đối số. Các sự kiện biên dịch mã cũng có thể được nêu ra.auditing event
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
03 with the code object as the argument. Code compilation events may also be raised.

________ 304 (Object [, Globals [, người dân địa phương]])

Hàm này hỗ trợ thực thi động của mã python. Đối tượng phải là một chuỗi hoặc một đối tượng mã. Nếu đó là một chuỗi, chuỗi được phân tích cú pháp như một bộ câu lệnh Python sau đó được thực thi (trừ khi xảy ra lỗi cú pháp). 1 Nếu đó là một đối tượng mã, nó chỉ được thực thi. Trong mọi trường hợp, mã được thực thi dự kiến ​​sẽ hợp lệ dưới dạng đầu vào tệp (xem phần đầu vào tệp trong hướng dẫn tham chiếu). Xin lưu ý rằng các câu lệnh

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
05,
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
06 và
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
07 có thể không được sử dụng bên ngoài các định nghĩa chức năng ngay cả trong bối cảnh mã được chuyển đến hàm
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
3. Giá trị trả về là
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
05.

________ 304 (Object [, Globals [, người dân địa phương]])

Hàm này hỗ trợ thực thi động của mã python. Đối tượng phải là một chuỗi hoặc một đối tượng mã. Nếu đó là một chuỗi, chuỗi được phân tích cú pháp như một bộ câu lệnh Python sau đó được thực thi (trừ khi xảy ra lỗi cú pháp). 1 Nếu đó là một đối tượng mã, nó chỉ được thực thi. Trong mọi trường hợp, mã được thực thi dự kiến ​​sẽ hợp lệ dưới dạng đầu vào tệp (xem phần đầu vào tệp trong hướng dẫn tham chiếu). Xin lưu ý rằng các câu lệnh

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
05,
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
06 và
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
07 có thể không được sử dụng bên ngoài các định nghĩa chức năng ngay cả trong bối cảnh mã được chuyển đến hàm
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
3. Giá trị trả về là
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
05.

Trong mọi trường hợp, nếu các phần tùy chọn bị bỏ qua, mã được thực thi trong phạm vi hiện tại. Nếu chỉ có Globals được cung cấp, nó phải là một từ điển (và không phải là một lớp từ điển), sẽ được sử dụng cho cả các biến toàn cầu và các biến cục bộ. Nếu toàn cầu và người dân địa phương được đưa ra, chúng được sử dụng cho các biến toàn cầu và địa phương, tương ứng. Nếu được cung cấp, người dân địa phương có thể là bất kỳ đối tượng ánh xạ. Hãy nhớ rằng ở cấp độ mô -đun, Globals và người dân địa phương là cùng một từ điển. Nếu EXEC nhận được hai đối tượng riêng biệt là Globals và người dân địa phương, mã sẽ được thực thi như thể nó được nhúng trong một định nghĩa lớp.(function, iterable)

Nếu từ điển toàn cầu không chứa giá trị cho khóa

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
88, một tham chiếu đến từ điển của mô-đun tích hợp
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
89 được chèn dưới khóa đó. Bằng cách đó, bạn có thể kiểm soát những gì tích hợp có sẵn cho mã được thực thi bằng cách chèn từ điển
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
88 của riêng bạn vào các thế giới trước khi chuyển nó đến
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
3.

Ghi chú

Các chức năng tích hợp

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
9 và
>>> x = 1
>>> eval('x+1')
2
2 trả về từ điển toàn cầu và địa phương hiện tại, tương ứng, có thể hữu ích để chuyển qua để sử dụng làm đối số thứ hai và thứ ba cho
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
3.

Người dân địa phương mặc định hoạt động như được mô tả cho chức năng
>>> x = 1
>>> eval('x+1')
2
2 bên dưới: Không nên thử các sửa đổi cho từ điển địa phương mặc định. Vượt qua một từ điển người dân địa phương rõ ràng nếu bạn cần xem hiệu ứng của mã đối với người dân địa phương sau khi chức năng
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
3 trả về.([x])

________ 320 (chức năng, có thể lặp lại) ¶

Xây dựng một trình lặp từ các yếu tố của điều đó có thể trả về đúng. Có thể lặp lại có thể là một chuỗi, một thùng chứa hỗ trợ lặp lại hoặc lặp lại. Nếu hàm là

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
05, hàm nhận dạng được giả định, nghĩa là, tất cả các yếu tố của có thể được loại bỏ.

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value

Lưu ý rằng

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
22 tương đương với biểu thức máy phát
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
23 nếu hàm không phải là
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
05 và
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
25 nếu hàm là
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
05.Floating point literals. Case is not significant, so, for example, “inf”, “Inf”, “INFINITY”, and “iNfINity” are all acceptable spellings for positive infinity.

Mặt khác, nếu đối số là một số nguyên hoặc số điểm nổi, một số điểm nổi có cùng giá trị (trong độ chính xác của điểm nổi Python) được trả về. Nếu cuộc tranh luận nằm ngoài phạm vi của một chiếc phao python, một

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
33 sẽ được nâng lên.

Đối với một đối tượng Python chung

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
38,
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
35 đại biểu cho
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
36. Nếu
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
42 không được xác định thì nó sẽ trở lại
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
36.

Nếu không có đối số nào được đưa ra,

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
39 sẽ được trả về.

Examples:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
0

Loại phao được mô tả theo loại số - int, float, phức tạp.Numeric Types — int, float, complex.

Đã thay đổi trong phiên bản 3.6: Nhóm các chữ số có dấu gạch dưới như trong các chữ viết được cho phép.Grouping digits with underscores as in code literals is allowed.

Đã thay đổi trong phiên bản 3.7: x hiện là tham số chỉ có vị trí.x is now a positional-only parameter.

Đã thay đổi trong phiên bản 3.8: rơi trở lại

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
36 nếu
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
42 không được xác định.Falls back to
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
36 if
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
42 is not defined.

________ 342 (giá trị [, format_spec]) ¶(value[, format_spec])

Chuyển đổi một giá trị thành một biểu diễn được định dạng trên mạng, như được kiểm soát bởi Format_spec. Việc giải thích định dạng_spec sẽ phụ thuộc vào loại đối số giá trị; Tuy nhiên, có một cú pháp định dạng tiêu chuẩn được sử dụng bởi hầu hết các loại tích hợp: Đặc điểm kỹ thuật của ngôn ngữ nhỏ.Format Specification Mini-Language.

Định dạng mặc định_spec là một chuỗi trống thường mang lại hiệu ứng tương tự như gọi

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
43.

Một cuộc gọi đến

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
44 được dịch thành
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
45, bỏ qua từ điển thể hiện khi tìm kiếm phương thức giá trị
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
46. Ngoại lệ
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
29 được nâng lên nếu tìm kiếm phương thức đạt
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
48 và format_spec không trống hoặc nếu định dạng_spec hoặc giá trị trả về không phải là chuỗi.

Đã thay đổi trong phiên bản 3.4:

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
49 tăng
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
29 nếu Format_spec không phải là một chuỗi trống.
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
49 raises
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
29 if format_spec is not an empty string.

Lớp ________ 351 ([itable])([iterable])

Trả về một đối tượng

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
52 mới, tùy chọn với các phần tử được lấy từ ITable.
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
52 là một lớp tích hợp. Xem các loại
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
52 và SET - Đặt, Frozenset cho tài liệu về lớp này.Set Types — set, frozenset for documentation about this class.

Đối với các thùng chứa khác, hãy xem các lớp

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
64,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
63,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
65 và
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
61 tích hợp, cũng như mô-đun
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
66.

________ 360 (đối tượng, tên [, mặc định]) ¶(object, name[, default])

Trả về giá trị của thuộc tính được đặt tên của đối tượng. Tên phải là một chuỗi. Nếu chuỗi là tên của một trong các thuộc tính của đối tượng, kết quả là giá trị của thuộc tính đó. Ví dụ,

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
61 tương đương với
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
62. Nếu thuộc tính được đặt tên không tồn tại, mặc định sẽ được trả về nếu được cung cấp, nếu không
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
63 sẽ được nâng lên. Tên không cần phải là một định danh Python (xem
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
00).

Ghi chú

Vì tên riêng tư xảy ra vào thời điểm biên dịch, người ta phải tự xử lý một tên thuộc tính riêng tư (thuộc tính có hai dấu gạch dưới hàng đầu) để lấy nó bằng

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
8.private name mangling happens at compilation time, one must manually mangle a private attribute’s (attributes with two leading underscores) name in order to retrieve it with
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
8.

________ 366 ()()

Trả về từ điển thực hiện không gian tên mô -đun hiện tại. Đối với mã trong các chức năng, điều này được đặt khi hàm được xác định và vẫn giữ nguyên bất kể hàm được gọi ở đâu.

________ 367 (đối tượng, tên)(object, name)

Các đối số là một đối tượng và một chuỗi. Kết quả là

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
19 nếu chuỗi là tên của một trong các thuộc tính của đối tượng,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
26 nếu không. (Điều này được thực hiện bằng cách gọi
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
70 và xem liệu nó có tăng
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
63 hay không.)

________ 372 (đối tượng) ¶(object)

Trả về giá trị băm của đối tượng (nếu nó có một). Giá trị băm là số nguyên. Chúng được sử dụng để nhanh chóng so sánh các khóa từ điển trong quá trình tra cứu từ điển. Các giá trị số so sánh bằng nhau có cùng giá trị băm (ngay cả khi chúng thuộc các loại khác nhau, như trường hợp của 1 và 1.0).

Ghi chú

Vì tên riêng tư xảy ra vào thời điểm biên dịch, người ta phải tự xử lý một tên thuộc tính riêng tư (thuộc tính có hai dấu gạch dưới hàng đầu) để lấy nó bằng

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
8.

________ 366 ()([object])

Trả về từ điển thực hiện không gian tên mô -đun hiện tại. Đối với mã trong các chức năng, điều này được đặt khi hàm được xác định và vẫn giữ nguyên bất kể hàm được gọi ở đâu.

________ 367 (đối tượng, tên)the FAQ entry on positional-only parameters.

Các đối số là một đối tượng và một chuỗi. Kết quả là

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
19 nếu chuỗi là tên của một trong các thuộc tính của đối tượng,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
26 nếu không. (Điều này được thực hiện bằng cách gọi
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
70 và xem liệu nó có tăng
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
63 hay không.)

________ 372 (đối tượng) ¶Changes to

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
79 and
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
80 mean that the reported signatures for callables are now more comprehensive and consistent.

Trả về giá trị băm của đối tượng (nếu nó có một). Giá trị băm là số nguyên. Chúng được sử dụng để nhanh chóng so sánh các khóa từ điển trong quá trình tra cứu từ điển. Các giá trị số so sánh bằng nhau có cùng giá trị băm (ngay cả khi chúng thuộc các loại khác nhau, như trường hợp của 1 và 1.0).(x)

Đối với các đối tượng có các phương thức tùy chỉnh ____373, lưu ý rằng

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
1 cắt giảm giá trị trả về dựa trên chiều rộng bit của máy chủ. Xem
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
73 để biết chi tiết.

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
1

Nếu bạn muốn chuyển đổi một số nguyên thành chuỗi chữ viết hoa hoặc thấp hơn với tiền tố hoặc không, bạn có thể sử dụng một trong các cách sau:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
2

Xem thêm

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
6 để biết thêm thông tin.

Xem thêm

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
6 để chuyển đổi chuỗi thập lục phân thành một số nguyên bằng cách sử dụng cơ sở 16.

Ghi chú

Để có được một biểu diễn chuỗi thập lục phân cho một chiếc phao, hãy sử dụng phương pháp

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
86.

________ 387 (đối tượng) ¶(object)

Trả về danh tính của người Viking của một đối tượng. Đây là một số nguyên được đảm bảo là duy nhất và không đổi cho đối tượng này trong suốt cuộc đời của nó. Hai đối tượng có tuổi thọ không chồng chéo có thể có cùng giá trị

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
4.

Chi tiết triển khai CPython: Đây là địa chỉ của đối tượng trong bộ nhớ. This is the address of the object in memory.

Tăng một sự kiện kiểm toán

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
89 với đối số
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
90.auditing event
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
89 with argument
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
90.

________ 391 ([Nhắc]) ¶([prompt])

Nếu đối số nhanh chóng có mặt, nó được viết vào đầu ra tiêu chuẩn mà không cần một dòng mới. Hàm sau đó đọc một dòng từ đầu vào, chuyển đổi nó thành một chuỗi (tước một dòng mới) và trả về nó. Khi EOF được đọc,

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
92 được nâng lên. Thí dụ:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
3

Nếu mô -đun

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
93 được tải, thì
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
5 sẽ sử dụng nó để cung cấp các tính năng chỉnh sửa và lịch sử dòng phức tạp.

Tăng một sự kiện kiểm toán

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
95 với đối số
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
96 trước khi đọc đầu vàoauditing event
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
95 with argument
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
96 before reading input

Tăng một sự kiện kiểm toán

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
97 với kết quả sau khi đọc thành công đầu vào.auditing event
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
97 with the result after successfully reading input.

Lớp ________ 398 ([x]) ¶ Lớp ________ 398 (x, cơ sở = 10)([x])¶ class
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
98(x, base=10)

Trả về một đối tượng số nguyên được xây dựng từ một số hoặc chuỗi X hoặc trả về

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
14 nếu không có đối số nào được đưa ra. Nếu X Xác định
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
01,
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
02 trả về
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
03. Nếu X Xác định
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
36, nó sẽ trả về
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
05. Nếu X Xác định
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
06, nó sẽ trả về
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
07. Đối với số điểm nổi, điều này cắt ngắn về số 0.

Nếu x không phải là số hoặc nếu cơ sở được đưa ra, thì x phải là một chuỗi,

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
62 hoặc
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
61 đại diện cho một số nguyên theo nghĩa đen trong cơ sở radix. Tùy chọn, chữ có thể được đi trước bằng
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
45 hoặc
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
46 (không có không gian ở giữa) và được bao quanh bởi khoảng trắng. Một cơ sở-N theo nghĩa đen bao gồm các chữ số 0 đến N-1, với
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
12 đến
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
13 (hoặc
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
14 đến
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
15) có các giá trị 10 đến 35. Cơ sở mặc định là 10. Các giá trị được phép là 0 và 2 .3636. Base -2, -8 và -16 Biết chữ có thể được tiền tố tùy ý với ________ 416/________ 417, ________ 418/________ 419, hoặc ________ 420/________ 421, như với các chữ số nguyên trong mã. Cơ sở 0 có nghĩa là giải thích chính xác như một mã theo nghĩa đen, do đó cơ sở thực tế là 2, 8, 10 hoặc 16, và do đó
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
22 không hợp pháp, trong khi
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
23, cũng như
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
24.integer literal in radix base. Optionally, the literal can be preceded by
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
45 or
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
46 (with no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
12 to
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
13 (or
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
14 to
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
15) having values 10 to 35. The default base is 10. The allowed values are 0 and 2–36. Base-2, -8, and -16 literals can be optionally prefixed with
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
16/
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
17,
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
18/
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
19, or
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
20/
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
21, as with integer literals in code. Base 0 means to interpret exactly as a code literal, so that the actual base is 2, 8, 10, or 16, and so that
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
22 is not legal, while
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
23 is, as well as
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
24.

Loại số nguyên được mô tả trong các loại số - int, float, phức tạp.Numeric Types — int, float, complex.

Thay đổi trong phiên bản 3.4: Nếu cơ sở không phải là một thể hiện của

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
35 và đối tượng cơ sở có phương thức
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
26, phương pháp đó được gọi để có được số nguyên cho cơ sở. Các phiên bản trước được sử dụng
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
27 thay vì
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
26.If base is not an instance of
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
35 and the base object has a
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
26 method, that method is called to obtain an integer for the base. Previous versions used
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
27 instead of
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
26.

Đã thay đổi trong phiên bản 3.6: Nhóm các chữ số có dấu gạch dưới như trong các chữ viết được cho phép.Grouping digits with underscores as in code literals is allowed.

Đã thay đổi trong phiên bản 3.7: x hiện là tham số chỉ có vị trí.x is now a positional-only parameter.

Đã thay đổi trong phiên bản 3.8: rơi trở lại

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
36 nếu
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
01 không được xác định.Falls back to
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
36 if
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
01 is not defined.

Đã thay đổi trong phiên bản 3.10.7:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
35 Đầu vào và biểu diễn chuỗi có thể được giới hạn để giúp tránh từ chối các cuộc tấn công dịch vụ. Một
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
83 được nâng lên khi vượt quá giới hạn trong khi chuyển đổi chuỗi X thành
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
35 hoặc khi chuyển đổi
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
35 thành một chuỗi sẽ vượt quá giới hạn. Xem tài liệu giới hạn độ dài chuyển đổi chuỗi số nguyên.
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
35 string inputs and string representations can be limited to help avoid denial of service attacks. A
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
83 is raised when the limit is exceeded while converting a string x to an
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
35 or when converting an
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
35 into a string would exceed the limit. See the integer string conversion length limitation documentation.

________ 435 (đối tượng, classinfo) ¶(object, classinfo)

Trả về

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
19 Nếu đối số đối tượng là một thể hiện của đối số classinfo hoặc của một lớp con (trực tiếp, gián tiếp hoặc ảo). Nếu đối tượng không phải là một đối tượng của loại đã cho, hàm luôn trả về
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
26. Nếu classinfo là một bộ của các đối tượng loại (hoặc đệ quy, các bộ dữ liệu khác như vậy) hoặc một loại liên kết nhiều loại, hãy trả về
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
19 nếu đối tượng là một thể hiện của bất kỳ loại nào. Nếu classinfo không phải là loại hoặc tuple loại và các bộ dữ liệu như vậy, ngoại lệ
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
29 được nâng lên.virtual) subclass thereof. If object is not an object of the given type, the function always returns
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
26. If classinfo is a tuple of type objects (or recursively, other such tuples) or a Union Type of multiple types, return
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
19 if object is an instance of any of the types. If classinfo is not a type or tuple of types and such tuples, a
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
29 exception is raised.

Đã thay đổi trong phiên bản 3.10: Classinfo có thể là loại liên minh.classinfo can be a Union Type.

________ 440 (Class, Classinfo) ¶(class, classinfo)

Trả về

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
19 Nếu lớp là một lớp con (trực tiếp, gián tiếp hoặc ảo) của classinfo. Một lớp học được coi là một lớp con của chính nó. ClassInfo có thể là một bộ của các đối tượng lớp (hoặc đệ quy, các bộ dữ liệu khác như vậy) hoặc một loại liên minh, trong trường hợp đó trở lại
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
19 nếu lớp là một lớp con của bất kỳ mục nào trong classinfo. Trong mọi trường hợp khác, ngoại lệ
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
29 được nâng lên.virtual) of classinfo. A class is considered a subclass of itself. classinfo may be a tuple of class objects (or recursively, other such tuples) or a Union Type, in which case return
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
19 if class is a subclass of any entry in classinfo. In any other case, a
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
29 exception is raised.

Đã thay đổi trong phiên bản 3.10: Classinfo có thể là loại liên minh.classinfo can be a Union Type.

________ 440 (Class, Classinfo) ¶(object[, sentinel])

Trả về một đối tượng lặp. Đối số đầu tiên được giải thích rất khác nhau tùy thuộc vào sự hiện diện của đối số thứ hai. Không có đối số thứ hai, đối tượng phải là đối tượng thu thập hỗ trợ giao thức có thể lặp lại (phương thức

class C:
    @classmethod
    def f(cls, arg1, arg2): ...
45) hoặc nó phải hỗ trợ giao thức trình tự (phương thức
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
46 với các đối số số nguyên bắt đầu từ
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
14). Nếu nó không hỗ trợ một trong hai giao thức đó,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
29 sẽ được nâng lên. Nếu đối số thứ hai, Sentinel, được đưa ra, thì đối tượng phải là một đối tượng có thể gọi được. Trình lặp được tạo trong trường hợp này sẽ gọi đối tượng không có đối số cho mỗi cuộc gọi đến phương thức
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
85 của nó; Nếu giá trị được trả về bằng Sentinel,
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
50 sẽ được nâng lên, nếu không giá trị sẽ được trả về.iterator object. The first argument is interpreted very differently depending on the presence of the second argument. Without a second argument, object must be a collection object which supports the iterable protocol (the
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
45 method), or it must support the sequence protocol (the
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
46 method with integer arguments starting at
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
14). If it does not support either of those protocols,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
29 is raised. If the second argument, sentinel, is given, then object must be a callable object. The iterator created in this case will call object with no arguments for each call to its
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
85 method; if the value returned is equal to sentinel,
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
50 will be raised, otherwise the value will be returned.

Xem thêm các loại Iterator.Iterator Types.

Một ứng dụng hữu ích của dạng

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
9 thứ hai là xây dựng một trình đọc khối. Ví dụ: đọc các khối chiều rộng cố định từ tệp cơ sở dữ liệu nhị phân cho đến khi đạt được kết thúc tệp:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
4

________ 452 (s)(s)

Trả về độ dài (số lượng mục) của một đối tượng. Đối số có thể là một chuỗi (như chuỗi, byte, tuple, danh sách hoặc phạm vi) hoặc một bộ sưu tập (như từ điển, bộ hoặc bộ đông lạnh).

Lớp ________ 453 ([itable])([iterable])

Thay vì là một hàm,

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
63 thực sự là một loại trình tự có thể thay đổi, như được ghi lại trong danh sách và loại trình tự - danh sách, tuple, phạm vi.Lists and Sequence Types — list, tuple, range.

________ 455 ()()

Cập nhật và trả về một từ điển đại diện cho bảng ký hiệu cục bộ hiện tại. Các biến miễn phí được trả về bởi

>>> x = 1
>>> eval('x+1')
2
2 khi nó được gọi trong các khối chức năng, nhưng không phải trong các khối lớp. Lưu ý rằng ở cấp độ mô -đun,
>>> x = 1
>>> eval('x+1')
2
2 và
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
9 là cùng một từ điển.

Ghi chú

Nội dung của từ điển này không nên được sửa đổi; Thay đổi có thể không ảnh hưởng đến các giá trị của các biến cục bộ và các biến miễn phí được sử dụng bởi trình thông dịch.

________ 459 (chức năng, có thể lặp lại, ...) ¶(function, iterable, ...)

Trả về một trình lặp lại áp dụng chức năng cho mọi mục có thể lặp lại, mang lại kết quả. Nếu các đối số khác có thể được thông qua, chức năng phải lấy nhiều đối số đó và được áp dụng cho các mục từ tất cả các vòng lặp song song. Với nhiều lần lặp, người lặp lại dừng khi có thể cạn kiệt ngắn nhất. Đối với các trường hợp các đầu vào hàm đã được sắp xếp thành các bộ dữ liệu đối số, xem

class C:
    @classmethod
    def f(cls, arg1, arg2): ...
60.

________ 461 (itable, *[, key, mặc định])(iterable, *[, key, default])
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
61(arg1, arg2, *args[, key])

Trả lại mục lớn nhất trong một cuộc đối số có thể lặp lại hoặc lớn nhất trong hai hoặc nhiều đối số.

Nếu một đối số vị trí được cung cấp, nó phải là một điều có thể. Các mặt hàng lớn nhất trong ITBER có thể được trả lại. Nếu hai hoặc nhiều đối số vị trí được cung cấp, thì lớn nhất trong số các đối số vị trí được trả về.iterable. The largest item in the iterable is returned. If two or more positional arguments are provided, the largest of the positional arguments is returned.

Có hai đối số chỉ từ khóa tùy chọn. Đối số chính chỉ định hàm đặt hàng một đối tượng như được sử dụng cho

class C:
    @classmethod
    def f(cls, arg1, arg2): ...
63. Đối số mặc định chỉ định một đối tượng để trả về nếu ITBER được cung cấp là trống. Nếu có thể trống và mặc định không được cung cấp,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
83 sẽ được nâng lên.

Nếu nhiều mục là tối đa, chức năng trả về mục đầu tiên gặp phải. Điều này phù hợp với các công cụ bảo quản ổn định sắp xếp khác như

class C:
    @classmethod
    def f(cls, arg1, arg2): ...
65 và
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
66.

Mới trong phiên bản 3.4: Đối số chỉ từ khóa mặc định.The default keyword-only argument.

Đã thay đổi trong phiên bản 3.8: Khóa có thể là

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
05.The key can be
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
05.

Lớp ________ 468 (đối tượng)(object)

Trả về một đối tượng Chế độ xem bộ nhớ của người Viking được tạo từ đối số đã cho. Xem chế độ xem bộ nhớ để biết thêm thông tin.Memory Views for more information.

________ 469 (itable, *[, key, mặc định])(iterable, *[, key, default])
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
69(arg1, arg2, *args[, key])

Trả lại vật phẩm nhỏ nhất trong một điều có thể lặp lại hoặc nhỏ nhất trong hai hoặc nhiều đối số.

Nếu một đối số vị trí được cung cấp, nó phải là một điều có thể. Các mặt hàng nhỏ nhất trong ITBER có thể được trả lại. Nếu hai hoặc nhiều đối số vị trí được cung cấp, thì các đối số vị trí nhỏ nhất được trả về.iterable. The smallest item in the iterable is returned. If two or more positional arguments are provided, the smallest of the positional arguments is returned.

Có hai đối số chỉ từ khóa tùy chọn. Đối số chính chỉ định hàm đặt hàng một đối tượng như được sử dụng cho

class C:
    @classmethod
    def f(cls, arg1, arg2): ...
63. Đối số mặc định chỉ định một đối tượng để trả về nếu ITBER được cung cấp là trống. Nếu có thể trống và mặc định không được cung cấp,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
83 sẽ được nâng lên.

Nếu nhiều mục là tối đa, chức năng trả về mục đầu tiên gặp phải. Điều này phù hợp với các công cụ bảo quản ổn định sắp xếp khác như

class C:
    @classmethod
    def f(cls, arg1, arg2): ...
65 và
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
66.

Mới trong phiên bản 3.4: Đối số chỉ từ khóa mặc định.The default keyword-only argument.

Đã thay đổi trong phiên bản 3.8: Khóa có thể là

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
05.The key can be
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
05.

Lớp ________ 468 (đối tượng)(iterator[, default])

Trả về một đối tượng Chế độ xem bộ nhớ của người Viking được tạo từ đối số đã cho. Xem chế độ xem bộ nhớ để biết thêm thông tin.iterator by calling its

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
85 method. If default is given, it is returned if the iterator is exhausted, otherwise
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
50 is raised.

________ 469 (itable, *[, key, mặc định])

Trả lại vật phẩm nhỏ nhất trong một điều có thể lặp lại hoặc nhỏ nhất trong hai hoặc nhiều đối số.

Ghi chú

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
48 không có
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
73, vì vậy bạn có thể gán các thuộc tính tùy ý cho một thể hiện của lớp
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
48.

________ 484 (x)(x)

Chuyển đổi một số nguyên thành một chuỗi octal có tiền tố với số 0o 0o. Kết quả là một biểu thức python hợp lệ. Nếu X không phải là đối tượng Python

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
35, thì nó phải xác định một phương thức
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
36 trả về một số nguyên. Ví dụ:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
5

Nếu bạn muốn chuyển đổi một số nguyên thành chuỗi bát phân với tiền tố là 0o 0o, hoặc không, bạn có thể sử dụng một trong những cách sau.

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
6

Xem thêm

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
6 để biết thêm thông tin.

________ 488 (Tệp, mode = 'r', buffering =- 1, mã hóa = không, lỗi = none, newline = none(file, mode='r', buffering=- 1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

Mở tệp và trả về một đối tượng tệp tương ứng. Nếu tệp không thể được mở, một

class C:
    @classmethod
    def f(cls, arg1, arg2): ...
89 sẽ được nâng lên. Xem đọc và viết tệp để biết thêm ví dụ về cách sử dụng chức năng này.file object. If the file cannot be opened, an
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
89 is raised. See Reading and Writing Files for more examples of how to use this function.

Tệp là một đối tượng giống như đường dẫn cho tên đường dẫn (tuyệt đối hoặc liên quan đến thư mục làm việc hiện tại) của tệp sẽ được mở hoặc một bộ mô tả tệp số nguyên của tệp được gói. .path-like object giving the pathname (absolute or relative to the current working directory) of the file to be opened or an integer file descriptor of the file to be wrapped. (If a file descriptor is given, it is closed when the returned I/O object is closed unless closefd is set to

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
26.)

Chế độ là một chuỗi tùy chọn chỉ định chế độ mà tệp được mở. Nó mặc định là

class C:
    @classmethod
    def f(cls, arg1, arg2): ...
91 có nghĩa là mở để đọc ở chế độ văn bản. Các giá trị phổ biến khác là
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
92 để viết (cắt ngắn tệp nếu nó đã tồn tại),
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
93 để tạo độc quyền và
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
79 để nối thêm (trên một số hệ thống Unix, có nghĩa là tất cả đều ghi vào cuối tệp bất kể vị trí tìm kiếm hiện tại hiện tại ). Trong chế độ văn bản, nếu mã hóa không được chỉ định, mã hóa được sử dụng phụ thuộc vào nền tảng:
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
95 được gọi để có được mã hóa ngôn ngữ hiện tại. .

Tính cách

Nghĩa

class C:
    @classmethod
    def f(cls, arg1, arg2): ...
91

Mở để đọc (mặc định)

class C:
    @classmethod
    def f(cls, arg1, arg2): ...
92

Mở để viết, cắt ngắn tệp trước

class C:
    @classmethod
    def f(cls, arg1, arg2): ...
93

Mở để tạo độc quyền, không thành công nếu tệp đã tồn tại

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
79

Mở để viết, thêm vào cuối tệp nếu nó tồn tại

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
00

Chế độ nhị phân

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
01

Chế độ văn bản (mặc định)

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
29

Mở để cập nhật (đọc và viết)

Chế độ mặc định là

class C:
    @classmethod
    def f(cls, arg1, arg2): ...
91 (mở để đọc văn bản, từ đồng nghĩa của
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
04). Các chế độ
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
05 và
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
06 Mở và cắt ngắn tệp. Các chế độ
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
07 và
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
08 Mở tệp mà không bị cắt.

Như đã đề cập trong tổng quan, Python phân biệt giữa nhị phân và văn bản I/O. Các tệp được mở ở chế độ nhị phân (bao gồm

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
00 trong đối số chế độ) trả về nội dung dưới dạng đối tượng
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
62 mà không có bất kỳ giải mã nào. Trong chế độ văn bản (mặc định hoặc khi
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
01 được bao gồm trong đối số chế độ), nội dung của tệp được trả về dưới dạng
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
12, các byte lần đầu tiên được giải mã bằng cách sử dụng mã hóa phụ thuộc vào nền tảng hoặc sử dụng mã hóa được chỉ định nếu được cung cấp.Overview, Python distinguishes between binary and text I/O. Files opened in binary mode (including
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
00 in the mode argument) return contents as
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
62 objects without any decoding. In text mode (the default, or when
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
01 is included in the mode argument), the contents of the file are returned as
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
12, the bytes having been first decoded using a platform-dependent encoding or using the specified encoding if given.

Có một ký tự chế độ bổ sung được phép,

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
13, không còn ảnh hưởng nào nữa và được coi là không dùng nữa. Trước đây nó đã kích hoạt Universal Newlines trong chế độ văn bản, trở thành hành vi mặc định trong Python 3.0. Tham khảo tài liệu của tham số Newline để biết thêm chi tiết.universal newlines in text mode, which became the default behavior in Python 3.0. Refer to the documentation of the newline parameter for further details.

Ghi chú

Python không phụ thuộc vào hệ điều hành cơ bản khái niệm về các tệp văn bản; Tất cả việc xử lý được thực hiện bởi chính Python, và do đó độc lập với nền tảng.

Bộ đệm là một số nguyên tùy chọn được sử dụng để đặt chính sách đệm. Vượt qua 0 để chuyển bộ đệm tắt (chỉ được phép ở chế độ nhị phân), 1 để chọn bộ đệm dòng (chỉ có thể sử dụng ở chế độ văn bản) và số nguyên> 1 để biểu thị kích thước trong byte của bộ đệm chunk có kích thước cố định. Lưu ý rằng việc chỉ định kích thước bộ đệm theo cách này áp dụng cho I/O được đệm nhị phân, nhưng

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
14 (tức là, các tệp được mở bằng
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
15) sẽ có một bộ đệm khác. Để vô hiệu hóa bộ đệm trong
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
14, hãy xem xét sử dụng cờ
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
17 cho
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
18. Khi không có đối số bộ đệm nào được đưa ra, chính sách bộ đệm mặc định hoạt động như sau:

  • Các tập tin nhị phân được đệm trong các khối kích thước cố định; Kích thước của bộ đệm được chọn bằng cách sử dụng một heuristic để cố gắng xác định thiết bị cơ bản kích thước khối, và rơi trở lại trên

    >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    19. Trên nhiều hệ thống, bộ đệm thường sẽ dài 4096 hoặc 8192 byte.

  • Các tệp văn bản tương tác trực tuyến (các tệp

    >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    20 trả về
    def any(iterable):
        for element in iterable:
            if element:
                return True
        return False
    
    19) sử dụng bộ đệm dòng. Các tệp văn bản khác sử dụng chính sách được mô tả ở trên cho các tệp nhị phân.

Mã hóa là tên của mã hóa được sử dụng để giải mã hoặc mã hóa tệp. Điều này chỉ nên được sử dụng trong chế độ văn bản. Mã hóa mặc định phụ thuộc vào nền tảng (bất cứ điều gì

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
22 trả về), nhưng bất kỳ mã hóa văn bản nào được hỗ trợ bởi Python đều có thể được sử dụng. Xem mô -đun
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
23 để biết danh sách các mã hóa được hỗ trợ.text encoding supported by Python can be used. See the
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
23 module for the list of supported encodings.

Lỗi là một chuỗi tùy chọn chỉ định cách xử lý các lỗi mã hóa và giải mã được xử lý, điều này không thể được sử dụng trong chế độ nhị phân. Một loạt các trình xử lý lỗi tiêu chuẩn có sẵn (được liệt kê trong trình xử lý lỗi), mặc dù mọi tên xử lý lỗi đã được đăng ký với

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
24 cũng hợp lệ. Các tên tiêu chuẩn bao gồm:Error Handlers), though any error handling name that has been registered with
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
24 is also valid. The standard names include:

  • >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    25 để tăng ngoại lệ
    def any(iterable):
        for element in iterable:
            if element:
                return True
        return False
    
    83 nếu có lỗi mã hóa. Giá trị mặc định của
    >>> bin(3)
    '0b11'
    >>> bin(-10)
    '-0b1010'
    
    05 có hiệu ứng tương tự.

  • >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    28 bỏ qua lỗi. Lưu ý rằng việc bỏ qua các lỗi mã hóa có thể dẫn đến mất dữ liệu.

  • >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    29 gây ra một dấu hiệu thay thế (chẳng hạn như
    >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    30) được chèn vào nơi có dữ liệu không đúng.

  • >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    31 sẽ biểu thị bất kỳ byte không chính xác nào dưới dạng các đơn vị mã thay thế thấp từ U+DC80 đến U+DCFF. Các đơn vị mã thay thế này sau đó sẽ được biến trở lại thành cùng một byte khi trình xử lý lỗi
    >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    32 được sử dụng khi viết dữ liệu. Điều này rất hữu ích cho việc xử lý các tệp trong một mã hóa không xác định.

  • >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    33 chỉ được hỗ trợ khi ghi vào tệp. Các ký tự không được hỗ trợ bởi mã hóa được thay thế bằng tham chiếu ký tự XML thích hợp
    >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    34.

  • >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    35 thay thế dữ liệu bị dị dạng bằng các chuỗi thoát hiểm Python.

  • >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    36 (cũng chỉ được hỗ trợ khi viết) thay thế các ký tự không được hỗ trợ bằng các chuỗi thoát
    >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    37.

Newline xác định cách phân tích các ký tự dòng mới từ luồng. Nó có thể là

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
05,
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
39,
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
40,
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
41 và
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
42. Nó hoạt động như sau:

  • Khi đọc đầu vào từ luồng, nếu Newline là

    >>> bin(3)
    '0b11'
    >>> bin(-10)
    '-0b1010'
    
    05, chế độ NewLines phổ quát sẽ được bật. Các dòng trong đầu vào có thể kết thúc trong
    >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    40,
    >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    41 hoặc
    >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    42 và chúng được dịch thành
    >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    40 trước khi được trả lại cho người gọi. Nếu đó là
    >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    39, chế độ NewLines Universal được bật, nhưng các kết thúc dòng được trả lại cho người gọi không được dịch. Nếu nó có bất kỳ giá trị pháp lý nào khác, các dòng đầu vào chỉ bị chấm dứt bởi chuỗi đã cho và kết thúc dòng được trả lại cho người gọi không được dịch.

  • Khi viết đầu ra vào luồng, nếu Newline là

    >>> bin(3)
    '0b11'
    >>> bin(-10)
    '-0b1010'
    
    05, bất kỳ ký tự
    >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    40 nào được viết được dịch sang bộ phân cách dòng mặc định của hệ thống,
    >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    51. Nếu Newline là
    >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    39 hoặc
    >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    40, không có bản dịch nào diễn ra. Nếu Newline là bất kỳ giá trị pháp lý nào khác, bất kỳ ký tự
    >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    40 nào được viết thành chuỗi đã cho.

Nếu CloseFD là

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
26 và bộ mô tả tệp thay vì tên tệp được đưa ra, bộ mô tả tệp cơ bản sẽ được mở khi tệp được đóng. Nếu tên tệp được đưa ra gần phải là
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
19 (mặc định); Nếu không, một lỗi sẽ được nêu ra.

Một dụng cụ mở tùy chỉnh có thể được sử dụng bằng cách truyền một người gọi là người mở. Bộ mô tả tệp cơ bản cho đối tượng tệp sau đó được lấy bằng cách gọi mở với (tệp, cờ). Trình mở phải trả về một bộ mô tả tệp mở (PASS

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
57 dưới dạng công cụ mở dẫn đến chức năng tương tự như vượt qua
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
05).

Tệp mới được tạo là không thể không thể cưỡng lại được.non-inheritable.

Ví dụ sau sử dụng tham số DIR_FD của hàm

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
59 để mở tệp liên quan đến một thư mục đã cho:dir_fd parameter of the
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
59 function to open a file relative to a given directory:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
7

Loại đối tượng tệp được trả về bởi hàm

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
0 phụ thuộc vào chế độ. Khi
sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
0 được sử dụng để mở một tệp ở chế độ văn bản (
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
92,
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
91,
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
64,
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
04, v.v.), nó sẽ trả về một lớp con là
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
66 (cụ thể là
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
67). Khi được sử dụng để mở một tệp ở chế độ nhị phân với bộ đệm, lớp được trả về là một lớp con của
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
68. Lớp chính xác khác nhau: Trong chế độ nhị phân đọc, nó trả về
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
69; Trong Write nhị phân và phụ lục các chế độ nhị phân, nó trả về một
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
70 và trong chế độ đọc/ghi, nó trả về một
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
71. Khi bộ đệm bị vô hiệu hóa, luồng thô, một lớp con của
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
72,
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
73, được trả về.file object returned by the
sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
0 function depends on the mode. When
sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
0 is used to open a file in a text mode (
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
92,
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
91,
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
64,
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
04, etc.), it returns a subclass of
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
66 (specifically
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
67). When used to open a file in a binary mode with buffering, the returned class is a subclass of
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
68. The exact class varies: in read binary mode, it returns an
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
69; in write binary and append binary modes, it returns an
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
70, and in read/write mode, it returns an
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
71. When buffering is disabled, the raw stream, a subclass of
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
72,
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
73, is returned.

Xem thêm các mô -đun xử lý tệp, chẳng hạn như

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
74,
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
75 (trong đó
sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
0 được khai báo),
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
77,
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
78,
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
79 và
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
80.

Tăng một sự kiện kiểm toán

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
81 với các đối số
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
82,
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
83,
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
84.auditing event
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
81 with arguments
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
82,
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
83,
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
84.

Các đối số

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
83 và
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
84 có thể đã được sửa đổi hoặc suy ra từ cuộc gọi ban đầu.

Đã thay đổi trong phiên bản 3.3:

  • Tham số mở đã được thêm vào.

  • Chế độ

    class C:
        @classmethod
        def f(cls, arg1, arg2): ...
    
    93 đã được thêm vào.

  • >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    88 đã từng được nâng lên, bây giờ nó là bí danh của
    class C:
        @classmethod
        def f(cls, arg1, arg2): ...
    
    89.

  • >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    90 hiện được nâng lên nếu tệp được mở ở chế độ tạo độc quyền (
    class C:
        @classmethod
        def f(cls, arg1, arg2): ...
    
    93) đã tồn tại.

Đã thay đổi trong phiên bản 3.4:

  • Các tập tin hiện không thể không thể cưỡng lại được.

Không dùng nữa kể từ phiên bản 3.4, bị xóa trong phiên bản 3.10: Chế độ

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
13.The
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
13 mode.

Đã thay đổi trong phiên bản 3.5:

  • Nếu cuộc gọi hệ thống bị gián đoạn và trình xử lý tín hiệu không tăng ngoại lệ, thì chức năng bây giờ sẽ gọi lại cuộc gọi hệ thống thay vì tăng ngoại lệ

    >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    93 (xem PEP 475 cho lý do).PEP 475 for the rationale).

  • Trình xử lý lỗi

    >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    36 đã được thêm vào.

Đã thay đổi trong phiên bản 3.6:

  • Hỗ trợ được thêm vào để chấp nhận các đối tượng thực hiện

    >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    95.

  • Trên Windows, việc mở bộ đệm bảng điều khiển có thể trả về một lớp con là

    >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    72 khác với
    >>> import struct
    >>> dir()   # show the names in the module namespace  
    ['__builtins__', '__name__', 'struct']
    >>> dir(struct)   # show the names in the struct module 
    ['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
     '__initializing__', '__loader__', '__name__', '__package__',
     '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
     'unpack', 'unpack_from']
    >>> class Shape:
    ...     def __dir__(self):
    ...         return ['area', 'perimeter', 'location']
    >>> s = Shape()
    >>> dir(s)
    ['area', 'location', 'perimeter']
    
    73.

________ 598 (c)(c)

Đưa ra một chuỗi đại diện cho một ký tự Unicode, hãy trả về một số nguyên đại diện cho điểm mã Unicode của ký tự đó. Ví dụ:

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
99 trả về số nguyên
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
00 và
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
01 (dấu Euro) trả về
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
02. Đây là nghịch đảo của
>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
3.

________ 604 (cơ sở, exp [, mod]) ¶(base, exp[, mod])

Trả lại cơ sở cho Power Exp; Nếu mod có mặt, hãy trả lại cơ sở cho exp power, modulo mod (được tính toán hiệu quả hơn so với

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
05). Mẫu hai đối số
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
06 tương đương với việc sử dụng toán tử nguồn:
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
07.

Các đối số phải có các loại số. Với các loại toán hạng hỗn hợp, các quy tắc ép buộc cho các toán tử số học nhị phân áp dụng. Đối với các toán hạng

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
35, kết quả có cùng loại với các toán hạng (sau khi ép buộc) trừ khi đối số thứ hai là âm; Trong trường hợp đó, tất cả các đối số được chuyển đổi thành float và kết quả nổi được phân phối. Ví dụ:
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
09 trả về
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
10, nhưng
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
11 trả về
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
12. Đối với một cơ sở âm của loại
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
35 hoặc
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
36 và số mũ không phân tích, một kết quả phức tạp được cung cấp. Ví dụ:
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
15 trả về một giá trị gần với
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
16.

Đối với

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
35 Operands Base và Exp, nếu có mod, mod cũng phải thuộc loại số nguyên và mod phải là không khác. Nếu mod có mặt và exp là âm, cơ sở phải tương đối chính với mod. Trong trường hợp đó,
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
18 được trả về, trong đó Inv_Base là một nghịch đảo với mod modulo cơ sở.

Ở đây, một ví dụ về tính toán nghịch đảo cho

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
19 modulo
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
00:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
8

Đã thay đổi trong phiên bản 3.8: Đối với các toán hạng

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
35, hình thức ba đối tượng của
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
22 hiện cho phép đối số thứ hai là âm, cho phép tính toán các nghịch đảo mô-đun.For
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
35 operands, the three-argument form of
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
22 now allows the second argument to be negative, permitting computation of modular inverses.

Thay đổi trong phiên bản 3.8: Cho phép đối số từ khóa. Trước đây, chỉ có các lập luận vị trí được hỗ trợ.Allow keyword arguments. Formerly, only positional arguments were supported.

________ 623 (*đối tượng, sep = '', end = '\ n', file = sys.stdout, flush = false)(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

Các đối tượng in vào tệp luồng văn bản, được phân tách bằng SEP và theo sau là kết thúc. Tháng 9, kết thúc, tệp và tuôn ra, nếu có, phải được đưa ra làm đối số từ khóa.

Tất cả các đối số không phải là Keyword được chuyển đổi thành các chuỗi như

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
04 và được ghi vào luồng, được phân tách bằng SEP và theo sau là kết thúc. Cả SEP và END phải là chuỗi; Chúng cũng có thể là
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
05, có nghĩa là sử dụng các giá trị mặc định. Nếu không có đối tượng nào được đưa ra,
sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
3 sẽ chỉ viết kết thúc.

Đối số tệp phải là một đối tượng với phương thức

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
27; Nếu nó không có mặt hoặc
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
05,
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
29 sẽ được sử dụng. Vì các đối số in được chuyển đổi thành chuỗi văn bản,
sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
3 không thể được sử dụng với các đối tượng tệp chế độ nhị phân. Đối với những điều này, hãy sử dụng
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
31 thay thế.

Cho dù đầu ra được đệm thường được xác định bằng tệp, nhưng nếu đối số từ khóa Flush là đúng, luồng bị buộc phải xóa.

Đã thay đổi trong phiên bản 3.3: Đã thêm đối số từ khóa Flush.Added the flush keyword argument.

Lớp ________ 632 (fget = none, fset = none, fdel = none, doc = none) ¶(fget=None, fset=None, fdel=None, doc=None)

Trả về một thuộc tính thuộc tính.

FGET là một hàm để có được một giá trị thuộc tính. FSET là một hàm để thiết lập một giá trị thuộc tính. FDEL là một hàm để xóa giá trị thuộc tính. Và Doc tạo ra một tài liệu cho thuộc tính.

Một cách sử dụng điển hình là xác định thuộc tính được quản lý

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
38:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
9

Nếu C là một ví dụ của C,

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
34 sẽ gọi getter,
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
35 sẽ gọi người thiết lập và
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
36 Deleter.

Nếu được đưa ra, Doc sẽ là tài liệu của thuộc tính tài sản. Mặt khác, tài sản sẽ sao chép tài liệu FGET (nếu nó tồn tại). Điều này cho phép tạo các thuộc tính chỉ đọc dễ dàng bằng cách sử dụng

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
4 làm người trang trí:decorator:

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
0

Bộ trang trí

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
38 biến phương thức
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
39 thành một getter getter cho một thuộc tính chỉ đọc có cùng tên và nó đặt tài liệu cho điện áp để nhận điện áp hiện tại.

Một đối tượng thuộc tính có các phương pháp

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
40,
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
41 và
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
42 có thể sử dụng như các trình trang trí tạo một bản sao của tài sản với hàm truy cập tương ứng được đặt thành hàm được trang trí. Điều này được giải thích tốt nhất với một ví dụ:

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
1

Mã này hoàn toàn tương đương với ví dụ đầu tiên. Hãy chắc chắn đặt cho các chức năng bổ sung cùng tên với thuộc tính gốc (

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
38 trong trường hợp này.)

Đối tượng thuộc tính được trả về cũng có các thuộc tính

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
44,
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
45 và
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
46 tương ứng với các đối số của hàm tạo.

Đã thay đổi trong phiên bản 3.5: Các tài liệu của các đối tượng thuộc tính hiện có thể ghi được.The docstrings of property objects are now writeable.

Lớp ________ 647 (Dừng) Lớp ________ 647 (bắt đầu, dừng [, bước])(stop) class
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
47(start, stop[, step])

Thay vì là một hàm,

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
49 thực sự là một loại trình tự bất biến, như được ghi trong phạm vi và loại trình tự - danh sách, tuple, phạm vi.Ranges and Sequence Types — list, tuple, range.

________ 650 (đối tượng) ¶(object)

Trả về một chuỗi chứa một biểu diễn có thể in của một đối tượng. Đối với nhiều loại, chức năng này thực hiện một nỗ lực để trả về một chuỗi sẽ mang lại một đối tượng có cùng giá trị khi được chuyển đến

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
2; Mặt khác, biểu diễn là một chuỗi được đặt trong các khung góc chứa tên của loại đối tượng cùng với thông tin bổ sung thường bao gồm tên và địa chỉ của đối tượng. Một lớp có thể kiểm soát những gì hàm này trả về cho các trường hợp của nó bằng cách xác định phương thức
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
52. Nếu
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
53 không thể truy cập được, chức năng này sẽ tăng
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
57.

________ 655 (SEQ) ¶(seq)

Trả về một trình lặp ngược. SEQ phải là một đối tượng có phương thức

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
56 hoặc hỗ trợ giao thức trình tự (phương pháp
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
57 và phương thức
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
46 với các đối số số nguyên bắt đầu từ
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
14).iterator. seq must be an object which has a
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
56 method or supports the sequence protocol (the
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
57 method and the
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
46 method with integer arguments starting at
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
14).

________ 660 (số [, ndigits]) ¶(number[, ndigits])

Số trả về được làm tròn vào độ chính xác của ndigits sau điểm thập phân. Nếu ndigits bị bỏ qua hoặc là

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
05, nó sẽ trả lại số nguyên gần nhất về đầu vào của nó.

Đối với các loại tích hợp hỗ trợ

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
8, các giá trị được làm tròn đến bội số gần nhất của 10 với các hình ảnh trừ Power Minus; Nếu hai bội số gần như nhau, việc làm tròn được thực hiện theo lựa chọn chẵn (ví dụ, cả
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
63 và
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
64 là
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
14 và
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
66 là
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
18). Bất kỳ giá trị số nguyên nào cũng hợp lệ cho ndigits (dương, 0 hoặc âm). Giá trị trả về là một số nguyên nếu ndigits bị bỏ qua hoặc
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
05. Mặt khác, giá trị trả về có cùng loại với số.

Đối với một đối tượng Python chung

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
69,
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
70 đại biểu cho
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
71.

Ghi chú

Hành vi của

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
8 đối với phao có thể gây ngạc nhiên: ví dụ,
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
73 cho
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
74 thay vì dự kiến ​​
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
75. Đây không phải là một lỗi: Nó là kết quả của thực tế là hầu hết các phân số thập phân có thể được thể hiện chính xác dưới dạng phao. Xem số học dấu phẩy động: Các vấn đề và giới hạn để biết thêm thông tin.Floating Point Arithmetic: Issues and Limitations for more information.

Lớp ________ 676 ([itable])([iterable])

Trả về một đối tượng

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
64 mới, tùy chọn với các phần tử được lấy từ ITable.
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
64 là một lớp tích hợp. Xem các loại
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
64 và SET - Đặt, Frozenset cho tài liệu về lớp này.Set Types — set, frozenset for documentation about this class.

Đối với các thùng chứa khác, hãy xem các lớp

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
52,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
63,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
65 và
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
61 tích hợp, cũng như mô-đun
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
66.

________ 685 (đối tượng, tên, giá trị) ¶(object, name, value)

Đây là đối tác của

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
8. Các đối số là một đối tượng, một chuỗi và một giá trị tùy ý. Chuỗi có thể đặt tên cho một thuộc tính hiện có hoặc thuộc tính mới. Hàm gán giá trị cho thuộc tính, với điều kiện đối tượng cho phép nó. Ví dụ,
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
87 tương đương với
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
88.

Tên không cần phải là một định danh Python như được định nghĩa trong các định danh và từ khóa trừ khi đối tượng chọn thực thi điều đó, ví dụ như trong một tùy chỉnh ____270 hoặc qua

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
90. Một thuộc tính có tên không phải là một định danh sẽ không thể truy cập được bằng cách sử dụng ký hiệu dấu chấm, nhưng có thể truy cập được thông qua
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
8, v.v.Identifiers and keywords unless the object chooses to enforce that, for example in a custom
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
70 or via
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
90. An attribute whose name is not an identifier will not be accessible using the dot notation, but is accessible through
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
8 etc..

Ghi chú

Hành vi của

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
8 đối với phao có thể gây ngạc nhiên: ví dụ,
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
73 cho
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
74 thay vì dự kiến ​​
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
75. Đây không phải là một lỗi: Nó là kết quả của thực tế là hầu hết các phân số thập phân có thể được thể hiện chính xác dưới dạng phao. Xem số học dấu phẩy động: Các vấn đề và giới hạn để biết thêm thông tin.private name mangling happens at compilation time, one must manually mangle a private attribute’s (attributes with two leading underscores) name in order to set it with
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
00.

Lớp ________ 676 ([itable])(stop)¶ class
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
93(start, stop[, step])

Trả về một đối tượng

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
64 mới, tùy chọn với các phần tử được lấy từ ITable.
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
64 là một lớp tích hợp. Xem các loại
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
64 và SET - Đặt, Frozenset cho tài liệu về lớp này.slice object representing the set of indices specified by
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
95. The start and step arguments default to
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
05. Slice objects have read-only data attributes
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
97,
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
98, and
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
99 which merely return the argument values (or their default). They have no other explicit functionality; however, they are used by NumPy and other third-party packages. Slice objects are also generated when extended indexing syntax is used. For example:
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
00 or
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
01. See
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
02 for an alternate version that returns an iterator.

Đối với các thùng chứa khác, hãy xem các lớp
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
52,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
63,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
65 và
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
61 tích hợp, cũng như mô-đun
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
66.(iterable, /, *, key=None, reverse=False)

________ 685 (đối tượng, tên, giá trị) ¶

Đây là đối tác của

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
8. Các đối số là một đối tượng, một chuỗi và một giá trị tùy ý. Chuỗi có thể đặt tên cho một thuộc tính hiện có hoặc thuộc tính mới. Hàm gán giá trị cho thuộc tính, với điều kiện đối tượng cho phép nó. Ví dụ,
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
87 tương đương với
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
88.

Tên không cần phải là một định danh Python như được định nghĩa trong các định danh và từ khóa trừ khi đối tượng chọn thực thi điều đó, ví dụ như trong một tùy chỉnh ____270 hoặc qua

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
90. Một thuộc tính có tên không phải là một định danh sẽ không thể truy cập được bằng cách sử dụng ký hiệu dấu chấm, nhưng có thể truy cập được thông qua
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
8, v.v.

Vì tên riêng tư xảy ra vào thời điểm biên dịch, người ta phải tự xử lý một tên riêng tư (thuộc tính có hai dấu gạch dưới hàng đầu) để đặt nó với

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
00.

Lớp ________ 693 (Dừng) Lớp ____ ____ 693 (Bắt đầu, Dừng [, Bước])

Hàm

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
02 tích hợp được đảm bảo là ổn định. Một loại ổn định nếu nó đảm bảo không thay đổi thứ tự tương đối của các yếu tố so sánh bằng nhau - điều này rất hữu ích để sắp xếp trong nhiều lần vượt qua (ví dụ, sắp xếp theo bộ phận, sau đó theo mức lương).

Thuật toán sắp xếp chỉ sử dụng so sánh

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
09 giữa các mục. Trong khi xác định phương pháp
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
10 sẽ đủ để sắp xếp, PEP 8 khuyến nghị tất cả sáu so sánh phong phú sẽ được thực hiện. Điều này sẽ giúp tránh các lỗi khi sử dụng cùng một dữ liệu với các công cụ đặt hàng khác như
>>> x = 1
>>> eval('x+1')
2
4 dựa vào một phương thức cơ bản khác. Việc thực hiện tất cả sáu so sánh cũng giúp tránh sự nhầm lẫn cho các so sánh loại hỗn hợp có thể gọi phản ánh phương pháp
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
12.PEP 8 recommends that all six rich comparisons be implemented. This will help avoid bugs when using the same data with other ordering tools such as
>>> x = 1
>>> eval('x+1')
2
4 that rely on a different underlying method. Implementing all six comparisons also helps avoid confusion for mixed type comparisons which can call reflected the
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
12 method.

Để sắp xếp các ví dụ và một hướng dẫn sắp xếp ngắn gọn, xem phân loại cách.Sorting HOW TO.

________ 184 ________ 714¶

Chuyển đổi một phương thức thành một phương thức tĩnh.

Một phương pháp tĩnh không nhận được một đối số đầu tiên ngầm. Để khai báo một phương pháp tĩnh, hãy sử dụng thành ngữ này:

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
2

Mẫu

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
15 là một trình trang trí chức năng - xem các định nghĩa chức năng để biết chi tiết.decorator – see Function definitions for details.

Một phương pháp tĩnh có thể được gọi là trên lớp (chẳng hạn như

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
87) hoặc trên một thể hiện (chẳng hạn như
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
88). Hơn nữa, chúng có thể được gọi là chức năng thông thường (chẳng hạn như
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
18).

Các phương pháp tĩnh trong Python tương tự như các phương pháp được tìm thấy trong Java hoặc C ++. Ngoài ra, xem

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
4 để biết một biến thể hữu ích để tạo các hàm tạo lớp thay thế.

Giống như tất cả các nhà trang trí, cũng có thể gọi

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
20 là một chức năng thường xuyên và làm điều gì đó với kết quả của nó. Điều này là cần thiết trong một số trường hợp bạn cần tham chiếu đến một chức năng từ cơ thể lớp và bạn muốn tránh chuyển đổi tự động sang phương thức thể hiện. Đối với những trường hợp này, hãy sử dụng thành ngữ này:

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
3

Để biết thêm thông tin về các phương thức tĩnh, hãy xem hệ thống phân cấp loại tiêu chuẩn.The standard type hierarchy.

Đã thay đổi trong phiên bản 3.10: Các phương thức tĩnh hiện kế thừa các thuộc tính phương thức (

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
91,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
92,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
93,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
94 và
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
95), có thuộc tính
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
96 mới và hiện có thể gọi là các chức năng thông thường.Static methods now inherit the method attributes (
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
91,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
92,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
93,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
94 and
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
95), have a new
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
96 attribute, and are now callable as regular functions.

Lớp ________ 727 (Object = '') Lớp ________ 727 (Object = b '', mã hóa = 'utf-8', lỗi = 'nghiêm ngặt')(object='') class
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
27(object=b'', encoding='utf-8', errors='strict')

Trả về phiên bản

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
12 của đối tượng. Xem
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
04 để biết chi tiết.

>>> import struct
>>> dir()   # show the names in the module namespace  
['__builtins__', '__name__', 'struct']
>>> dir(struct)   # show the names in the struct module 
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
 '__initializing__', '__loader__', '__name__', '__package__',
 '_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
 'unpack', 'unpack_from']
>>> class Shape:
...     def __dir__(self):
...         return ['area', 'perimeter', 'location']
>>> s = Shape()
>>> dir(s)
['area', 'location', 'perimeter']
12 là lớp chuỗi tích hợp. Để biết thông tin chung về chuỗi, xem loại chuỗi văn bản - str.class. For general information about strings, see Text Sequence Type — str.

________ 732 (có thể lặp lại, /, bắt đầu = 0) ¶(iterable, /, start=0)

Tổng số bắt đầu và các mục của một thứ có thể lặp lại từ trái sang phải và trả lại tổng số. Các mục có thể lặp lại thường là các số và giá trị bắt đầu không được phép là một chuỗi.

Đối với một số trường hợp sử dụng, có những lựa chọn thay thế tốt cho

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
05. Cách nhanh chóng, nhanh chóng để kết hợp một chuỗi các chuỗi là bằng cách gọi
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
34. Để thêm các giá trị điểm nổi với độ chính xác mở rộng, xem
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
35. Để kết hợp một loạt các vòng lặp, hãy xem xét sử dụng
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
36.

Thay đổi trong phiên bản 3.8: Tham số bắt đầu có thể được chỉ định là đối số từ khóa.The start parameter can be specified as a keyword argument.

Lớp ________ 737 ([loại [, đối tượng hoặc loại]]) ¶([type[, object-or-type]])

Trả về một đối tượng proxy ủy quyền gọi cho một loại phụ huynh hoặc anh chị em của loại. Điều này rất hữu ích cho việc truy cập các phương thức kế thừa đã được ghi đè trong một lớp.

Loại hoặc loại đối tượng xác định thứ tự phân giải phương thức được tìm kiếm. Tìm kiếm bắt đầu từ lớp ngay sau loại.method resolution order to be searched. The search starts from the class right after the type.

Ví dụ: nếu

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
38 của đối tượng hoặc loại là
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
39 và giá trị của loại là
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
40, thì
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
06 tìm kiếm
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
42.

Thuộc tính

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
38 của loại đối tượng hoặc loại liệt kê thứ tự tìm kiếm độ phân giải phương thức được sử dụng bởi cả
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
8 và
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
06. Thuộc tính là động và có thể thay đổi bất cứ khi nào hệ thống phân cấp kế thừa được cập nhật.

Nếu đối số thứ hai bị bỏ qua, đối tượng siêu trả về không bị ràng buộc. Nếu đối số thứ hai là một đối tượng,

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
46 phải đúng. Nếu đối số thứ hai là một loại,
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
47 phải đúng (điều này hữu ích cho ClassMethods).

Có hai trường hợp sử dụng điển hình cho Super. Trong một hệ thống phân cấp lớp với kế thừa duy nhất, Super có thể được sử dụng để chỉ các lớp cha mà không đặt tên cho chúng một cách rõ ràng, do đó làm cho mã có thể duy trì hơn. Việc sử dụng này gần đúng với việc sử dụng siêu trong các ngôn ngữ lập trình khác.

Trường hợp sử dụng thứ hai là để hỗ trợ nhiều kế thừa hợp tác trong môi trường thực thi động. Trường hợp sử dụng này là duy nhất cho Python và không được tìm thấy trong các ngôn ngữ hoặc ngôn ngữ được biên dịch tĩnh chỉ hỗ trợ thừa kế đơn. Điều này cho phép thực hiện các sơ đồ kim cương trên mạng, nơi nhiều lớp cơ sở thực hiện cùng một phương pháp. Thiết kế tốt chỉ ra rằng các triển khai như vậy có cùng chữ ký cuộc gọi trong mọi trường hợp (vì thứ tự các cuộc gọi được xác định theo thời gian chạy, bởi vì thứ tự đó thích nghi với các thay đổi trong hệ thống phân cấp lớp và vì thứ tự đó có thể bao gồm các lớp anh chị em chưa biết trước khi chạy ).

Đối với cả hai trường hợp sử dụng, một cuộc gọi siêu lớp điển hình trông như thế này:

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
4

Ngoài việc tra cứu phương pháp,

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
06 cũng hoạt động để tra cứu thuộc tính. Một trường hợp sử dụng có thể cho điều này là gọi các mô tả trong lớp cha mẹ hoặc anh chị em.descriptors in a parent or sibling class.

Lưu ý rằng

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
06 được triển khai như là một phần của quy trình ràng buộc để tra cứu thuộc tính chấm dứt điểm như
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
50. Nó làm như vậy bằng cách thực hiện phương pháp
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
70 của riêng mình để tìm kiếm các lớp theo thứ tự có thể dự đoán được hỗ trợ nhiều kế thừa hợp tác. Theo đó,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
06 không được xác định cho các tra cứu ngầm bằng cách sử dụng các câu lệnh hoặc toán tử như
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
53.

Cũng lưu ý rằng, ngoài biểu mẫu đối số không,

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
06 không giới hạn sử dụng các phương thức bên trong. Hai biểu mẫu đối số chỉ định chính xác các đối số và đưa ra các tham chiếu phù hợp. Biểu mẫu đối số bằng không chỉ hoạt động bên trong định nghĩa lớp, vì trình biên dịch điền vào các chi tiết cần thiết để truy xuất chính xác lớp được xác định, cũng như truy cập phiên bản hiện tại cho các phương thức thông thường.

Để biết các đề xuất thực tế về cách thiết kế các lớp hợp tác bằng

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
06, hãy xem Hướng dẫn sử dụng Super ().

Lớp ________ 756 ([itable])([iterable])

Thay vì là một hàm,

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
65 thực sự là một loại trình tự bất biến, như được ghi trong các bộ dữ liệu và loại trình tự - danh sách, tuple, phạm vi.Tuples and Sequence Types — list, tuple, range.

Lớp ________ 758 (Đối tượng) ¶ Lớp ____ 758 (Tên, Base, Dict, ** KWDS)(object)¶ class
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
58(name, bases, dict, **kwds)

Với một đối số, trả về loại đối tượng. Giá trị trả về là một đối tượng loại và thường cùng một đối tượng được trả về bởi

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
60.

Hàm tích hợp

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
7 được khuyến nghị để kiểm tra loại đối tượng, bởi vì nó tính đến các lớp con.

Với ba đối số, trả về một đối tượng loại mới. Đây thực chất là một hình thức động của câu lệnh

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
62. Chuỗi tên là tên lớp và trở thành thuộc tính
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
92. Các cơ sở tuple chứa các lớp cơ sở và trở thành thuộc tính
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
64; Nếu trống,
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
48, cơ sở cuối cùng của tất cả các lớp, được thêm vào. Từ điển Dict chứa các định nghĩa thuộc tính và phương thức cho cơ thể lớp; Nó có thể được sao chép hoặc bọc trước khi trở thành thuộc tính
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
73. Hai câu sau đây tạo ra các đối tượng
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
67 giống hệt nhau:

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
5

Xem thêm Loại đối tượng.Type Objects.

Các đối số từ khóa được cung cấp cho ba biểu mẫu đối số được chuyển cho máy móc metaclass thích hợp (thường là

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
68) giống như cách các từ khóa trong một định nghĩa lớp (bên cạnh metaclass) sẽ.

Xem thêm tùy chỉnh tạo lớp.Customizing class creation.

Đã thay đổi trong phiên bản 3.6: Các lớp con của

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
67 mà không ghi đè lên
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
70 có thể không còn sử dụng biểu mẫu một đối tượng để lấy loại đối tượng.Subclasses of
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
67 which don’t override
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
70 may no longer use the one-argument form to get the type of an object.

________ 771 ([đối tượng]) ¶([object])

Trả về thuộc tính

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
73 cho một mô -đun, lớp, thể hiện hoặc bất kỳ đối tượng nào khác với thuộc tính
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
73.

Các đối tượng như mô -đun và phiên bản có thuộc tính

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
73 có thể cập nhật; Tuy nhiên, các đối tượng khác có thể có các hạn chế ghi trên các thuộc tính
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
73 của chúng (ví dụ: các lớp sử dụng
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
76 để ngăn chặn cập nhật từ điển trực tiếp).

Không có lập luận,

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
09 hoạt động như
>>> x = 1
>>> eval('x+1')
2
2. Lưu ý, từ điển địa phương chỉ hữu ích cho việc đọc vì các bản cập nhật cho từ điển địa phương bị bỏ qua.

Ngoại lệ

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
29 được nêu ra nếu một đối tượng được chỉ định nhưng nó không có thuộc tính
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
73 (ví dụ: nếu lớp của nó xác định thuộc tính
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
90).

________ 782 (*iterables, nghiêm ngặt = false) ¶(*iterables, strict=False)

Lặp lại trên một số lần lặp song song, tạo ra các bộ dữ liệu với một mục từ mỗi mục.

Example:

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
6

Chính thức hơn:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
10 Trả về một trình lặp của các bộ dữ liệu, trong đó bộ thứ ba chứa phần tử thứ i từ mỗi lần lặp đối số.

Một cách khác để nghĩ về

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
10 là nó biến các hàng thành các cột và các cột thành các hàng. Điều này tương tự như chuyển một ma trận.

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
10 là lười biếng: Các yếu tố giành được được xử lý cho đến khi có thể lặp lại được, ví dụ: bởi một vòng
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
86 hoặc bằng cách gói trong một
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
63.

Một điều cần xem xét là các vòng lặp được chuyển sang

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
10 có thể có độ dài khác nhau; Đôi khi theo thiết kế, và đôi khi vì một lỗi trong mã đã chuẩn bị các vòng lặp này. Python cung cấp ba cách tiếp cận khác nhau để xử lý vấn đề này:

  • Theo mặc định,

    def any(iterable):
        for element in iterable:
            if element:
                return True
        return False
    
    10 dừng khi có thể cạn kiệt ngắn nhất. Nó sẽ bỏ qua các mục còn lại trong các vòng lặp dài hơn, cắt giảm kết quả theo chiều dài của một thời gian ngắn nhất:

    >>> bin(3)
    '0b11'
    >>> bin(-10)
    '-0b1010'
    
    7

  • def any(iterable):
        for element in iterable:
            if element:
                return True
        return False
    
    10 thường được sử dụng trong trường hợp các phép lặp được giả định là có độ dài bằng nhau. Trong những trường hợp như vậy, nó khuyến nghị sử dụng tùy chọn
    def enumerate(sequence, start=0):
        n = start
        for elem in sequence:
            yield n, elem
            n += 1
    
    91. Đầu ra của nó giống như
    def any(iterable):
        for element in iterable:
            if element:
                return True
        return False
    
    10 thông thường:

    >>> bin(3)
    '0b11'
    >>> bin(-10)
    '-0b1010'
    
    8

    Không giống như hành vi mặc định, nó kiểm tra xem độ dài của các vòng lặp có giống hệt nhau không, tăng

    def any(iterable):
        for element in iterable:
            if element:
                return True
        return False
    
    83 nếu chúng aren aren:

    >>> bin(3)
    '0b11'
    >>> bin(-10)
    '-0b1010'
    
    9

    Nếu không có đối số

    def enumerate(sequence, start=0):
        n = start
        for elem in sequence:
            yield n, elem
            n += 1
    
    91, bất kỳ lỗi nào dẫn đến các vòng lặp có độ dài khác nhau sẽ bị im lặng, có thể biểu hiện như một lỗi khó tìm trong một phần khác của chương trình.

  • Các vòng lặp ngắn hơn có thể được đệm với giá trị không đổi để làm cho tất cả các vòng lặp có cùng độ dài. Điều này được thực hiện bởi

    def enumerate(sequence, start=0):
        n = start
        for elem in sequence:
            yield n, elem
            n += 1
    
    95.

Các trường hợp cạnh: Với một đối số có thể lặp lại duy nhất,

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
10 trả về một trình lặp gồm 1 bộ. Không có đối số, nó trả về một trình lặp trống.

Các mẹo và thủ thuật:

  • Thứ tự đánh giá từ trái sang phải của Iterables được đảm bảo. Điều này có thể làm cho một thành ngữ để phân cụm chuỗi dữ liệu thành các nhóm độ dài N bằng

    def enumerate(sequence, start=0):
        n = start
        for elem in sequence:
            yield n, elem
            n += 1
    
    97. Điều này lặp lại cùng một trình lặp
    def enumerate(sequence, start=0):
        n = start
        for elem in sequence:
            yield n, elem
            n += 1
    
    98 lần để mỗi bộ đầu ra có kết quả của các cuộc gọi
    def enumerate(sequence, start=0):
        n = start
        for elem in sequence:
            yield n, elem
            n += 1
    
    98 đến trình lặp. Điều này có tác dụng chia đầu vào thành các khối độ dài.

  • def any(iterable):
        for element in iterable:
            if element:
                return True
        return False
    
    10 Kết hợp với toán tử
    >>> x = 1
    >>> eval('x+1')
    2
    
    01 có thể được sử dụng để giải nén danh sách:

    >>> format(14, '#b'), format(14, 'b')
    ('0b1110', '1110')
    >>> f'{14:#b}', f'{14:b}'
    ('0b1110', '1110')
    
    0

Đã thay đổi trong phiên bản 3.10: Đã thêm đối số

>>> x = 1
>>> eval('x+1')
2
02.Added the
>>> x = 1
>>> eval('x+1')
2
02 argument.

________ 803 (Tên, Globals = none, locals = none, fromList = (), level = 0) ¶(name, globals=None, locals=None, fromlist=(), level=0)

Hàm này được gọi bởi câu lệnh

>>> x = 1
>>> eval('x+1')
2
04. Nó có thể được thay thế (bằng cách nhập mô -đun
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
89 và gán cho
>>> x = 1
>>> eval('x+1')
2
06) để thay đổi ngữ nghĩa của tuyên bố
>>> x = 1
>>> eval('x+1')
2
04, nhưng làm như vậy không được khuyến khích vì thường sử dụng các móc nhập khẩu (xem PEP 302) để đạt được cùng một mục tiêu tương tự và không gây ra các vấn đề với mã giả định việc triển khai nhập mặc định đang được sử dụng. Sử dụng trực tiếp
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
11 cũng không được khuyến khích ủng hộ
>>> x = 1
>>> eval('x+1')
2
09.strongly discouraged as it is usually simpler to use import hooks (see PEP 302) to attain the same goals and does not cause issues with code which assumes the default import implementation is in use. Direct use of
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
11 is also discouraged in favor of
>>> x = 1
>>> eval('x+1')
2
09.

Hàm nhập tên mô -đun, có khả năng sử dụng Globals và người dân địa phương đã cho để xác định cách diễn giải tên trong bối cảnh gói. FromList đưa ra tên của các đối tượng hoặc mô hình con nên được nhập từ mô -đun được đưa ra theo tên. Việc triển khai tiêu chuẩn hoàn toàn không sử dụng đối số của người dân địa phương và chỉ sử dụng toàn cầu của mình để xác định bối cảnh gói của câu lệnh

>>> x = 1
>>> eval('x+1')
2
04.

Cấp độ chỉ định nên sử dụng nhập khẩu tuyệt đối hay tương đối.

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
14 (mặc định) có nghĩa là chỉ thực hiện nhập khẩu tuyệt đối. Các giá trị tích cực cho cấp độ chỉ ra số lượng thư mục mẹ để tìm kiếm liên quan đến thư mục của mô -đun gọi
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
11 (xem PEP 328 để biết chi tiết).PEP 328 for the details).

Khi biến tên thuộc Mẫu

>>> x = 1
>>> eval('x+1')
2
13, thông thường, gói cấp cao nhất (tên cho đến chấm đầu tiên) được trả về, không phải mô-đun được đặt tên theo tên. Tuy nhiên, khi một đối số từ danh sách không phát thanh được đưa ra, mô-đun được đặt tên theo tên được trả về.

Ví dụ: câu lệnh

>>> x = 1
>>> eval('x+1')
2
14 dẫn đến mã byte giống như mã sau:

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
1

Tuyên bố

>>> x = 1
>>> eval('x+1')
2
15 kết quả trong cuộc gọi này:

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
2

Lưu ý cách

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
11 trả về mô -đun Toplevel ở đây vì đây là đối tượng được liên kết với một tên bằng câu lệnh
>>> x = 1
>>> eval('x+1')
2
04.

Mặt khác, tuyên bố

>>> x = 1
>>> eval('x+1')
2
18 dẫn đến

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
3

Ở đây, mô -đun

>>> x = 1
>>> eval('x+1')
2
19 được trả về từ
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
11. Từ đối tượng này, các tên để nhập được truy xuất và gán cho tên tương ứng của chúng.

Nếu bạn chỉ muốn nhập một mô -đun (có khả năng trong một gói) theo tên, hãy sử dụng

>>> x = 1
>>> eval('x+1')
2
09.

Thay đổi trong phiên bản 3.3: Các giá trị âm cho mức không còn được hỗ trợ (cũng thay đổi giá trị mặc định thành 0).Negative values for level are no longer supported (which also changes the default value to 0).

Đã thay đổi trong phiên bản 3.9: Khi các tùy chọn dòng lệnh

>>> x = 1
>>> eval('x+1')
2
22 hoặc
>>> x = 1
>>> eval('x+1')
2
23 đang được sử dụng, biến môi trường
>>> x = 1
>>> eval('x+1')
2
24 hiện đã bị bỏ qua.When the command line options
>>> x = 1
>>> eval('x+1')
2
22 or
>>> x = 1
>>> eval('x+1')
2
23 are being used, the environment variable
>>> x = 1
>>> eval('x+1')
2
24 is now ignored.

Chú thích

1

Lưu ý rằng trình phân tích cú pháp chỉ chấp nhận kết thúc kiểu UNIX của quy ước dòng. Nếu bạn đang đọc mã từ một tệp, hãy đảm bảo sử dụng chế độ chuyển đổi Newline để chuyển đổi các dòng mới của Windows hoặc Mac.

Làm thế nào để tôi tìm thấy

Kiểm tra với hàm tích hợp Dir () hàm tích hợp Dir () trả về danh sách tên của các thuộc tính, phương thức, v.v. của đối tượng được chỉ định trong đối số. Bạn có thể nhận được một danh sách các tên của các đối tượng tích hợp, chẳng hạn như các hàm và hằng số tích hợp, bằng cách chuyển mô-đun tích hợp hoặc __builtins__ cho dir ().dir() The built-in function dir() returns a list of names of attributes, methods, etc. of the object specified in the argument. You can get a list of names of built-in objects, such as built-in functions and constants, by passing the builtins module or __builtins__ to dir() .

Các chức năng được lưu trữ trong bộ nhớ ở đâu?

Ngăn xếp, trong đó các biến tự động được lưu trữ, cùng với thông tin được lưu mỗi khi một hàm được gọi.Mỗi lần được gọi là một hàm, địa chỉ của nơi quay lại và thông tin nhất định về môi trường của người gọi, chẳng hạn như một số thanh ghi máy, được lưu trên ngăn xếp., where automatic variables are stored, along with information that is saved each time a function is called. Each time a function is called, the address of where to return to and certain information about the caller's environment, such as some of the machine registers, are saved on the stack.

Có bao nhiêu python được xây dựng

Các chức năng Python tích hợp được xác định trước bởi trình thông dịch Python.Có 68 chức năng Python tích hợp.Các chức năng này thực hiện một nhiệm vụ cụ thể và có thể được sử dụng trong bất kỳ chương trình nào, tùy thuộc vào yêu cầu của người dùng.68 built-in python functions. These functions perform a specific task and can be used in any program, depending on the requirement of the user.

Những gì được xây dựng

Thư viện chứa các mô-đun tích hợp (được viết bằng C) cung cấp quyền truy cập vào chức năng hệ thống như tệp I/O mà nếu không các lập trình viên Python không thể truy cập được, cũng như các mô-đun được viết bằng Python cung cấp các giải pháp tiêu chuẩn cho nhiều vấn đề xảy ralập trình hàng ngày.provide access to system functionality such as file I/O that would otherwise be inaccessible to Python programmers, as well as modules written in Python that provide standardized solutions for many problems that occur in everyday programming.