Hướng dẫn is input a built in function in python - là đầu vào một hàm tích hợp trong python

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

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

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

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

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

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

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

B

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

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

C

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

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

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

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']
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

>>> 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

E

>>> 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

F

>>> 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

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')]
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

H

>>> 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

>>> 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

Tô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')]
7

>>> 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

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

L

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

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

M

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

N

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

O

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

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

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

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

P

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

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

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

R

>>> 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

S

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

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

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

T

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

V

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

Z

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

________ 105 (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
06. Nếu đối số là một số phức, độ lớn của nó được trả về.

________ 107 (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
08.asynchronous iterator for an asynchronous iterable. Equivalent to calling
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
08.

LƯU Ý: Không giống như

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
2,
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
5 không có biến thể 2 đối số.

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

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

Trả về

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
12 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ờ ________ 113 (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
0 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
15 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
16 đượ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
16 is raised.

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

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

Trả về

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
12 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 any(iterable):
    for element in iterable:
        if element:
            return True
    return False

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

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.

Đây là biến thể không đồng bộ của
>>> x = 1
>>> eval('x+1')
2
0 tích hợp và hoạt động tương tự.(x)

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

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
15 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
16 được nâng lên.

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

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

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

Trả về

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
12 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
19. Tương đương với:

________ 120 (đối tượng) ¶([x])

Như

>>> x = 1
>>> eval('x+1')
2
9, 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
>>> x = 1
>>> eval('x+1')
2
9 bằng cách sử dụng
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
23,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
24 hoặc
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
25 thoát ra. Điều này tạo ra một chuỗi tương tự như được trả về bởi
>>> x = 1
>>> eval('x+1')
2
9 trong Python 2.truth testing procedure. If x is false or omitted, this returns
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
19; otherwise, it returns
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
12. The
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
36 class is a subclass of
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
28 (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
19 and
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
12 (see Boolean Values).

________ 127 (x)x is now a positional-only parameter.

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
28, 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
29 trả về một số nguyên. Vài ví dụ:(*args, **kws)

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.

Xem thê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']
9 để biết thêm thông tin.auditing event
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
51 with argument
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
52.

Lớp ________ 131 ([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
12 hoặc
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
19. 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
19; Nếu không, nó trả về
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
12. Lớp
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
36 là một lớp con của
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
28 (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
19 và
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
12 (xem các giá trị Boolean).([source[, encoding[, errors]]])

Đã thay đổi trong phiên bản 3.7: x hiện là tham số chỉ có vị trí.Mutable Sequence Types, as well as most methods that the

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
55 type has, see Bytes and Bytearray Operations.

________ 140 (*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
    
    41, vượt qua
    def any(iterable):
        for element in iterable:
            if element:
                return True
        return False
    
    42 và
    def any(iterable):
        for element in iterable:
            if element:
                return True
        return False
    
    43 trực tiếp. Theo mặc định,
    def any(iterable):
        for element in iterable:
            if element:
                return True
        return False
    
    41 gọi
    def any(iterable):
        for element in iterable:
            if element:
                return True
        return False
    
    45 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
    
    46 hoặc gõ 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
    
    41 có thể được đặt thành một số chức năng khác và
    class C:
        @classmethod
        def f(cls, arg1, arg2): ...
    
    2 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
    
    41 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
    
    50.

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

    def any(iterable):
        for element in iterable:
            if element:
                return True
        return False
    
    51 với đối số
    def any(iterable):
        for element in iterable:
            if element:
                return True
        return False
    
    52.

  • Mới trong phiên bản 3.7.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 có thể đi đượ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
    
    58, đượ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 ________ 159 ([Nguồn [, mã hóa [, lỗi]]]))([source[, encoding[, errors]]])

Trả về một đối tượng mới của By 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
58.
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
55 là phiên bản bất biến của
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
54-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

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

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.

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

Trả về

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
12 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
19 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
12, 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
19, đố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
69.

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.

________ 170 (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
71 Trả về chuỗi
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
72, trong khi
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
73 trả về chuỗi
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
74. Đây là nghịch đảo của
>>> x = 1
>>> eval('x+1')
2
4.

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
76 sẽ được nâng lên nếu tôi ở ngoài phạm vi đó.

________ 177 ________ 178¶

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
79 là một trình trang trí chức năng - xem đị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
80) 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
81). 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

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
6 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ư

>>> x = 1
>>> eval('x+1')
2
7.Class methods can now wrap other descriptors such as
>>> x = 1
>>> eval('x+1')
2
7.

Đã 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
84,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
85,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
86,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
87 và
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
88) và có thuộc tính
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
89 mới.Class methods now inherit the method attributes (
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
84,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
85,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
86,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
87 and
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
88) and have a new
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
89 attribute.

________ 190 (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

>>> 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 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']
5. 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
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
93 để 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 (

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
94 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à

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
95 nếu nguồn bao gồm một chuỗi các câu lệnh,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
96 nếu nó bao gồm một biểu thức duy nhất hoặc
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
97 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 so với
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
98 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

class C:
    @classmethod
    def f(cls, arg1, arg2): ...
8. 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
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
8. 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'
00 trên thể hiện
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
01 trong mô -đun
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
02. Cờ trình biên dịch có thể được tìm thấy trong mô -đun
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
93, với tiền tố
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
04.Compiler flags can be found in
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
93 module, with
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
04 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'
05 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'
06. Các mức rõ ràng là
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
07 (không tối ưu hóa;
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
08 là đúng),
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
09 (khẳng định được loại bỏ,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
08 là sai) hoặc
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
11 (DocStrings cũng bị loại bỏ).

Hàm này tăng

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
12 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
76 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'
14.

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

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
15 với các đối số
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
16 và
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
17. 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'
15 with arguments
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
16 and
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
17. 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 trong chế độ

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
97 hoặc
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
96, đầ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'
20.

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 ở chế độ

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
95 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
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
95 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'
22 đã được nâng lên khi gặp phải các byte null trong nguồn.Previously,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
22 was raised when null bytes were encountered in source.

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

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
23 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'
24,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
25 và
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
26.
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
23 can now be passed in flags to enable support for top-level
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
24,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
25, and
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
26.

Lớp ________ 227 ([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
28 và
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
29. Nếu cả hai đối số bị bỏ qua, trả về
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
30.

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

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
31,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
32 đại biểu cho
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
33. Nếu
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
34 không được xác định thì nó sẽ trở lại
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
35. Nếu
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
35 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
29.

Ghi chú

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

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
97 hoặc
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
96, đầ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'
20.

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 ở chế độ

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
95 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
29 if
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
34 and
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
35 are not defined.

Đã thay đổi trong phiên bản 3.5: Trước đây,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
22 đã đượ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'
23 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'
24,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
25 và
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
26.

Lớp ________ 227 ([real [, hình ảnh]]) ¶(**kwarg) class
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
51(mapping, **kwarg) class
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
51(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
28 và
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
29. Nếu cả hai đối số bị bỏ qua, trả về
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
30.Mapping Types — dict for documentation about this class.

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

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
31,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
32 đại biểu cho
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
33. Nếu
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
34 không được xác định thì nó sẽ trở lại
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
35. Nếu
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
35 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
29.

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'
38 hoặc
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
39. Ví dụ,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
40 là tốt, nhưng
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
41 tăng
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
76.([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
29 nếu
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
34 và
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
35 không được xác định.

________ 246 (đối tượng, tên)

  • Đây là một người thân của

    sign           ::=  "+" | "-"
    infinity       ::=  "Infinity" | "inf"
    nan            ::=  "nan"
    numeric_value  ::=  floatnumber | infinity | nan
    numeric_string ::=  [sign] numeric_value
    
    3. Các đối số là một đối tượng và một chuỗi. Chuỗi phải là tên của một trong các thuộc tính của đối tượng. Hàm xóa thuộc tính được đặt tên, với điều kiện đối tượng cho phép nó. Ví dụ,
    >>> bin(3)
    '0b11'
    >>> bin(-10)
    '-0b1010'
    
    48 tương đương với
    >>> bin(3)
    '0b11'
    >>> bin(-10)
    '-0b1010'
    
    49. Tên không cần phải là một định danh Python (xem
    sign           ::=  "+" | "-"
    infinity       ::=  "Infinity" | "inf"
    nan            ::=  "nan"
    numeric_value  ::=  floatnumber | infinity | nan
    numeric_string ::=  [sign] numeric_value
    
    3).

  • Lớp ________ 251 (** kwarg) Lớp ________ 251 (ánh xạ, ** kwarg) Lớp ________ 251 (có thể sử dụng được, ** kwarg)

  • 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']
2 đượ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 nó 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.

________ 270 (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'
71. Đối với số điểm nổi, kết quả là
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
72, trong đó q thường là
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
73 nhưng có thể ít hơn 1. Trong mọi trường hợp
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
74 rất gần với A, nếu
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
75 không khác, nó có cùng dấu hiệu với B và
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
76.

________ 277 (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'
78 của trình lặp được trả về 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']
4 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'
78 method of the iterator returned by
>>> 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 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

________ 280 (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'
81, một tham chiếu đến từ điển của mô-đun tích hợp
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
82 đượ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'
81 của riêng bạn vào các thế giới trước khi chuyển nó 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']
5. 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
>>> 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 đượ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

class C:
    @classmethod
    def f(cls, arg1, arg2): ...
8). 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
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
95 là đối số chế độ, giá trị trả về của ____ 55 sẽ là
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
98.

Gợi ý: Thực hiện động của các câu lệnh được hỗ trợ bởi 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']
6. 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')]
2 và
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
5 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
>>> 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 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']
6.

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

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
95 để 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 chữ.

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

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
96 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
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
96 with the code object as the argument. Code compilation events may also be raised.

________ 297 (Đối tượng [, 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

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
98,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
99 và
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
00 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 truyền cho 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']
6. Giá trị trả về là
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
98.File input in the Reference Manual). Be aware that the
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
98,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
99, and
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
00 statements may not be used outside of function definitions even within the context of code passed to 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']
6 function. The return value is
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
98.

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'
81, một tham chiếu đến từ điển của mô-đun tích hợp
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
82 được chèn dưới phím đó. 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'
81 của riêng bạn vào các thế giới trước khi chuyển nó 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']
6.

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

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
96 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
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
96 with the code object as the argument. Code compilation events may also be raised.

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')]
2 và
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
5 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
>>> 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.

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')]
2 và
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
5 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
>>> 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.

Người dân địa phương mặc định hoạt động như được mô tả cho chức năng
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
5 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
>>> 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 trả về.(function, iterable)

________ 313 (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à

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
98, hàm nhận dạng được giả định, nghĩa là tất cả các yếu tố của có thể sai được được xóa.

Lưu ý rằng

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
15 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')
16 nếu hàm không phải là
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
98 và
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
18 nếu hàm là
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
98.

Xem
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
20 để biết chức năng bổ sung trả về các phần tử có thể lặp lại cho chức năng nào trả về sai.([x])

Lớp ________ 321 ([x])

Trả về một số điểm nổi được xây dựng từ một số hoặc chuỗi x.

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

Nếu đối số là một chuỗi, nó phải chứa một số thập phân, tùy chọn đi trước một dấu hiệu và được nhúng tùy ý trong khoảng trắng. Dấu hiệu tùy chọn có thể là

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
22 hoặc
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
23; Dấu hiệu
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
22 không ảnh hưởng đến giá trị được tạo ra. Đối số cũng có thể là một chuỗi đại diện cho một NAN (không phải là một số) hoặc vô cùng tích cực hoặc tiêu cực. Chính xác hơn, đầu vào phải phù hợp với ngữ pháp sau sau khi dẫn đầu và dấu hiệu các ký tự khoảng trắng bị loại bỏ:Floating point literals. Case is not significant, so, for example, “inf”, “Inf”, “INFINITY”, and “iNfINity” are all acceptable spellings for positive infinity.

Ở đây

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
25 là hình thức của một điểm nổi của Python, được mô tả trong các chữ nổi. Trường hợp này không đáng kể, vì vậy, ví dụ,, Inf Inf, Inf Inf, Infinity, và Infinity, tất cả đều là những cách viết có thể chấp nhận được cho vô cùng tích cực.

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 đối số 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')
26 sẽ được nâng lên.

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

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
31,
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
28 đại biểu cho
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
29. Nếu
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
35 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
29.

Examples:

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

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')
32 sẽ được trả về.Numeric Types — int, float, complex.

Loại phao được mô tả theo loại số - int, float, phức tạp.Grouping digits with underscores as in code literals is allowed.

Đã 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.x is now a positional-only parameter.

Đã thay đổi trong phiên bản 3.7: x hiện là tham số chỉ có vị trí.Falls back to

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
29 if
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
35 is not defined.

Đã 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
29 nếu
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
35 không được xác định.(value[, format_spec])

________ 335 (giá trị [, format_spec]) ¶Format Specification Mini-Language.

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ỏ.

Đị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')
36.

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

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
37 được dịch thành
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
38, 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')
39. Ngoại lệ
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
22 đượ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')
41 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.
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
42 raises
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
22 if format_spec is not an empty string.

Đã 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')
42 tăng
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
22 nếu format_spec không phải là một chuỗi trống.([iterable])

Lớp ________ 344 ([itable])Set Types — set, frozenset for documentation about this class.

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

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
45 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')
45 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')
45 và SET - Đặt, Frozenset cho tài liệu về lớp này.

Đối với các thùng chứa khác, hãy xem các lớp
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
57,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
56,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
58 và
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
54 tích hợp, cũng như mô-đun
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
59.(object, name[, default])

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

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 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')]
1.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')]
1.

________ 359 ()()

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.

________ 360 (đố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
12 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
19 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')
63 và xem liệu nó có tăng
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
56 hay không.)

________ 365 (đố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ú

Đối với các đối tượng có các phương thức tùy chỉnh

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
66, lưu ý rằ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')]
4 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')
66 để biết chi tiết.

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

Gọi hệ thống trợ giúp tích hợp. (Hàm này được dự định để sử dụng tương tác.) Nếu không có đối số nào được đưa ra, hệ thống trợ giúp tương tác bắt đầu trên bảng điều khiển phiên dịch. Nếu đối số là một chuỗi, thì chuỗi được tra cứu là tên của một mô -đun, hàm, lớp, phương thức, từ khóa hoặc chủ đề tài liệu và trang trợ giúp được in trên bảng điều khiển. Nếu đối số là bất kỳ loại đối tượng nào khác, một trang trợ giúp trên đối tượng được tạo.

Lưu ý rằng nếu một dấu gạch chéo (/) xuất hiện trong danh sách tham số của một hàm khi gọ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')]
5, điều đó có nghĩa là các tham số trước khi chém chỉ có vị trí. Để biết thêm thông tin, hãy xem mục FAQ trên các tham số chỉ có vị trí.the FAQ entry on positional-only parameters.

Hàm này được thêm vào không gian tên tích hợp bởi mô-đun

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

Đã thay đổi trong phiên bản 3.4: Thay đổi thành

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
72 và
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
73 có nghĩa là các chữ ký được báo cáo cho các thiết bị gọi hiện đang toàn diện và nhất quán hơn.Changes to
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
72 and
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
73 mean that the reported signatures for callables are now more comprehensive and consistent.

________ 374 (x)(x)

Chuyển đổi một số nguyên thành chuỗi hexadecimal viết thường được đặt trước với số 0x 0x. 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
28, 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
29 trả về một số nguyên. Vài ví dụ:

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

>>> 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 để biết thêm thông tin.

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')]
9 để 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 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')
79.

________ 380 (đố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ị

>>> 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.

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')
82 với đối số
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
83.auditing event
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
82 with argument
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
83.

________ 384 ([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')
85 đượ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')
86 được tải, thì
>>> 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 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')
88 với đối số
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
89 trước khi đọc đầu vàoauditing event
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
88 with argument
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
89 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')
90 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')
90 with the result after successfully reading input.

Lớp ________ 391 ([x]) ¶ Lớp ________ 391 (x, cơ sở = 10)([x])¶ class
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
91(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'
07 nếu không có đối số nào được đưa ra. Nếu X Xác định
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
94,
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
95 trả về
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
96. Nếu X Xác định
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
29, nó sẽ trả về
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
98. Nếu X Xác định
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
99, nó sẽ trả về
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
00. Đố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
55 hoặc
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
54 thể hiện một chữ số nguyên trong cơ sở radix. Tùy chọn, nghĩa đen có thể được đi trước bởi
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
38 hoặc
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
39 (không có không gian ở giữa) và được bao quanh bởi khoảng trắng. Một bả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): ...
05 đến
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
06 (hoặc
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
07 đến
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
08) 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 đặt trước tùy ý với ________ 409/____ 410, ________ 411/________ 412 hoặc ________ 413/________ 414, 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): ...
15 không hợp pháp, trong khi
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
16, cũng như
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
17.integer literal in radix base. Optionally, the literal can be preceded by
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
38 or
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
39 (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): ...
05 to
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
06 (or
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
07 to
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
08) 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): ...
09/
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
10,
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
11/
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
12, or
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
13/
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
14, 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): ...
15 is not legal, while
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
16 is, as well as
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
17.

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
28 và đối tượng cơ sở có phương thức
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
19, phương thức đó đượ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): ...
20 thay vì
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
19.If base is not an instance of
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
28 and the base object has a
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
19 method, that method is called to obtain an integer for the base. Previous versions used
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
20 instead of
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
19.

Đã 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
29 nếu
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
94 không được xác định.Falls back to
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
29 if
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
94 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
28 Đầ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
76 đượ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
28 hoặc khi chuyển đổi
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
28 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
28 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
76 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
28 or when converting an
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
28 into a string would exceed the limit. See the integer string conversion length limitation documentation.

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

Trả về

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
12 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
19. 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
12 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 của các loại và các bộ dữ liệu như vậy, một ngoại lệ
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
22 đượ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
19. 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
12 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'
22 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.

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

Trả về

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
12 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) 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
12 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'
22 đượ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
12 if class is a subclass of any entry in classinfo. In any other case, a
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
22 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.

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

Trả về

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
12 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) 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
12 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'
22 được nâng lên.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): ...
38 method), or it must support the sequence protocol (the
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
39 method with integer arguments starting at
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
07). If it does not support either of those protocols,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
22 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'
78 method; if the value returned is equal to sentinel,
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
43 will be raised, otherwise the value will be returned.

________ 437 (đối tượng [, sentinel]) ¶Iterator Types.

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 pháp

class C:
    @classmethod
    def f(cls, arg1, arg2): ...
38) 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): ...
39 với các đối số số nguyên bắt đầu từ
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
07). Nếu nó không hỗ trợ một trong hai giao thức đó,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
22 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'
78 của nó; Nếu giá trị được trả về bằng Sentinel,
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
43 sẽ được nâng lên, nếu không giá trị sẽ được trả về.

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

Xem thêm các loại Iterator.(s)

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
2 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:

________ 445 (s)([iterable])

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).Lists and Sequence Types — list, tuple, range.

Lớp ________ 446 ([itable])()

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

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
56 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.

________ 448 ()

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

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
5 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,
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
5 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')]
2 là cùng một từ điển.

Ghi chú(function, iterable, ...)

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.

________ 452 (chức năng, có thể lặp lại, ...) ¶(iterable, *[, key, default])
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
54(arg1, arg2, *args[, key])

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): ...
53.

________ 454 (itable, *[, key, mặc định])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): ...
56. Đố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
76 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): ...
58 và
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
59.

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à

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
98.The key can be
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
98.

Lớp ________ 461 (đố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.

________ 462 (itable, *[, key, mặc định]) ________ 462 (Arg1, arg2, *args [, key])(iterable, *[, key, default])
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
62(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): ...
56. Đố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
76 sẽ được nâng lên.

Nếu nhiều mục là tối thiểu, hàm 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): ...
66 và
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
67.

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à

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
98.The key can be
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
98.

Lớp ________ 461 (đố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'
78 method. If default is given, it is returned if the iterator is exhausted, otherwise
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
43 is raised.

________ 462 (itable, *[, key, mặc định]) ________ 462 (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ề.

Nếu nhiều mục là tối thiểu, hàm 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): ...
66 và
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
67.

________ 469 (iterator [, mặc định]) ¶(x)

Lấy mục tiếp theo từ trình lặp bằng cách gọi phương thức

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
78 của nó. 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
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
43 được nâng lên.

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

Lớp ________ 472¶

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

Trả lại một đối tượng không đặc biệt mới.

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
41 là một cơ sở cho tất cả các lớp. Nó có các phương pháp phổ biến cho tất cả các trường hợp của các lớp Python. Hàm này không chấp nhận bất kỳ đối số.

Ghi chú(file, mode='r', buffering=- 1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
41 không có
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
66, 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')
41.file object. If the file cannot be opened, an
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
82 is raised. See Reading and Writing Files for more examples of how to use this function.

________ 477 (x)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
19.)

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
28, 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
29 trả về một số nguyên. Ví dụ:

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ố hay không, hoặc không, bạn có thể sử dụng một trong những cách sau.

Xem thê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']
9 để biết thêm thông tin.

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

________ 481 (tệp, mode = 'r', buffering =- 1, mã hóa = không, lỗi = none, newline = none

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

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): ...
82 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.

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

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. .

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

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): ...
84 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): ...
85 để viết (cắt ngắn tệp nếu nó đã tồn tại),
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
86 để tạo độc quyền và
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
72 để 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): ...
88 được gọi để có được mã hóa ngôn ngữ hiện tại. .

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

Tính cách

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

Nghĩa

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

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

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

Mở để tạo độc quyền, không thành công nếu tệp đã tồn tạiOverview, Python distinguishes between binary and text I/O. Files opened in binary mode (including

class C:
    @classmethod
    def f(cls, arg1, arg2): ...
93 in the mode argument) return contents as
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
55 objects without any decoding. In text mode (the default, or when
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
94 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']
05, 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']
06, 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']
07 (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']
08) 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']
07, 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']
10 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']
11. 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']
    
    12. 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']
    
    13 trả về
    def any(iterable):
        for element in iterable:
            if element:
                return True
        return False
    
    12) 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']
15 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']
16 để 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']
16 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']
17 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']
17 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']
    
    18 để tăng ngoại lệ
    def any(iterable):
        for element in iterable:
            if element:
                return True
        return False
    
    76 nếu có lỗi mã hóa. Giá trị mặc định của
    def any(iterable):
        for element in iterable:
            if element:
                return True
        return False
    
    98 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']
    
    21 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']
    
    22 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']
    
    23) đượ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']
    
    24 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']
    
    25 đượ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']
    
    26 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']
    
    27.

  • >>> 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 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']
    
    29 (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']
    
    30.

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à

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
98,
>>> 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,
>>> 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,
>>> 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 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']
35. Nó hoạt động như sau:

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

    def any(iterable):
        for element in iterable:
            if element:
                return True
        return False
    
    98, 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']
    
    33,
    >>> 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 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']
    
    35 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']
    
    33 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']
    
    32, 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à

    def any(iterable):
        for element in iterable:
            if element:
                return True
        return False
    
    98, 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']
    
    33 nào được viết thành phân tá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']
    
    44. 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']
    
    32 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']
    
    33, 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']
    
    33 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
19 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
12 (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']
50 dưới dạng công cụ mở dẫn đến chức năng tương tự như truyền
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
98).

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']
52 để 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']
52 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

>>> x = 1
>>> eval('x+1')
2
3 phụ thuộc vào chế độ. Khi
>>> x = 1
>>> eval('x+1')
2
3 được sử dụng để mở một tệp ở chế độ văn bản (
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
85,
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
84,
>>> 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,
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
97, 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']
59 (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']
60). 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']
61. 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']
62; Trong Viết nhị phân và nối 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']
63 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']
64. 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']
65,
>>> 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 trả về.file object returned by the
>>> x = 1
>>> eval('x+1')
2
3 function depends on the mode. When
>>> x = 1
>>> eval('x+1')
2
3 is used to open a file in a text mode (
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
85,
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
84,
>>> 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,
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
97, 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']
59 (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']
60). 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']
61. 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']
62; 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']
63, 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']
64. 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']
65,
>>> 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, 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']
67,
>>> 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 (trong đó
>>> x = 1
>>> eval('x+1')
2
3 đượ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']
70,
>>> 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,
>>> 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 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']
73.

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']
74 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']
75,
>>> 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']
76,
>>> 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.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']
74 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']
75,
>>> 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']
76,
>>> 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.

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']
76 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']
77 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): ...
    
    86 đã đượ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']
    
    81 từng được nâng lên, bây giờ nó là bí danh của
    class C:
        @classmethod
        def f(cls, arg1, arg2): ...
    
    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 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): ...
    
    86) đã 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']
06.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']
06 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']
    
    86 (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']
    
    29 đã đượ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']
    
    88.

  • 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']
    
    65 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']
    
    66.

________ 591 (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']
92 trả về số nguyê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']
93 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']
94 (dấu hiệu Euro) 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']
95. Đây là nghịch đảo của
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
6.

________ 597 (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

>>> 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']
98). Mẫu hai đố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']
99 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')]
00.

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
28, 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')]
02 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')]
03, 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')]
04 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')]
05. Đố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
28 hoặc
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
29 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')]
08 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')]
09.

Đối với

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
28 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')]
11 đượ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')]
12 modulo
>>> 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:

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
28, 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')]
15 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
28 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')]
15 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.

________ 616 (*đố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ư

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
7 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à
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
98, 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,
>>> x = 1
>>> eval('x+1')
2
6 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')]
20; Nếu nó không có mặt hoặc
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
98,
>>> 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 sẽ được sử dụng. Vì các đối số in được chuyển đổi thành chuỗi văn bản,
>>> x = 1
>>> eval('x+1')
2
6 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')]
24 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 ________ 625 (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'
31:

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')]
27 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')]
28 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')]
29 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

>>> x = 1
>>> eval('x+1')
2
7 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')]
31 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')]
32 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')]
33,
>>> 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 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')]
35 có thể sử dụng làm người 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'
31 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')]
37,
>>> 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 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')]
39 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 ________ 640 (Dừng) Lớp ________ 640 (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')]
40(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')]
42 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.

________ 643 (đố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 truyền đế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']
5; 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')]
45. 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')]
46 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
50.

________ 648 (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')]
49 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')]
50 và phương thức
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
39 với các đối số số nguyên bắt đầu từ
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
07).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')]
49 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')]
50 method and the
class C:
    @classmethod
    def f(cls, arg1, arg2): ...
39 method with integer arguments starting at
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
07).

________ 653 (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à

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
98, 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
1, 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')]
56 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')]
57 là
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
07 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')]
59 là
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
11). 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
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
98. 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')]
62,
>>> 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 đạ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')]
64.

Ghi chú

Hành vi của

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
1 đố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')]
66 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')]
67 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')]
68. Đâ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 ________ 669 ([itable])([iterable])

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

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
57 mới, tùy chọn với các phần tử được lấy từ ITable.
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
57 là một lớp tích hợp. Xem các loại
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
57 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')
45,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
56,
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
58 và
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
54 tích hợp, cũng như mô-đun
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
59.

________ 678 (đố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')]
1. 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')]
80 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')]
81.

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

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
63 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')]
83. 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')]
1, v.v.Identifiers and keywords unless the object chooses to enforce that, for example in a custom
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
63 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')]
83. 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')]
1 etc..

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) để đặt nó với

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
3.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
sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
3.

Lớp ________ 686 (Dừng) Lớp ____ ____ 686 (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')]
86(start, stop[, step])

Trả về một đối tượng lát cắt biểu thị tập hợp các chỉ số được chỉ định 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')]
88. Các đối số bắt đầu và bước mặc định là
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
98. Các đối tượng lát cắt có các thuộc tính dữ liệu chỉ đọ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')]
90,
>>> 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')]
91 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')]
92 chỉ trả về các giá trị đối số (hoặc mặc định của chúng). Họ không có chức năng rõ ràng khác; Tuy nhiên, chúng được sử dụng bởi Numpy và các gói bên thứ ba khác. Các đối tượng lát cắt cũng được tạo khi cú pháp lập chỉ mục mở rộng được sử dụng. 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')]
93 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')]
94. Xem
>>> 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 để biết phiên bản thay thế trả về trình lặp.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')]
88. The start and step arguments default to
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
98. 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')]
90,
>>> 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')]
91, 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')]
92 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:
>>> 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 or
>>> 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')]
94. See
>>> 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 for an alternate version that returns an iterator.

________ 696 (itable, /, *, key = none, lùi = sai) ¶(iterable, /, *, key=None, reverse=False)

Trả về một danh sách được sắp xếp mới từ các mục trong ITable.

Có hai đối số tùy chọn phải được chỉ định là đối số từ khóa.

Khóa chỉ định hàm của một đối số được sử dụng để trích xuất một khóa so sánh từ mỗi phần tử trong ITable (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')]
97). Giá trị mặc định là
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
98 (so sánh trực tiếp các phần tử).

Đảo ngược là một giá trị boolean. Nếu được đặt thành

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
12, thì các yếu tố danh sách được sắp xếp như thể mỗi so sánh được đảo ngược.

Sử dụng

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
00 để chuyển đổi chức năng CMP kiểu cũ thành chức năng chính.

Hàm

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
5 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
02 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
03 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ư
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
7 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
05.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
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
7 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
05 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.

________ 177 ________ 707¶

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
08 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
80) 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
81). 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
11).

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

class C:
    @classmethod
    def f(cls, arg1, arg2): ...
7 để 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
13 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
84,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
85,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
86,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
87 và
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
88), có thuộc tính
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
89 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
84,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
85,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
86,
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
87 and
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
88), have a new
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
89 attribute, and are now callable as regular functions.

Lớp ________ 720 (Object = '') Lớp ____ 720 (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
20(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']
05 của đối tượng. Xem
sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
7 để 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']
05 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.

________ 725 (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

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
8. 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
27. Để 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
28. Để 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
29.

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 ________ 730 ([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
31 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
32 và giá trị của loại là
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
33, thì
sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
9 tìm kiếm
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
35.

Thuộc tính

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
31 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')]
1 và
sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
9. 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
39 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
40 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,

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
9 còn 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

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
9 đượ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
43. Nó làm như vậy bằng cách thực hiện phương pháp
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
63 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 đó,
sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
9 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
46.

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

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
9 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

sign           ::=  "+" | "-"
infinity       ::=  "Infinity" | "inf"
nan            ::=  "nan"
numeric_value  ::=  floatnumber | infinity | nan
numeric_string ::=  [sign] numeric_value
9, hãy xem Hướng dẫn sử dụng Super ().

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

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

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
58 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 ________ 751 (Đối tượng) Lớp ____ 751 (tên, cơ sở, dict, ** kwds)(object)¶ class
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
51(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
53.

Hàm tích hợp

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
0 đượ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 năng động của tuyên bố

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
55. 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
85. 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
57; Nếu trống,
>>> format(14, '#b'), format(14, 'b')
('0b1110', '1110')
>>> f'{14:#b}', f'{14:b}'
('0b1110', '1110')
41, 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'
66. 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
60 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
61) 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
60 mà không ghi đè lên
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
63 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
60 which don’t override
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
63 may no longer use the one-argument form to get the type of an object.

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

Trả về thuộc tính

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
66 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'
66.

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

>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
66 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'
66 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
69 để 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
02 hoạt động như
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
5. 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'
22 đượ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'
66 (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')]
83).

________ 775 (*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
03 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 lại đối số.

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

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
03 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
03 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
79 hoặc bằng cách gói trong
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
56.

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
03 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
    
    03 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
    
    03 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
    
    84. Đầu ra của nó giống như
    def any(iterable):
        for element in iterable:
            if element:
                return True
        return False
    
    03 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
    
    76 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
    
    84, 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
    
    88.

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
03 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
    
    90. Đ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
    
    91 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
    
    91 đế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
    
    03 Kết hợp với toán tử
    def enumerate(sequence, start=0):
        n = start
        for elem in sequence:
            yield n, elem
            n += 1
    
    94 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ố

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

________ 796 (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

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
97. Nó có thể được thay thế (bằng cách nhập mô -đun
>>> bin(3)
'0b11'
>>> bin(-10)
'-0b1010'
82 và gán cho
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
99) để thay đổi ngữ nghĩa của câu lệnh
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
97, nhưng làm như vậy không được khuyến khích vì thường việc 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
04 cũng không được khuyến khích ủng hộ
>>> x = 1
>>> eval('x+1')
2
02.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
04 is also discouraged in favor of
>>> x = 1
>>> eval('x+1')
2
02.

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

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

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'
07 (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
04 (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
06, 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
07 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
08 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
04 trả về mô -đun Toplevel ở đây bởi vì đây là đối tượng được liên kết với một tên bằng câu lệnh
def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1
97.

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

>>> x = 1
>>> eval('x+1')
2
11 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
12 được trả về từ
def any(iterable):
    for element in iterable:
        if element:
            return True
    return False
04. 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
02.

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
15 hoặc
>>> x = 1
>>> eval('x+1')
2
16 đang được sử dụng, biến môi trường
>>> x = 1
>>> eval('x+1')
2
17 hiện đã bị bỏ qua.When the command line options
>>> x = 1
>>> eval('x+1')
2
15 or
>>> x = 1
>>> eval('x+1')
2
16 are being used, the environment variable
>>> x = 1
>>> eval('x+1')
2
17 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à đầu vào () là một

hàm input () nó là một hàm tích hợp.It is a built-in function.

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

Python có một bộ các chức năng tích hợp.... Python được xây dựng trong các chức năng ..

Đầu vào là một hàm hoặc từ khóa trong Python?

Không, đầu vào không phải là một từ khóa.Thay vào đó, nó là một chức năng tích hợp.. Instead, it is a built-in function.

Đầu vào có phải là một chức năng không?

Nói một cách đơn giản, đầu vào là những gì đi vào chức năng và đầu ra là những gì xuất phát từ chức năng.Biến đầu vào và biến đầu ra cũng được gọi là miền và phạm vi.Miền của hàm là tập hợp tất cả các giá trị sẽ hoạt động trong hàm, là đầu vào.the input is what goes into the function and the output is what comes out of the function. The input variable and output variable are also called the domain and the range. The domain of a function is the set of all values that will work in the function, which are the inputs.