Hướng dẫn what are the two types of string operators in python? - hai loại toán tử chuỗi trong python là gì?

Các phần sau đây mô tả các loại tiêu chuẩn được tích hợp vào thông dịch viên.

Các loại tích hợp chính là số, trình tự, ánh xạ, lớp, trường hợp và ngoại lệ.

Một số lớp thu thập là có thể thay đổi. Các phương pháp thêm, trừ hoặc sắp xếp lại các thành viên của họ và không trả lại một mục cụ thể, không bao giờ trả lại bản thân bộ sưu tập mà

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26.

Một số hoạt động được hỗ trợ bởi một số loại đối tượng; Cụ thể, thực tế, tất cả các đối tượng có thể được so sánh với sự bình đẳng, được kiểm tra giá trị sự thật và được chuyển đổi thành một chuỗi [với hàm

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
27 hoặc hàm
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
28 hơi khác nhau]. Hàm thứ hai được sử dụng ngầm khi một đối tượng được viết bởi hàm
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
29.

Kiểm tra giá trị sự thật

Bất kỳ đối tượng nào cũng có thể được kiểm tra giá trị sự thật, để sử dụng trong điều kiện

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
30 hoặc
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
31 hoặc như là hoạt động của các hoạt động Boolean bên dưới.

Theo mặc định, một đối tượng được coi là đúng trừ khi lớp của nó xác định phương thức

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
32 trả về
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33 hoặc phương thức
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
34 trả về số 0, khi được gọi với đối tượng. 1 Dưới đây là hầu hết các đối tượng tích hợp được coi là sai:

  • Các hằng số được xác định là sai:

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    26 và
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    33.

  • số không thuộc bất kỳ loại số nào:

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    37,
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    38,
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    39,
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    40,
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    41

  • Trình tự và bộ sưu tập trống:

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    42,
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    43,
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    44,
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    45,
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    46,
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    47

Các hoạt động và các chức năng tích hợp có kết quả Boolean luôn trả về

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
37 hoặc
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33 cho SAI và
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
50 hoặc
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51 cho đúng, trừ khi có quy định khác. .

Hoạt động Boolean -
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
53,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
52, ________ 156¶

Đây là các hoạt động Boolean, được đặt hàng theo ưu tiên tăng dần:

Hoạt động

Kết quả

Ghi chú

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
57

Nếu x là sai, thì y, khác x

[1]

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
58

Nếu x là sai, thì x, khác y

[2]

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
59

Nếu x là sai, thì

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51, khác
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33

[3]

Notes:

  1. Đây là một toán tử ngắn mạch, vì vậy nó chỉ đánh giá đối số thứ hai nếu câu thứ nhất là sai.

  2. Đây là một toán tử ngắn mạch, vì vậy nó chỉ đánh giá đối số thứ hai nếu câu thứ nhất là đúng.

  3. def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    56 có mức độ ưu tiên thấp hơn so với các toán tử phi Boolean, do đó
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    63 được hiểu là
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    64 và
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    65 là lỗi cú pháp.

So sánh lor

Có tám hoạt động so sánh trong Python. Tất cả đều có cùng mức độ ưu tiên [cao hơn so với các hoạt động của Boolean]. So sánh có thể được xích ý nghĩa; Ví dụ,

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
66 tương đương với
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
67, ngoại trừ y chỉ được đánh giá một lần [nhưng trong cả hai trường hợp Z không được đánh giá khi
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
68 được tìm thấy là sai].

Bảng này tóm tắt các hoạt động so sánh:

Hoạt động

Kết quả

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
69

Ghi chú

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
70

Nếu x là sai, thì y, khác x

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
71

Nếu x là sai, thì x, khác y

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
72

Nếu x là sai, thì

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51, khác
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
73

Đây là một toán tử ngắn mạch, vì vậy nó chỉ đánh giá đối số thứ hai nếu câu thứ nhất là sai.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
74

Đây là một toán tử ngắn mạch, vì vậy nó chỉ đánh giá đối số thứ hai nếu câu thứ nhất là đúng.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
75

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
56 có mức độ ưu tiên thấp hơn so với các toán tử phi Boolean, do đó
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
63 được hiểu là
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
64 và
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
65 là lỗi cú pháp.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
76

So sánh lor

Có tám hoạt động so sánh trong Python. Tất cả đều có cùng mức độ ưu tiên [cao hơn so với các hoạt động của Boolean]. So sánh có thể được xích ý nghĩa; Ví dụ,

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
66 tương đương với
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
67, ngoại trừ y chỉ được đánh giá một lần [nhưng trong cả hai trường hợp Z không được đánh giá khi
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
68 được tìm thấy là sai].

Bảng này tóm tắt các hoạt động so sánh:

Nghĩa

hoàn toàn ít hơn

nhỏ hơn hoặc bằngiterable or implement the

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
95 method.

lớn hơn lớn hơn

lớn hơn hoặc bằng

Các số được tạo bằng chữ số hoặc là kết quả của các hàm và toán tử tích hợp. Các số nguyên số nguyên chưa được trang trí [bao gồm hex, số bát phân và số nhị phân] mang lại số nguyên. Các chữ số có chứa một điểm thập phân hoặc số lượng dấu hiệu số mũ. Lắp đặt

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
05 hoặc
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
06 vào số chữ số mang lại một số tưởng tượng [một số phức với phần thực bằng không] mà bạn có thể thêm vào một số nguyên hoặc nổi để có được một số phức với các phần thực và tưởng tượng.

Python hoàn toàn hỗ trợ số học hỗn hợp: Khi một toán tử số học nhị phân có các toán hạng các loại số khác nhau, toán hạng với loại hẹp hơn của Hồi được mở rộng so với loại khác, trong đó số nguyên hẹp hơn so với điểm nổi, hẹp hơn so với phức tạp. Một so sánh giữa các số của các loại khác nhau hoạt động như thể các giá trị chính xác của các số đó đã được so sánh. 2

Các hàm tạo

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
07,
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
08 và
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
09 có thể được sử dụng để tạo ra số lượng thuộc loại cụ thể.

Tất cả các loại số [ngoại trừ phức tạp] hỗ trợ các hoạt động sau [để biết ưu tiên của các hoạt động, xem ưu tiên của nhà điều hành]:Operator precedence]:

Hoạt động

Kết quả

Ghi chú

Tài liệu đầy đủ

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
10

tổng của x và y

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
11

sự khác biệt của x và y

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
12

sản phẩm của x và y

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
13

chỉ số của x và y

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
14

chỉ số của x và y

[1]

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
15

Phần còn lại của

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
13

[2]

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
17

x phủ định

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
18

x Không thay đổi

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
19

giá trị tuyệt đối hoặc độ lớn của x

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
20

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
21

x chuyển đổi thành số nguyên

[3][6]

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
07

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
23

x chuyển đổi thành điểm nổi

[4][6]

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
08

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
25

Một số phức tạp với phần thực re, phần tưởng tượng IM. IM mặc định về 0.

[6]

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
09

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
27

Liên hợp số phức C

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
28

cặp

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
29

[2]

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
30

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
31

x đến sức mạnh y

[5]

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
32

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
33

x đến sức mạnh y

[5]

Notes:

  1. Cũng được gọi là bộ phận số nguyên. Giá trị kết quả là toàn bộ số nguyên, mặc dù loại kết quả không nhất thiết là int. Kết quả luôn được làm tròn về phía trừ vô cực:

    >>> n = 19
    >>> bin[n]
    '0b10011'
    >>> n.bit_count[]
    3
    >>> [-n].bit_count[]
    3
    
    34 là
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    37,
    >>> n = 19
    >>> bin[n]
    '0b10011'
    >>> n.bit_count[]
    3
    >>> [-n].bit_count[]
    3
    
    36 là
    >>> n = 19
    >>> bin[n]
    '0b10011'
    >>> n.bit_count[]
    3
    >>> [-n].bit_count[]
    3
    
    37,
    >>> n = 19
    >>> bin[n]
    '0b10011'
    >>> n.bit_count[]
    3
    >>> [-n].bit_count[]
    3
    
    38 là
    >>> n = 19
    >>> bin[n]
    '0b10011'
    >>> n.bit_count[]
    3
    >>> [-n].bit_count[]
    3
    
    37 và
    >>> n = 19
    >>> bin[n]
    '0b10011'
    >>> n.bit_count[]
    3
    >>> [-n].bit_count[]
    3
    
    40 là
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    37.

  2. Không cho các số phức. Thay vào đó, hãy chuyển đổi sang phao bằng cách sử dụng

    >>> n = 19
    >>> bin[n]
    '0b10011'
    >>> n.bit_count[]
    3
    >>> [-n].bit_count[]
    3
    
    20 nếu thích hợp.

  3. Chuyển đổi từ điểm nổi sang số nguyên có thể làm tròn hoặc cắt ngắn như trong c; Xem các chức năng

    >>> n = 19
    >>> bin[n]
    '0b10011'
    >>> n.bit_count[]
    3
    >>> [-n].bit_count[]
    3
    
    43 và
    >>> n = 19
    >>> bin[n]
    '0b10011'
    >>> n.bit_count[]
    3
    >>> [-n].bit_count[]
    3
    
    44 để biết chuyển đổi được xác định rõ.

  4. Float cũng chấp nhận các chuỗi, NAN NAN và và Inf Inf với một tiền tố tùy chọn,+hoặc hoặc--không phải là một số [NAN] và vô cùng tích cực hoặc tiêu cực.

  5. Python định nghĩa

    >>> n = 19
    >>> bin[n]
    '0b10011'
    >>> n.bit_count[]
    3
    >>> [-n].bit_count[]
    3
    
    45 và
    >>> n = 19
    >>> bin[n]
    '0b10011'
    >>> n.bit_count[]
    3
    >>> [-n].bit_count[]
    3
    
    46 là
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    50, như là phổ biến đối với các ngôn ngữ lập trình.

  6. Các chữ số được chấp nhận bao gồm các chữ số

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    37 đến
    >>> n = 19
    >>> bin[n]
    '0b10011'
    >>> n.bit_count[]
    3
    >>> [-n].bit_count[]
    3
    
    49 hoặc bất kỳ tương đương Unicode [điểm mã với thuộc tính
    >>> n = 19
    >>> bin[n]
    '0b10011'
    >>> n.bit_count[]
    3
    >>> [-n].bit_count[]
    3
    
    50].

    Xem //www.unicode.org/public/13.0.0/ucd/extracted/derivingnumerictype.txt để biết danh sách đầy đủ các điểm mã với thuộc tính

    >>> n = 19
    >>> bin[n]
    '0b10011'
    >>> n.bit_count[]
    3
    >>> [-n].bit_count[]
    3
    
    50.

Tất cả các loại

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
52 [
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
96 và
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
97] cũng bao gồm các hoạt động sau:

Hoạt động

Kết quả

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
55

Ghi chú

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
57

Tài liệu đầy đủ

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
58

tổng của x và y

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
60

sự khác biệt của x và y

sản phẩm của x và y

chỉ số của x và y

chỉ số của x và y

Phần còn lại của

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
13

x phủ định

Hoạt động

Kết quả

Ghi chú

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
67

Tài liệu đầy đủ

[4]

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
68

tổng của x và y

[4]

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
69

sự khác biệt của x và y

[4]

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
70

sản phẩm của x và y

[1][2]

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
71

chỉ số của x và y

[1][3]

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
72

chỉ số của x và y

Notes:

  1. Phần còn lại của

    >>> n = 19
    >>> bin[n]
    '0b10011'
    >>> n.bit_count[]
    3
    >>> [-n].bit_count[]
    3
    
    13

  2. x phủ định

  3. x Không thay đổi

  4. giá trị tuyệt đối hoặc độ lớn của x

x chuyển đổi thành số nguyên

Loại INT thực hiện lớp cơ sở trừu tượng

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
77. Ngoài ra, nó cung cấp thêm một vài phương pháp:abstract base class. In addition, it provides a few more methods:

________ 278 ________ 279 [][]

Trả về số lượng bit cần thiết để thể hiện số nguyên trong nhị phân, không bao gồm dấu hiệu và số không hàng đầu:

>>> n = -37
>>> bin[n]
'-0b100101'
>>> n.bit_length[]
6

Chính xác hơn, nếu

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
80 là khác nhau, thì
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
81 là số nguyên dương duy nhất
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
82 sao cho
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
83. Tương đương, khi
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
19 đủ nhỏ để có logarit tròn chính xác, sau đó
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
85. Nếu
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
80 bằng không, thì
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
81 trả về
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
37.

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

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6

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

________ 278 ________ 290 [][]

Trả về số lượng trong số biểu diễn nhị phân của giá trị tuyệt đối của số nguyên. Điều này còn được gọi là số lượng dân số. Thí dụ:

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3

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

def bit_count[self]:
    return bin[self].count["1"]

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

________ 278 ________ 290 [][length, byteorder, *, signed=False]

Trả về số lượng trong số biểu diễn nhị phân của giá trị tuyệt đối của số nguyên. Điều này còn được gọi là số lượng dân số. Thí dụ:

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'

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

________ 278 ________ 292 [length, byteorder, *, đã ký = false]

Trả về một mảng byte đại diện cho một số nguyên.

Số nguyên được biểu diễn bằng cách sử dụng các byte chiều dài. Một

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
93 được nâng lên nếu số nguyên không thể đại diện với số byte đã cho.

Đối số byteorder xác định thứ tự byte được sử dụng để thể hiện số nguyên. Nếu byteorder là
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
94, byte quan trọng nhất là ở đầu mảng byte. Nếu byteorder là
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
95, byte quan trọng nhất là ở cuối mảng byte. Để yêu cầu thứ tự byte gốc của hệ thống máy chủ, hãy sử dụng
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
96 làm giá trị đơn hàng byte.[bytes, byteorder, *, signed=False]

Đối số đã ký xác định xem hai bổ sung có thể được sử dụng để đại diện cho số nguyên hay không. Nếu đã ký là

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33 và số nguyên âm được đưa ra, một
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
93 sẽ được nâng lên. Giá trị mặc định cho đã ký là
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33.

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680

Mới trong phiên bản 3.2.bytes-like object or an iterable producing bytes.

________ 278 ________ 292 [length, byteorder, *, đã ký = false]

Trả về một mảng byte đại diện cho một số nguyên.

Số nguyên được biểu diễn bằng cách sử dụng các byte chiều dài. Một

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
93 được nâng lên nếu số nguyên không thể đại diện với số byte đã cho.

Đối số byteorder xác định thứ tự byte được sử dụng để thể hiện số nguyên. Nếu byteorder là
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
94, byte quan trọng nhất là ở đầu mảng byte. Nếu byteorder là
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
95, byte quan trọng nhất là ở cuối mảng byte. Để yêu cầu thứ tự byte gốc của hệ thống máy chủ, hãy sử dụng
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
96 làm giá trị đơn hàng byte.[]

Đối số đã ký xác định xem hai bổ sung có thể được sử dụng để đại diện cho số nguyên hay không. Nếu đã ký là

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33 và số nguyên âm được đưa ra, một
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
93 sẽ được nâng lên. Giá trị mặc định cho đã ký là
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33.

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

ClassMethod ________ 278 ________ 301 [byte, byteorder, *, đã ký = false] ¶

Trả về số nguyên được biểu thị bằng mảng byte đã cho.abstract base class. float also has the following additional methods.

Các byte đối số phải là một đối tượng giống như byte hoặc một byte sản xuất có thể lặp lại.[]

Đối số đã ký cho biết liệu hai bổ sung có thể được sử dụng để đại diện cho số nguyên hay không.

________ 278 ________ 306 [][]

Trả về một cặp số nguyên có tỷ lệ chính xác bằng số nguyên ban đầu và với mẫu số dương. Tỷ lệ số nguyên của số nguyên [số nguyên] luôn là số nguyên là tử số và

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
50 làm mẫu số.

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False

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

Phương pháp bổ sung trên Float¶[]

Loại phao thực hiện lớp cơ sở trừu tượng

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
52. Float cũng có các phương pháp bổ sung sau.

________ 309 ________ 306 [][s]

Trả về một cặp số nguyên có tỷ lệ chính xác bằng phao gốc và với mẫu số dương. Tăng

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
93 về tính không đồng thời và
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
73 trên Nans.

________ 309 ________ 314 []

Trả về

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51 Nếu thể hiện float là hữu hạn với giá trị tích phân và
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33 nếu không:

[sign] ['0x'] integer ['.' fraction] ['p' exponent]

Hai phương pháp hỗ trợ chuyển đổi đến và từ các chuỗi thập lục phân. Vì các phao Python sườn được lưu trữ bên trong dưới dạng số nhị phân, việc chuyển đổi một chiếc phao thành hoặc từ một chuỗi thập phân thường liên quan đến một lỗi làm tròn nhỏ. Ngược lại, chuỗi thập lục phân cho phép biểu diễn chính xác và đặc điểm kỹ thuật của các số điểm nổi. Điều này có thể hữu ích khi gỡ lỗi, và trong công việc số.

Lưu ý rằng số mũ được viết theo số thập phân chứ không phải là thập lục phân và nó mang lại sức mạnh cho 2 để nhân hệ số. Ví dụ: chuỗi thập lục phân

def bit_count[self]:
    return bin[self].count["1"]
35 đại diện cho số điểm nổi
def bit_count[self]:
    return bin[self].count["1"]
36 hoặc
def bit_count[self]:
    return bin[self].count["1"]
37:

>>> float.fromhex['0x3.a7p10']
3740.0

Áp dụng chuyển đổi ngược lại

def bit_count[self]:
    return bin[self].count["1"]
37 mang lại cho một chuỗi thập lục phân khác đại diện cho cùng một số:

>>> float.hex[3740.0]
'0x1.d380000000000p+11'

Băm các loại số

Đối với các số

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
80 và
def bit_count[self]:
    return bin[self].count["1"]
40, có thể thuộc các loại khác nhau, đó là một yêu cầu rằng
def bit_count[self]:
    return bin[self].count["1"]
41 bất cứ khi nào
def bit_count[self]:
    return bin[self].count["1"]
42 [xem tài liệu phương thức
def bit_count[self]:
    return bin[self].count["1"]
43 để biết thêm chi tiết]. Để dễ thực hiện và hiệu quả qua nhiều loại số [bao gồm
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
96,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
97,
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
04 và
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
03] băm của Python cho các loại số dựa trên một hàm toán học duy nhất được xác định cho bất kỳ số lượng hợp lý nào và do đó áp dụng cho tất cả
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
03, và tất cả các trường hợp hữu hạn của
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
97 và
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
04. Về cơ bản, chức năng này được đưa ra bằng cách giảm modulo
def bit_count[self]:
    return bin[self].count["1"]
52 cho số nguyên tố cố định
def bit_count[self]:
    return bin[self].count["1"]
52. Giá trị của
def bit_count[self]:
    return bin[self].count["1"]
52 được cung cấp cho Python dưới dạng thuộc tính
def bit_count[self]:
    return bin[self].count["1"]
55 của
def bit_count[self]:
    return bin[self].count["1"]
56.

Chi tiết triển khai CPYThon: Hiện tại, số nguyên tố được sử dụng là

def bit_count[self]:
    return bin[self].count["1"]
57 trên các máy có độ dài C 32 bit và
def bit_count[self]:
    return bin[self].count["1"]
58 trên các máy có độ dài C 64 bit.
Currently, the prime used is
def bit_count[self]:
    return bin[self].count["1"]
57 on machines with 32-bit C longs and
def bit_count[self]:
    return bin[self].count["1"]
58 on machines with 64-bit C longs.

Dưới đây là các quy tắc chi tiết:

  • Nếu

    def bit_count[self]:
        return bin[self].count["1"]
    
    59 là một số hợp lý không âm và
    def bit_count[self]:
        return bin[self].count["1"]
    
    60 không chia hết cho
    def bit_count[self]:
        return bin[self].count["1"]
    
    52, hãy xác định
    def bit_count[self]:
        return bin[self].count["1"]
    
    62 là
    def bit_count[self]:
        return bin[self].count["1"]
    
    63, trong đó
    def bit_count[self]:
        return bin[self].count["1"]
    
    64 cung cấp nghịch đảo của modulo
    def bit_count[self]:
        return bin[self].count["1"]
    
    60
    def bit_count[self]:
        return bin[self].count["1"]
    
    52.

  • Nếu

    def bit_count[self]:
        return bin[self].count["1"]
    
    59 là một số hợp lý không âm và
    def bit_count[self]:
        return bin[self].count["1"]
    
    60 là chia hết cho
    def bit_count[self]:
        return bin[self].count["1"]
    
    52 [nhưng
    def bit_count[self]:
        return bin[self].count["1"]
    
    70 thì không] thì
    def bit_count[self]:
        return bin[self].count["1"]
    
    60 không có modulo nghịch đảo
    def bit_count[self]:
        return bin[self].count["1"]
    
    52 và quy tắc trên không áp dụng; Trong trường hợp này xác định
    def bit_count[self]:
        return bin[self].count["1"]
    
    62 là giá trị không đổi
    def bit_count[self]:
        return bin[self].count["1"]
    
    74.

  • Nếu

    def bit_count[self]:
        return bin[self].count["1"]
    
    59 là số hợp lý âm định nghĩa
    def bit_count[self]:
        return bin[self].count["1"]
    
    62 là
    def bit_count[self]:
        return bin[self].count["1"]
    
    77. Nếu băm kết quả là
    >>> n = 19
    >>> bin[n]
    '0b10011'
    >>> n.bit_count[]
    3
    >>> [-n].bit_count[]
    3
    
    37, hãy thay thế nó bằng
    def bit_count[self]:
        return bin[self].count["1"]
    
    79.

  • Các giá trị cụ thể

    def bit_count[self]:
        return bin[self].count["1"]
    
    74 và
    def bit_count[self]:
        return bin[self].count["1"]
    
    81 được sử dụng làm giá trị băm cho vô cực dương hoặc vô cực âm [tương ứng].

  • Đối với số

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    98
    def bit_count[self]:
        return bin[self].count["1"]
    
    83, các giá trị băm của các phần thực và tưởng tượng được kết hợp bằng cách tính toán
    def bit_count[self]:
        return bin[self].count["1"]
    
    84, giảm modulo
    def bit_count[self]:
        return bin[self].count["1"]
    
    85 để nó nằm trong
    def bit_count[self]:
        return bin[self].count["1"]
    
    86. Một lần nữa, nếu kết quả là
    >>> n = 19
    >>> bin[n]
    '0b10011'
    >>> n.bit_count[]
    3
    >>> [-n].bit_count[]
    3
    
    37, nó đã thay thế bằng
    def bit_count[self]:
        return bin[self].count["1"]
    
    79.

Để làm rõ các quy tắc trên, ở đây, một số ví dụ mã Python, tương đương với hàm băm tích hợp, để tính toán hàm băm của một số hợp lý,

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
97 hoặc
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
98:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
0

Các loại Iterator

Python hỗ trợ một khái niệm về việc lặp lại trên các thùng chứa. Điều này được thực hiện bằng hai phương pháp riêng biệt; Chúng được sử dụng để cho phép các lớp do người dùng xác định hỗ trợ lặp lại. Trình tự, được mô tả dưới đây chi tiết hơn, luôn hỗ trợ các phương pháp lặp.

Một phương thức cần được xác định cho các đối tượng container để cung cấp hỗ trợ có thể điều chỉnh được:iterable support:

________ 391 ________ 392 [][]

Trả về một đối tượng lặp. Đối tượng được yêu cầu để hỗ trợ giao thức Iterator được mô tả dưới đây. Nếu một container hỗ trợ các loại lặp khác nhau, các phương thức bổ sung có thể được cung cấp để yêu cầu cụ thể các trình lặp cho các loại lặp đó. .iterator object. The object is required to support the iterator protocol described below. If a container supports different types of iteration, additional methods can be provided to specifically request iterators for those iteration types. [An example of an object supporting multiple forms of iteration would be a tree structure which supports both breadth-first and depth-first traversal.] This method corresponds to the

def bit_count[self]:
    return bin[self].count["1"]
93 slot of the type structure for Python objects in the Python/C API.

Bản thân các đối tượng lặp được yêu cầu hỗ trợ hai phương thức sau, cùng nhau tạo thành giao thức iterator:

________ 394 ________ 392 [][]

Trả về chính đối tượng Iterator. Điều này là bắt buộc để cho phép cả hai container và trình lặp được sử dụng với các câu lệnh

def bit_count[self]:
    return bin[self].count["1"]
96 và
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
93. Phương pháp này tương ứng với khe
def bit_count[self]:
    return bin[self].count["1"]
93 của cấu trúc loại cho các đối tượng Python trong API Python/C.iterator object itself. This is required to allow both containers and iterators to be used with the
def bit_count[self]:
    return bin[self].count["1"]
96 and
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
93 statements. This method corresponds to the
def bit_count[self]:
    return bin[self].count["1"]
93 slot of the type structure for Python objects in the Python/C API.

________ 394 ________ 400 [][]

Trả lại mục tiếp theo từ trình lặp. Nếu không có mục nào nữa, hãy nâng ngoại lệ

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
01. Phương pháp này tương ứng với khe
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
02 của cấu trúc loại cho các đối tượng Python trong API Python/C.iterator. If there are no further items, raise the
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
01 exception. This method corresponds to the
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
02 slot of the type structure for Python objects in the Python/C API.

Python xác định một số đối tượng iterator để hỗ trợ lần lặp qua các loại trình tự chung và cụ thể, từ điển và các hình thức chuyên dụng khác. Các loại cụ thể không quan trọng ngoài việc thực hiện giao thức Iterator.

Khi một phương thức Iterator từ

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
03 tăng
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
01, nó phải tiếp tục thực hiện như vậy trên các cuộc gọi tiếp theo. Việc triển khai không tuân theo tài sản này được coi là bị hỏng.

Loại máy phát

Các máy phát điện Python cung cấp một cách thuận tiện để thực hiện giao thức Iterator. Nếu một phương thức đối tượng container

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
05 được triển khai như một trình tạo, nó sẽ tự động trả về một đối tượng iterator [về mặt kỹ thuật, đối tượng trình tạo] cung cấp các phương thức
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
05 và
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
03. Thông tin thêm về máy phát điện có thể được tìm thấy trong tài liệu cho biểu thức năng suất.generators provide a convenient way to implement the iterator protocol. If a container object’s
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
05 method is implemented as a generator, it will automatically return an iterator object [technically, a generator object] supplying the
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
05 and
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
03 methods. More information about generators can be found in the documentation for the yield expression.

Các loại trình tự -
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
08,
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
09, ________ 410¶

Có ba loại trình tự cơ bản: danh sách, bộ dữ liệu và các đối tượng phạm vi. Các loại trình tự bổ sung được thiết kế để xử lý dữ liệu nhị phân và chuỗi văn bản được mô tả trong các phần chuyên dụng.binary data and text strings are described in dedicated sections.

Các hoạt động trình tự phổ biến

Các hoạt động trong bảng sau được hỗ trợ bởi hầu hết các loại trình tự, cả có thể thay đổi và bất biến.

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
11 ABC được cung cấp để giúp thực hiện chính xác các hoạt động này một cách chính xác trên các loại trình tự tùy chỉnh.

Bảng này liệt kê các hoạt động trình tự được sắp xếp theo mức độ ưu tiên tăng dần. Trong bảng, s và t là các chuỗi cùng loại, n, i, j và k là số nguyên và x là một đối tượng tùy ý đáp ứng bất kỳ loại hạn chế nào và giá trị do s.

Các hoạt động

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
93 và
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
94 có các ưu tiên tương tự như các hoạt động so sánh. Các hoạt động
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
65 [nối] và
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
15 [lặp lại] có mức độ ưu tiên tương tự như các hoạt động số tương ứng. 3

Hoạt động

Kết quả

Ghi chú

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
16

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51 Nếu một mục S bằng x, khác
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33

[1]

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
19

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33 Nếu một mục S bằng x, khác
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51

[1]

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
22

sự kết hợp của s và t

[6][7]

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
23 hoặc
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
24

tương đương với việc thêm s vào chính nó n lần

[2][7]

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
25

ith mặt hàng của s, gốc 0

[3]

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
26

cắt lát từ tôi đến j

[3][4]

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
27

cắt lát từ i đến j với bước k

[3][5]

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
28

chiều dài của s

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
29

Mục nhỏ nhất của s

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
30

mục lớn nhất của s

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
31

Chỉ số về lần xuất hiện đầu tiên của X trong S [AT hoặc After INDEX I và trước chỉ mục J]

[8]

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
32

tổng số lần xuất hiện của x trong s

Trình tự cùng loại cũng hỗ trợ so sánh. Cụ thể, các bộ dữ liệu và danh sách được so sánh từ vựng bằng cách so sánh các yếu tố tương ứng. Điều này có nghĩa là để so sánh bằng nhau, mọi phần tử phải so sánh bằng nhau và hai chuỗi phải cùng loại và có cùng độ dài. [Để biết chi tiết đầy đủ, hãy xem so sánh trong tham chiếu ngôn ngữ.]Comparisons in the language reference.]

Chuyển tiếp và đảo ngược các trình lặp qua các chuỗi có thể thay đổi truy cập các giá trị bằng cách sử dụng một chỉ mục. Chỉ số đó sẽ tiếp tục diễu hành về phía trước [hoặc lùi] ngay cả khi chuỗi cơ bản bị đột biến. Trình lặp chỉ chấm dứt khi gặp phải

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
33 hoặc
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
01 [hoặc khi chỉ mục giảm xuống dưới 0].

Notes:

  1. Mặc dù các hoạt động

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    93 và
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    94 chỉ được sử dụng để kiểm tra ngăn chặn đơn giản trong trường hợp chung, một số trình tự chuyên dụng [như
    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    37,
    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    38 và
    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    39] cũng sử dụng chúng để kiểm tra sau:

  2. Các giá trị N nhỏ hơn

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    37 được coi là
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    37 [mang lại một chuỗi trống cùng loại với S]. Lưu ý rằng các mục trong chuỗi s không được sao chép; Chúng được tham chiếu nhiều lần. Điều này thường ám ảnh các lập trình viên Python mới; xem xét:

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    1

    Điều đã xảy ra là

    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    42 là danh sách một phần tử chứa một danh sách trống, vì vậy cả ba yếu tố của
    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    43 đều tham khảo danh sách trống duy nhất này. Sửa đổi bất kỳ yếu tố nào của
    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    44 sửa đổi danh sách duy nhất này. Bạn có thể tạo một danh sách các danh sách khác nhau theo cách này:

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    2

    Giải thích thêm có sẵn trong mục FAQ Làm cách nào để tạo một danh sách đa chiều ?.How do I create a multidimensional list?.

  3. Nếu I hoặc J là âm, chỉ số có liên quan đến phần cuối của chuỗi S:

    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    45 hoặc
    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    46 được thay thế. Nhưng lưu ý rằng
    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    47 vẫn là
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    37.

  4. Các lát của S từ I đến J được định nghĩa là chuỗi các mục có chỉ mục k sao cho

    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    49. Nếu i hoặc j lớn hơn
    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    28, hãy sử dụng
    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    28. Nếu tôi bị bỏ qua hoặc
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    26, hãy sử dụng
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    37. Nếu J bị bỏ qua hoặc
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    26, hãy sử dụng
    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    28. Nếu tôi lớn hơn hoặc bằng J, lát cắt trống.

  5. Các lát của S từ I đến J với bước k được định nghĩa là chuỗi các mục có chỉ mục

    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    56 sao cho
    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    57. Nói cách khác, các chỉ số là
    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    58,
    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    59,
    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    60,
    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    61, v.v., dừng lại khi đạt được J [nhưng không bao giờ bao gồm J]. Khi K dương tính, tôi và J sẽ giảm xuống còn
    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    28 nếu chúng lớn hơn. Khi K âm, I và J bị giảm xuống còn
    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    63 nếu chúng lớn hơn. Nếu tôi hoặc J bị bỏ qua hoặc
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    26, chúng sẽ trở thành các giá trị cuối cùng của Google [kết thúc phụ thuộc vào dấu hiệu của K]. Lưu ý, K không thể bằng không. Nếu K là
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    26, nó được đối xử như
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    50.

  6. Các chuỗi bất biến luôn luôn dẫn đến một đối tượng mới. Điều này có nghĩa là việc xây dựng một chuỗi bằng cách kết hợp lặp đi lặp lại sẽ có chi phí thời gian chạy bậc hai trong tổng chiều dài trình tự. Để có được chi phí thời gian chạy tuyến tính, bạn phải chuyển sang một trong những lựa chọn thay thế dưới đây:

    • Nếu kết hợp các đối tượng

      >>> [1024].to_bytes[2, byteorder='big']
      b'\x04\x00'
      >>> [1024].to_bytes[10, byteorder='big']
      b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
      >>> [-1024].to_bytes[10, byteorder='big', signed=True]
      b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
      >>> x = 1000
      >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
      b'\xe8\x03'
      
      37, bạn có thể xây dựng một danh sách và sử dụng
      >>> [1024].to_bytes[2, byteorder='big']
      b'\x04\x00'
      >>> [1024].to_bytes[10, byteorder='big']
      b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
      >>> [-1024].to_bytes[10, byteorder='big', signed=True]
      b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
      >>> x = 1000
      >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
      b'\xe8\x03'
      
      68 ở cuối hoặc nếu không hãy ghi vào một ví dụ
      >>> [1024].to_bytes[2, byteorder='big']
      b'\x04\x00'
      >>> [1024].to_bytes[10, byteorder='big']
      b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
      >>> [-1024].to_bytes[10, byteorder='big', signed=True]
      b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
      >>> x = 1000
      >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
      b'\xe8\x03'
      
      69 và lấy lại giá trị của nó khi hoàn thành

    • Nếu nối các đối tượng

      >>> [1024].to_bytes[2, byteorder='big']
      b'\x04\x00'
      >>> [1024].to_bytes[10, byteorder='big']
      b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
      >>> [-1024].to_bytes[10, byteorder='big', signed=True]
      b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
      >>> x = 1000
      >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
      b'\xe8\x03'
      
      38, bạn có thể sử dụng tương tự
      >>> [1024].to_bytes[2, byteorder='big']
      b'\x04\x00'
      >>> [1024].to_bytes[10, byteorder='big']
      b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
      >>> [-1024].to_bytes[10, byteorder='big', signed=True]
      b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
      >>> x = 1000
      >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
      b'\xe8\x03'
      
      71 hoặc
      >>> [1024].to_bytes[2, byteorder='big']
      b'\x04\x00'
      >>> [1024].to_bytes[10, byteorder='big']
      b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
      >>> [-1024].to_bytes[10, byteorder='big', signed=True]
      b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
      >>> x = 1000
      >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
      b'\xe8\x03'
      
      72 hoặc bạn có thể thực hiện kết nối tại chỗ với đối tượng
      >>> [1024].to_bytes[2, byteorder='big']
      b'\x04\x00'
      >>> [1024].to_bytes[10, byteorder='big']
      b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
      >>> [-1024].to_bytes[10, byteorder='big', signed=True]
      b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
      >>> x = 1000
      >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
      b'\xe8\x03'
      
      39. Các đối tượng
      >>> [1024].to_bytes[2, byteorder='big']
      b'\x04\x00'
      >>> [1024].to_bytes[10, byteorder='big']
      b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
      >>> [-1024].to_bytes[10, byteorder='big', signed=True]
      b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
      >>> x = 1000
      >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
      b'\xe8\x03'
      
      39 có thể thay đổi và có cơ chế tổng thể hiệu quả

    • Nếu kết hợp các đối tượng

      >>> [1024].to_bytes[2, byteorder='big']
      b'\x04\x00'
      >>> [1024].to_bytes[10, byteorder='big']
      b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
      >>> [-1024].to_bytes[10, byteorder='big', signed=True]
      b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
      >>> x = 1000
      >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
      b'\xe8\x03'
      
      09, hãy mở rộng
      >>> [1024].to_bytes[2, byteorder='big']
      b'\x04\x00'
      >>> [1024].to_bytes[10, byteorder='big']
      b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
      >>> [-1024].to_bytes[10, byteorder='big', signed=True]
      b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
      >>> x = 1000
      >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
      b'\xe8\x03'
      
      08 thay thế

    • Đối với các loại khác, hãy điều tra các tài liệu lớp có liên quan

  7. Một số loại trình tự [chẳng hạn như

    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    10] chỉ hỗ trợ các chuỗi vật phẩm tuân theo các mẫu cụ thể và do đó don don hỗ trợ nối tiếp hoặc lặp lại trình tự.

  8. >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    78 tăng
    >>> n = 19
    >>> bin[n]
    '0b10011'
    >>> n.bit_count[]
    3
    >>> [-n].bit_count[]
    3
    
    73 khi X không được tìm thấy trong s. Không phải tất cả các triển khai hỗ trợ thông qua các đối số bổ sung i và j. Những đối số này cho phép tìm kiếm hiệu quả các tiểu mục của chuỗi. Truyền các đối số bổ sung gần tương đương với việc sử dụng
    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    80, chỉ không sao chép bất kỳ dữ liệu nào và với chỉ số được trả về liên quan đến việc bắt đầu chuỗi thay vì bắt đầu lát cắt.

Các loại trình tự bất biến Jor

Hoạt động duy nhất mà các loại trình tự bất biến thường thực hiện không được thực hiện bởi các loại chuỗi có thể thay đổi là hỗ trợ cho

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
81 tích hợp.

Hỗ trợ này cho phép các chuỗi bất biến, chẳng hạn như các trường hợp

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
09, được sử dụng làm khóa
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
83 và được lưu trữ trong các trường hợp
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
84 và
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
85.

Cố gắng băm một chuỗi bất biến chứa các giá trị không thể đo được sẽ dẫn đến

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
83.

Các loại trình tự có thể thay đổi

Các hoạt động trong bảng sau được xác định trên các loại chuỗi có thể thay đổi.

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
87 ABC được cung cấp để dễ dàng thực hiện chính xác các hoạt động này trên các loại trình tự tùy chỉnh.

Trong bảng S là một thể hiện của một loại chuỗi có thể thay đổi, T là bất kỳ đối tượng có thể lặp lại và X là một đối tượng tùy ý đáp ứng bất kỳ loại hạn chế nào và giá trị do S áp đặt [ví dụ,

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
39 chỉ chấp nhận số nguyên đáp ứng hạn chế giá trị
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
89].

Hoạt động

Kết quả

Ghi chú

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
90

Mục I của S được thay thế bởi x

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
91

Slice of s từ i đến j được thay thế bằng nội dung của tần số t có thể

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
92

Giống như

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
93

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
94

Các yếu tố của

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
27 được thay thế bằng các yếu tố của T

[1]

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
96

Xóa các yếu tố của

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
27 khỏi danh sách

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
98

nối X đến cuối chuỗi [giống như

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
99]

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
00

Xóa tất cả các mục khỏi S [giống như

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
01]

[5]

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
02

Tạo một bản sao nông của S [giống như

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
03]

[5]

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
04 hoặc
>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
05

Mở rộng s với nội dung của t [phần lớn giống như

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
06]

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
07

Cập nhật s với nội dung của nó lặp lại n lần

[6]

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
08

Chèn X vào S tại chỉ mục được đưa ra bởi I [giống như

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
09]

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
10 hoặc
>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
11

Lấy mục tại I và cũng loại bỏ nó khỏi S

[2]

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
12

Xóa mục đầu tiên khỏi s trong đó

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
25 bằng x

[3]

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
14

đảo ngược các vật phẩm của s tại chỗ

[4]

Notes:

  1. T phải có cùng chiều dài với lát cắt mà nó đang thay thế.

  2. Đối số tùy chọn I mặc định là

    >>> n = 19
    >>> bin[n]
    '0b10011'
    >>> n.bit_count[]
    3
    >>> [-n].bit_count[]
    3
    
    37, do đó theo mặc định, mục cuối cùng được xóa và trả về.

  3. >>> int.from_bytes[b'\x00\x10', byteorder='big']
    16
    >>> int.from_bytes[b'\x00\x10', byteorder='little']
    4096
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
    -1024
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
    64512
    >>> int.from_bytes[[255, 0, 0], byteorder='big']
    16711680
    
    16 tăng
    >>> n = 19
    >>> bin[n]
    '0b10011'
    >>> n.bit_count[]
    3
    >>> [-n].bit_count[]
    3
    
    73 khi X không được tìm thấy trong s.

  4. Phương pháp

    >>> int.from_bytes[b'\x00\x10', byteorder='big']
    16
    >>> int.from_bytes[b'\x00\x10', byteorder='little']
    4096
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
    -1024
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
    64512
    >>> int.from_bytes[[255, 0, 0], byteorder='big']
    16711680
    
    18 sửa đổi trình tự tại chỗ cho nền kinh tế của không gian khi đảo ngược một chuỗi lớn. Để nhắc nhở người dùng rằng nó hoạt động bằng hiệu ứng phụ, nó không trả về trình tự đảo ngược.

  5. >>> int.from_bytes[b'\x00\x10', byteorder='big']
    16
    >>> int.from_bytes[b'\x00\x10', byteorder='little']
    4096
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
    -1024
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
    64512
    >>> int.from_bytes[[255, 0, 0], byteorder='big']
    16711680
    
    19 và
    >>> int.from_bytes[b'\x00\x10', byteorder='big']
    16
    >>> int.from_bytes[b'\x00\x10', byteorder='little']
    4096
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
    -1024
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
    64512
    >>> int.from_bytes[[255, 0, 0], byteorder='big']
    16711680
    
    20 được bao gồm để thống nhất với các giao diện của các container có thể thay đổi mà không ủng hộ các hoạt động cắt lát [như
    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    83 và
    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    84].
    >>> int.from_bytes[b'\x00\x10', byteorder='big']
    16
    >>> int.from_bytes[b'\x00\x10', byteorder='little']
    4096
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
    -1024
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
    64512
    >>> int.from_bytes[[255, 0, 0], byteorder='big']
    16711680
    
    20 không phải là một phần của
    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    87 ABC, nhưng hầu hết các lớp trình tự đột biến cụ thể đều cung cấp nó.

    Mới trong Phiên bản 3.3:

    >>> int.from_bytes[b'\x00\x10', byteorder='big']
    16
    >>> int.from_bytes[b'\x00\x10', byteorder='little']
    4096
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
    -1024
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
    64512
    >>> int.from_bytes[[255, 0, 0], byteorder='big']
    16711680
    
    19 và
    >>> int.from_bytes[b'\x00\x10', byteorder='big']
    16
    >>> int.from_bytes[b'\x00\x10', byteorder='little']
    4096
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
    -1024
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
    64512
    >>> int.from_bytes[[255, 0, 0], byteorder='big']
    16711680
    
    20.
    >>> int.from_bytes[b'\x00\x10', byteorder='big']
    16
    >>> int.from_bytes[b'\x00\x10', byteorder='little']
    4096
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
    -1024
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
    64512
    >>> int.from_bytes[[255, 0, 0], byteorder='big']
    16711680
    
    19 and
    >>> int.from_bytes[b'\x00\x10', byteorder='big']
    16
    >>> int.from_bytes[b'\x00\x10', byteorder='little']
    4096
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
    -1024
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
    64512
    >>> int.from_bytes[[255, 0, 0], byteorder='big']
    16711680
    
    20 methods.

  6. Giá trị n là một số nguyên hoặc một đối tượng thực hiện

    >>> int.from_bytes[b'\x00\x10', byteorder='big']
    16
    >>> int.from_bytes[b'\x00\x10', byteorder='little']
    4096
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
    -1024
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
    64512
    >>> int.from_bytes[[255, 0, 0], byteorder='big']
    16711680
    
    27. Giá trị bằng không và âm của n xóa chuỗi. Các mục trong chuỗi không được sao chép; Chúng được tham chiếu nhiều lần, như được giải thích cho
    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    23 theo các hoạt động trình tự phổ biến.Common Sequence Operations.

Danh sách

Danh sách là các chuỗi có thể thay đổi, thường được sử dụng để lưu trữ các bộ sưu tập các mục đồng nhất [trong đó mức độ tương tự chính xác sẽ thay đổi theo ứng dụng].

Lớp ________ 529 [[itable]] ¶[[iterable]]

Danh sách có thể được xây dựng theo nhiều cách:

  • Sử dụng một cặp dấu ngoặc vuông để biểu thị danh sách trống:

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    44

  • Sử dụng dấu ngoặc vuông, tách các vật phẩm với dấu phẩy:

    >>> int.from_bytes[b'\x00\x10', byteorder='big']
    16
    >>> int.from_bytes[b'\x00\x10', byteorder='little']
    4096
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
    -1024
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
    64512
    >>> int.from_bytes[[255, 0, 0], byteorder='big']
    16711680
    
    31,
    >>> int.from_bytes[b'\x00\x10', byteorder='big']
    16
    >>> int.from_bytes[b'\x00\x10', byteorder='little']
    4096
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
    -1024
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
    64512
    >>> int.from_bytes[[255, 0, 0], byteorder='big']
    16711680
    
    32

  • Sử dụng danh sách hiểu biết:

    >>> int.from_bytes[b'\x00\x10', byteorder='big']
    16
    >>> int.from_bytes[b'\x00\x10', byteorder='little']
    4096
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
    -1024
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
    64512
    >>> int.from_bytes[[255, 0, 0], byteorder='big']
    16711680
    
    33

  • Sử dụng hàm tạo loại:

    >>> int.from_bytes[b'\x00\x10', byteorder='big']
    16
    >>> int.from_bytes[b'\x00\x10', byteorder='little']
    4096
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
    -1024
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
    64512
    >>> int.from_bytes[[255, 0, 0], byteorder='big']
    16711680
    
    34 hoặc
    >>> int.from_bytes[b'\x00\x10', byteorder='big']
    16
    >>> int.from_bytes[b'\x00\x10', byteorder='little']
    4096
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
    -1024
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
    64512
    >>> int.from_bytes[[255, 0, 0], byteorder='big']
    16711680
    
    35

Hàm tạo xây dựng một danh sách có các mục giống nhau và theo cùng thứ tự với các mục ITBER. Có thể lặp lại có thể là một chuỗi, một thùng chứa hỗ trợ lặp hoặc đối tượng lặp. Nếu có thể là một danh sách, một bản sao được tạo và trả lại, tương tự như

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
36. Ví dụ:
>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
37 trả về
>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
38 và
>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
39 trả về
>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
40. Nếu không có đối số nào được đưa ra, hàm tạo tạo một danh sách trống mới,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
44.

Nhiều hoạt động khác cũng tạo ra các danh sách, bao gồm cả

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
42 tích hợp.

Danh sách thực hiện tất cả các hoạt động trình tự chung và có thể thay đổi. Danh sách cũng cung cấp phương pháp bổ sung sau:common and mutable sequence operations. Lists also provide the following additional method:

________ 543 [*, key = none, lùi = sai] ¶[*, key=None, reverse=False]

Phương pháp này sắp xếp danh sách tại chỗ, chỉ sử dụng so sánh

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
69 giữa các mục. Các trường hợp ngoại lệ không bị triệt tiêu - nếu bất kỳ hoạt động so sánh nào thất bại, toàn bộ hoạt động sắp xếp sẽ thất bại [và danh sách có thể sẽ bị bỏ lại ở trạng thái sửa đổi một phần].

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
45 chấp nhận hai đối số chỉ có thể được truyền bằng từ khóa [đối số chỉ từ khóa]:keyword-only arguments]:

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ử danh sách [ví dụ:

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
46]. Khóa tương ứng với từng mục trong danh sách được tính một lần và sau đó được sử dụng cho toàn bộ quy trình sắp xếp. Giá trị mặc định của
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26 có nghĩa là các mục danh sách được sắp xếp trực tiếp mà không tính toán giá trị khóa riêng.

Tiện ích

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
48 có sẵn để chuyển đổi hàm CMP kiểu 2.x thành chức năng chính.

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

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51, 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.

Phương pháp này sửa đổi trình tự tại chỗ cho nền kinh tế của không gian khi sắp xếp một chuỗi lớn. Để nhắc nhở người dùng rằng nó hoạt động bằng hiệu ứng phụ, nó không trả về trình tự được sắp xếp [sử dụng

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
42 để yêu cầu rõ ràng một thể hiện danh sách được sắp xếp mới].

Phương pháp

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
45 đượ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].

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

Chi tiết triển khai CPYThon: Trong khi một danh sách đang được sắp xếp, hiệu quả của việc cố gắng đột biến hoặc thậm chí kiểm tra, danh sách không được xác định. Việc triển khai C của Python làm cho danh sách xuất hiện trống trong thời gian và tăng

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
73 nếu nó có thể phát hiện rằng danh sách đã bị đột biến trong một loại. While a list is being sorted, the effect of attempting to mutate, or even inspect, the list is undefined. The C implementation of Python makes the list appear empty for the duration, and raises
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
73 if it can detect that the list has been mutated during a sort.

Bài hát

Các bộ dữ liệu là các trình tự bất biến, thường được sử dụng để lưu trữ các bộ sưu tập dữ liệu không đồng nhất [như 2 bộ phận được sản xuất bởi

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
53 tích hợp]. Các bộ dữ liệu cũng được sử dụng cho các trường hợp cần một chuỗi dữ liệu đồng nhất bất biến [chẳng hạn như cho phép lưu trữ trong ví dụ
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
84 hoặc
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
83].

Lớp ________ 556 [[itable]] ¶[[iterable]]

Tuples có thể được xây dựng theo một số cách:

  • Sử dụng một cặp dấu ngoặc đơn để biểu thị bộ tuple trống:

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    43

  • Sử dụng dấu phẩy theo dõi cho một bộ phim singleton:

    >>> int.from_bytes[b'\x00\x10', byteorder='big']
    16
    >>> int.from_bytes[b'\x00\x10', byteorder='little']
    4096
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
    -1024
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
    64512
    >>> int.from_bytes[[255, 0, 0], byteorder='big']
    16711680
    
    58 hoặc
    >>> int.from_bytes[b'\x00\x10', byteorder='big']
    16
    >>> int.from_bytes[b'\x00\x10', byteorder='little']
    4096
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
    -1024
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
    64512
    >>> int.from_bytes[[255, 0, 0], byteorder='big']
    16711680
    
    59

  • Tách các mục với dấu phẩy:

    >>> int.from_bytes[b'\x00\x10', byteorder='big']
    16
    >>> int.from_bytes[b'\x00\x10', byteorder='little']
    4096
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
    -1024
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
    64512
    >>> int.from_bytes[[255, 0, 0], byteorder='big']
    16711680
    
    60 hoặc
    >>> int.from_bytes[b'\x00\x10', byteorder='big']
    16
    >>> int.from_bytes[b'\x00\x10', byteorder='little']
    4096
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
    -1024
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
    64512
    >>> int.from_bytes[[255, 0, 0], byteorder='big']
    16711680
    
    61

  • Sử dụng

    >>> int.from_bytes[b'\x00\x10', byteorder='big']
    16
    >>> int.from_bytes[b'\x00\x10', byteorder='little']
    4096
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
    -1024
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
    64512
    >>> int.from_bytes[[255, 0, 0], byteorder='big']
    16711680
    
    62 tích hợp:
    >>> int.from_bytes[b'\x00\x10', byteorder='big']
    16
    >>> int.from_bytes[b'\x00\x10', byteorder='little']
    4096
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
    -1024
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
    64512
    >>> int.from_bytes[[255, 0, 0], byteorder='big']
    16711680
    
    62 hoặc
    >>> int.from_bytes[b'\x00\x10', byteorder='big']
    16
    >>> int.from_bytes[b'\x00\x10', byteorder='little']
    4096
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
    -1024
    >>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
    64512
    >>> int.from_bytes[[255, 0, 0], byteorder='big']
    16711680
    
    64

Hàm tạo xây dựng một tuple có các vật phẩm giống nhau và theo cùng thứ tự với các vật phẩm của ITerable. Có thể lặp lại có thể là một chuỗi, một thùng chứa hỗ trợ lặp hoặc đối tượng lặp. Nếu có thể là một tuple, nó sẽ được trả lại không thay đổi. Ví dụ:

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
65 trả về
>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
66 và
>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
67 trả về
>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
68. Nếu không có đối số nào được đưa ra, hàm tạo tạo một bộ tuple trống mới,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
43.

Lưu ý rằng đó thực sự là dấu phẩy tạo ra một tuple, không phải dấu ngoặc đơn. Các dấu ngoặc đơn là tùy chọn, ngoại trừ trong trường hợp tuple trống hoặc khi chúng cần thiết để tránh sự mơ hồ của cú pháp. Ví dụ:

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
70 là một cuộc gọi chức năng với ba đối số, trong khi
>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
71 là một cuộc gọi hàm với 3-tuple là đối số duy nhất.

Tuples thực hiện tất cả các hoạt động trình tự phổ biến.common sequence operations.

Đối với các bộ sưu tập dữ liệu không đồng nhất trong đó quyền truy cập theo tên rõ ràng hơn truy cập theo chỉ mục,

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
72 có thể là một lựa chọn phù hợp hơn một đối tượng tuple đơn giản.

Các dãy¶

Loại

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
10 đại diện cho một chuỗi số bất biến và thường được sử dụng để lặp lại một số lần cụ thể trong các vòng
def bit_count[self]:
    return bin[self].count["1"]
96.

Lớp ________ 575 [Dừng] Lớp ____ 575 [Bắt đầu, Dừng [, Bước]][stop]¶ class
>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
75[start, stop[, step]]

Các đối số cho hàm tạo phạm vi phải là số nguyên [tích hợp

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
96 hoặc bất kỳ đối tượng nào thực hiện phương thức đặc biệt
>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
27]. Nếu đối số bước bị bỏ qua, nó mặc định là
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
50. Nếu đối số bắt đầu bị bỏ qua, nó mặc định là
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
37. Nếu bước bằng không,
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
73 được nâng lên.

Đối với một bước tích cực, nội dung của một phạm vi

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
82 được xác định bởi công thức
>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
83 trong đó
>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
84 và
>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
85.

Đối với một bước âm, nội dung của phạm vi vẫn được xác định bởi công thức

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
83, nhưng các ràng buộc là
>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
84 và
>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
88.

Một đối tượng phạm vi sẽ trống nếu

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
89 không đáp ứng ràng buộc giá trị. Phạm vi hỗ trợ các chỉ số tiêu cực, nhưng chúng được hiểu là lập chỉ mục từ phần cuối của chuỗi được xác định bởi các chỉ số dương.

Phạm vi chứa các giá trị tuyệt đối lớn hơn

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
90 được cho phép nhưng một số tính năng [như
>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
91] có thể tăng
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
93.

Ví dụ phạm vi:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
3

Phạm vi thực hiện tất cả các hoạt động trình tự phổ biến ngoại trừ việc kết hợp và lặp lại [do thực tế là các đối tượng phạm vi chỉ có thể đại diện cho các chuỗi theo mô hình nghiêm ngặt và sự lặp lại và nối thường sẽ vi phạm mô hình đó].common sequence operations except concatenation and repetition [due to the fact that range objects can only represent sequences that follow a strict pattern and repetition and concatenation will usually violate that pattern].

________ 593¶

Giá trị của tham số bắt đầu [hoặc

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
37 nếu tham số không được cung cấp]

________ 595¶

Giá trị của tham số dừng

________ 596¶

Giá trị của tham số bước [hoặc

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
50 nếu tham số không được cung cấp]

Ưu điểm của loại

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
10 so với
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
08 hoặc
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
09 thông thường là một đối tượng
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
10 sẽ luôn có cùng lượng bộ nhớ [nhỏ], bất kể kích thước của phạm vi nó biểu thị [vì nó chỉ lưu trữ các giá trị , tính toán các mặt hàng riêng lẻ và phụ khi cần thiết].

Các đối tượng phạm vi thực hiện

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
11 ABC và cung cấp các tính năng như kiểm tra ngăn chặn, tra cứu chỉ mục phần tử, cắt và hỗ trợ cho các chỉ số âm [xem các loại trình tự - danh sách, tuple, phạm vi]:Sequence Types — list, tuple, range]:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
4

Các đối tượng phạm vi thử nghiệm cho sự bình đẳng với

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
73 và
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
74 so sánh chúng là trình tự. Đó là, hai đối tượng phạm vi được coi là bằng nhau nếu chúng đại diện cho cùng một chuỗi các giá trị. .

Đã thay đổi trong phiên bản 3.2: Thực hiện trình tự ABC. Hỗ trợ cắt lát và chỉ số tiêu cực. Kiểm tra

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
96 Đối tượng để thành viên trong thời gian không đổi thay vì lặp lại thông qua tất cả các mục.Implement the Sequence ABC. Support slicing and negative indices. Test
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
96 objects for membership in constant time instead of iterating through all items.

Đã thay đổi trong phiên bản 3.3: Xác định ‘== và và‘! = Để so sánh các đối tượng phạm vi dựa trên chuỗi các giá trị mà chúng xác định [thay vì so sánh dựa trên nhận dạng đối tượng].Define ‘==’ and ‘!=’ to compare range objects based on the sequence of values they define [instead of comparing based on object identity].

Mới trong phiên bản 3.3: Các thuộc tính

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
02,
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
03 và
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
04.The
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
02,
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
03 and
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
04 attributes.

Xem thêm

  • Công thức Linspace cho thấy cách thực hiện một phiên bản lười biếng của phạm vi phù hợp cho các ứng dụng điểm nổi.

Loại chuỗi văn bản - ________ 437¶

Dữ liệu văn bản trong Python được xử lý với các đối tượng

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
37 hoặc chuỗi. Chuỗi là chuỗi bất biến của các điểm mã unicode. Chuỗi chữ được viết theo nhiều cách khác nhau:sequences of Unicode code points. String literals are written in a variety of ways:

  • Trích dẫn đơn:

    >>> [-2.0].is_integer[]
    True
    >>> [3.2].is_integer[]
    False
    
    19

  • Báo giá kép:

    >>> [-2.0].is_integer[]
    True
    >>> [3.2].is_integer[]
    False
    
    20

  • Triple Trích dẫn:

    >>> [-2.0].is_integer[]
    True
    >>> [3.2].is_integer[]
    False
    
    21,
    >>> [-2.0].is_integer[]
    True
    >>> [3.2].is_integer[]
    False
    
    22

Các chuỗi được trích dẫn ba có thể kéo dài nhiều dòng - tất cả các khoảng trắng liên quan sẽ được bao gồm trong chuỗi theo nghĩa đen.

Chuỗi chữ là một phần của một biểu thức duy nhất và chỉ có khoảng trắng giữa chúng sẽ được chuyển đổi hoàn toàn thành một chuỗi theo nghĩa đen. Đó là,

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
23.

Xem Chuỗi và BYTE theo nghĩa đen để biết thêm về các dạng khác nhau của chuỗi theo nghĩa đen, bao gồm các chuỗi thoát được hỗ trợ và tiền tố

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
82 [Hồi RAW] vô hiệu hóa hầu hết các trình tự thoát.String and Bytes literals for more about the various forms of string literal, including supported escape sequences, and the
>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
82 [“raw”] prefix that disables most escape sequence processing.

Các chuỗi cũng có thể được tạo từ các đối tượng khác bằng hàm tạo

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
37.

Vì không có loại ký tự riêng biệt nào, việc lập chỉ mục một chuỗi tạo ra các chuỗi có độ dài 1. nghĩa là, đối với chuỗi không trống,

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
26.

Cũng không có loại chuỗi có thể thay đổi, nhưng

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
68 hoặc
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
69 có thể được sử dụng để xây dựng các chuỗi hiệu quả từ nhiều đoạn.

Đã thay đổi trong phiên bản 3.3: Để tương thích ngược với sê -ri Python 2, tiền tố

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
29 một lần nữa được cho phép trên các chữ viết. Nó không có tác dụng đối với ý nghĩa của các chữ cái và không thể được kết hợp với tiền tố
>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
82.For backwards compatibility with the Python 2 series, the
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
29 prefix is once again permitted on string literals. It has no effect on the meaning of string literals and cannot be combined with the
>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
82 prefix.

Lớp ________ 631 [Object = ''] ¶ Lớp ________ 631 [Object = b '', expoding = 'utf-8', error = 'nghiêm ngặt'][object='']¶ class
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
31[object=b'', encoding='utf-8', errors='strict']

Trả về một phiên bản chuỗi của đối tượng. Nếu đối tượng không được cung cấp, hãy trả về chuỗi trống. Mặt khác, hành vi của

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
28 phụ thuộc vào việc mã hóa hay lỗi được đưa ra, như sau.string version of object. If object is not provided, returns the empty string. Otherwise, the behavior of
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
28 depends on whether encoding or errors is given, as follows.

Nếu không mã hóa và lỗi nào được đưa ra,

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
34 trả về
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
35, đó là biểu diễn chuỗi không chính thức hoặc có thể in độc đáo của đối tượng. Đối với các đối tượng chuỗi, đây là chuỗi chính. Nếu đối tượng không có phương thức
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
36, thì
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
28 sẽ quay trở lại để trả lại
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
38.

Nếu ít nhất một mã hóa hoặc lỗi được đưa ra, đối tượng phải là một đối tượng giống như byte [ví dụ:

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
38 hoặc
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
39]. Trong trường hợp này, nếu đối tượng là đối tượng
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
38 [hoặc
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
39], thì
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
43 tương đương với
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
44. Mặt khác, đối tượng byte bên dưới đối tượng bộ đệm được lấy trước khi gọi
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
45. Xem các loại chuỗi nhị phân - Byte, bytearray, bộ nhớ và giao thức bộ đệm để biết thông tin về các đối tượng bộ đệm.bytes-like object [e.g.
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
38 or
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
39]. In this case, if object is a
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
38 [or
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
39] object, then
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
43 is equivalent to
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
44. Otherwise, the bytes object underlying the buffer object is obtained before calling
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
45. See Binary Sequence Types — bytes, bytearray, memoryview and Buffer Protocol for information on buffer objects.

Chuyển một đối tượng

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
38 cho
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
28 mà không có đối số mã hóa hoặc lỗi nằm trong trường hợp đầu tiên trả lại biểu diễn chuỗi không chính thức [xem thêm tùy chọn dòng lệnh
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
48 cho Python]. Ví dụ:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
5

Để biết thêm thông tin về lớp

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
37 và các phương thức của nó, hãy xem loại chuỗi văn bản - phần STR và phần Chuỗi bên dưới. Để đầu ra các chuỗi được định dạng, hãy xem các phần SHIGRE BÀI TẬP và Định dạng Syntax. Ngoài ra, xem phần Dịch vụ xử lý văn bản.Text Sequence Type — str and the String Methods section below. To output formatted strings, see the Formatted string literals and Format String Syntax sections. In addition, see the Text Processing Services section.

Phương pháp chuỗi

Các chuỗi thực hiện tất cả các hoạt động trình tự chung, cùng với các phương pháp bổ sung được mô tả dưới đây.common sequence operations, along with the additional methods described below.

Các chuỗi cũng hỗ trợ hai kiểu định dạng chuỗi, một loại cung cấp một mức độ linh hoạt và tùy chỉnh lớn [xem

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
50, cú pháp chuỗi định dạng và định dạng chuỗi tùy chỉnh] và kiểu khác dựa trên kiểu C Để sử dụng chính xác, nhưng thường nhanh hơn cho các trường hợp, nó có thể xử lý [định dạng chuỗi kiểu printf].Format String Syntax and Custom String Formatting] and the other based on C
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
51 style formatting that handles a narrower range of types and is slightly harder to use correctly, but is often faster for the cases it can handle [printf-style String Formatting].

Phần dịch vụ xử lý văn bản của thư viện tiêu chuẩn bao gồm một số mô -đun khác cung cấp các tiện ích liên quan đến văn bản khác nhau [bao gồm hỗ trợ biểu thức thông thường trong mô -đun

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
52].Text Processing Services section of the standard library covers a number of other modules that provide various text related utilities [including regular expression support in the
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
52 module].

________ 653 ________ 654 [][]

Trả về một bản sao của chuỗi với ký tự đầu tiên được viết hoa và phần còn lại.

Thay đổi trong phiên bản 3.8: Nhân vật đầu tiên hiện được đưa vào TitleCase thay vì chữ hoa. Điều này có nghĩa là các nhân vật như Digraphs sẽ chỉ có chữ cái đầu tiên được viết hoa, thay vì toàn bộ ký tự.The first character is now put into titlecase rather than uppercase. This means that characters like digraphs will only have their first letter capitalized, instead of the full character.

________ 653 ________ 656 [][]

Trả về một bản sao được giới thiệu của chuỗi. Các chuỗi casefold có thể được sử dụng để kết hợp không đồng ý.

Casfolding tương tự như LowerCasing nhưng tích cực hơn vì nó nhằm loại bỏ tất cả các trường hợp phân biệt trong một chuỗi. Ví dụ, chữ thường của Đức

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
57 tương đương với
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
58. Vì nó đã là chữ thường,
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
59 sẽ không làm gì với
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
57;
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
61 chuyển đổi nó thành
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
58.

Thuật toán Casefold được mô tả trong Phần 3.13 của tiêu chuẩn Unicode.

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

________ 653 ________ 664 [chiều rộng [, fillchar]] ¶[width[, fillchar]]

Trả về tập trung trong một chuỗi chiều rộng chiều dài. Đệm được thực hiện bằng cách sử dụng fillchar được chỉ định [mặc định là không gian ASCII]. Chuỗi ban đầu được trả về nếu chiều rộng nhỏ hơn hoặc bằng

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
28.

________ 653 ________ 667 [phụ [, bắt đầu [, kết thúc]]][sub[, start[, end]]]

Trả về số lần xuất hiện không chồng chéo của phụ con trong phạm vi [bắt đầu, kết thúc]. Đối số tùy chọn bắt đầu và kết thúc được hiểu là trong ký hiệu lát cắt.

________ 653 ________ 669 [mã hóa = 'UTF-8', lỗi = 'nghiêm ngặt'] ¶[encoding='utf-8', errors='strict']

Trả về một phiên bản được mã hóa của chuỗi dưới dạng đối tượng byte. Mã hóa mặc định là

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
70. Lỗi có thể được đưa ra để đặt sơ đồ xử lý lỗi khác. Mặc định cho các lỗi là
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
71, có nghĩa là các lỗi mã hóa làm tăng
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
72. Các giá trị có thể khác là
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
73,
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
74,
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
75,
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
76 và bất kỳ tên nào khác được đăng ký qua
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
77, xem phần xử lý lỗi phần. Để biết danh sách các mã hóa có thể, hãy xem phần Mã hóa tiêu chuẩn.Error Handlers. For a list of possible encodings, see section Standard Encodings.

Theo mặc định, đối số lỗi không được kiểm tra các hiệu suất tốt nhất, nhưng chỉ được sử dụng ở lỗi mã hóa đầu tiên. Kích hoạt chế độ phát triển Python hoặc sử dụng bản dựng gỡ lỗi để kiểm tra lỗi.Python Development Mode, or use a debug build to check errors.

Đã thay đổi trong phiên bản 3.1: Hỗ trợ cho các đối số từ khóa được thêm vào.Support for keyword arguments added.

Đã thay đổi trong phiên bản 3.9: Các lỗi hiện được kiểm tra ở chế độ phát triển và ở chế độ gỡ lỗi.The errors is now checked in development mode and in debug mode.

________ 653 ________ 679 [hậu tố [, bắt đầu [, kết thúc]]] ¶[suffix[, start[, end]]]

Trả về

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51 Nếu chuỗi kết thúc bằng hậu tố được chỉ định, nếu không hãy trả về
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33. Hậu tố cũng có thể là một bộ hậu tố để tìm kiếm. Với bắt đầu tùy chọn, kiểm tra bắt đầu ở vị trí đó. Với kết thúc tùy chọn, dừng so sánh ở vị trí đó.

________ 653 ________ 683 [tabSize = 8] ¶[tabsize=8]

Trả về một bản sao của chuỗi trong đó tất cả các ký tự tab được thay thế bằng một hoặc nhiều khoảng trắng, tùy thuộc vào cột hiện tại và kích thước tab đã cho. Vị trí tab xảy ra mọi ký tự TabSize [mặc định là 8, đưa ra các vị trí tab tại các cột 0, 8, 16, v.v.]. Để mở rộng chuỗi, cột hiện tại được đặt thành 0 và chuỗi được kiểm tra ký tự theo ký tự. Nếu ký tự là một tab [

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
84], một hoặc nhiều ký tự không gian được chèn vào kết quả cho đến khi cột hiện tại bằng vị trí tab tiếp theo. . Bất kỳ ký tự nào khác được sao chép không thay đổi và cột hiện tại được tăng lên bởi một nhân vật bất kể nhân vật được biểu diễn như thế nào khi được in.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
6

________ 653 ________ 688 [phụ [, bắt đầu [, kết thúc]]][sub[, start[, end]]]

Trả về chỉ số thấp nhất trong chuỗi nơi tìm thấy phụ phụ trong phần

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
89. Đối số tùy chọn bắt đầu và kết thúc được hiểu là trong ký hiệu lát cắt. Trả lại
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
37 nếu không tìm thấy phụ.

Ghi chú

Phương pháp

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
91 chỉ nên được sử dụng nếu bạn cần biết vị trí của phụ. Để kiểm tra xem Sub có phải là chất nền hay không, hãy sử dụng toán tử
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
93:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
7

________ 653 ________ 694 [*args, ** kwargs] ¶[*args, **kwargs]

Thực hiện thao tác định dạng chuỗi. Chuỗi mà phương pháp này được gọi là có thể chứa văn bản theo nghĩa đen hoặc các trường thay thế được phân tách bằng niềng răng

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
45. Mỗi trường thay thế chứa chỉ số số của đối số vị trí hoặc tên của đối số từ khóa. Trả về một bản sao của chuỗi trong đó mỗi trường thay thế được thay thế bằng giá trị chuỗi của đối số tương ứng.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
8

Xem Cú pháp Chuỗi định dạng để biết mô tả về các tùy chọn định dạng khác nhau có thể được chỉ định trong chuỗi định dạng.Format String Syntax for a description of the various formatting options that can be specified in format strings.

Ghi chú

Định dạng một số [

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
96,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
97,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
98,
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
04 và các lớp con] với loại
def bit_count[self]:
    return bin[self].count["1"]
60 [Xh:
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
01] hoặc dài hơn 1 byte và địa phương
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
03 khác với địa phương
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
02. Thay đổi tạm thời này ảnh hưởng đến các chủ đề khác.

Đã thay đổi trong phiên bản 3.7: Khi định dạng một số với loại

def bit_count[self]:
    return bin[self].count["1"]
60, hàm đặt tạm thời locale
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
02 thành locle
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
03 trong một số trường hợp.When formatting a number with the
def bit_count[self]:
    return bin[self].count["1"]
60 type, the function sets temporarily the
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
02 locale to the
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
03 locale in some cases.

________ 653 ________ 713 [ánh xạ] ¶[mapping]

Tương tự như

[sign] ['0x'] integer ['.' fraction] ['p' exponent]
14, ngoại trừ
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
15 được sử dụng trực tiếp và không được sao chép vào
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
83. Điều này rất hữu ích nếu ví dụ
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
15 là một lớp con Dict:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
9

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

________ 653 ________ 719 [phụ [, bắt đầu [, kết thúc]]][sub[, start[, end]]]

Giống như

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
91, nhưng tăng
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
73 khi không tìm thấy chuỗi con.

________ 653 ________ 723 [][]

Trả về

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51 Nếu tất cả các ký tự trong chuỗi là chữ và số và có ít nhất một ký tự,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33 nếu không. Một ký tự
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
26 là chữ và số nếu một trong những lợi nhuận sau đây
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51:
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
28,
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
29,
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
30 hoặc
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
31.

________ 653 ________ 733 [][]

Trả về

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51 Nếu tất cả các ký tự trong chuỗi là chữ cái và có ít nhất một ký tự,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33 nếu không. Các ký tự bảng chữ cái là những ký tự được xác định trong cơ sở dữ liệu ký tự Unicode là chữ cái chữ, tức là, những ký tự có thuộc tính danh mục chung là một trong những LM LM, LT LT, Lưu ý rằng điều này khác với thuộc tính bảng chữ cái của người Viking được xác định trong tiêu chuẩn Unicode.

________ 653 ________ 737 [][]

Trả về

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51 Nếu chuỗi trống hoặc tất cả các ký tự trong chuỗi là ASCII,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33 nếu không. Các ký tự ASCII có các điểm mã trong phạm vi U+0000-U+007F.

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

________ 653 ________ 741 [][]

Trả về

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51 Nếu tất cả các ký tự trong chuỗi là các ký tự thập phân và có ít nhất một ký tự,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33 nếu không. Các ký tự thập phân là các ký tự có thể được sử dụng để hình thành số trong cơ sở 10, ví dụ: U+0660, chữ số tiếng Ả Rập không. Chính thức là một ký tự thập phân là một nhân vật trong danh mục chung của Unicode.

________ 653 ________ 745 [][]

Trả về

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51 Nếu tất cả các ký tự trong chuỗi là các chữ số và có ít nhất một ký tự,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33 nếu không. Các chữ số bao gồm các ký tự thập phân và các chữ số cần xử lý đặc biệt, chẳng hạn như các chữ số siêu tương thích. Điều này bao gồm các chữ số không thể được sử dụng để hình thành số trong cơ sở 10, như số Kharosthi. Chính thức, một chữ số là một ký tự có giá trị thuộc tính numeric_type = Digit hoặc numeric_type = thập phân.

________ 653 ________ 749 [][]

Trả về

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51 Nếu chuỗi là một định danh hợp lệ theo định nghĩa ngôn ngữ, định danh phần và từ khóa.Identifiers and keywords.

Gọi

[sign] ['0x'] integer ['.' fraction] ['p' exponent]
51 để kiểm tra xem chuỗi
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
52 có phải là mã định danh dành riêng, chẳng hạn như
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
53 và
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
54.

Example:

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
0

________ 653 ________ 756 [][]

Trả về

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51 Nếu tất cả các ký tự vỏ 4 trong chuỗi là chữ thường và có ít nhất một ký tự vỏ,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33 nếu không.

________ 653 ________ 760 [][]

Trả về

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51 Nếu tất cả các ký tự trong chuỗi là các ký tự số và có ít nhất một ký tự,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33 nếu không. Các ký tự số bao gồm các ký tự chữ số và tất cả các ký tự có thuộc tính giá trị số Unicode, ví dụ: U+2155, phân số thô một phần năm. Chính thức, các ký tự số là các ký tự có giá trị thuộc tính numeric_type = Digit, numeric_type = decimal hoặc numeric_type = numeric.

________ 653 ________ 764 [][]

Trả về

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51 Nếu tất cả các ký tự trong chuỗi có thể in hoặc chuỗi trống,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33 nếu không. Các ký tự không thể in là những ký tự được xác định trong cơ sở dữ liệu ký tự Unicode là một phần khác hoặc phân tách khác, ngoại trừ không gian ASCII [0x20] được coi là có thể in. .

________ 653 ________ 771 [][]

Trả về

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51 nếu chỉ có các ký tự khoảng trắng trong chuỗi và có ít nhất một ký tự,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33 nếu không.

Một ký tự là khoảng trắng nếu trong cơ sở dữ liệu ký tự Unicode [xem

[sign] ['0x'] integer ['.' fraction] ['p' exponent]
74], loại chung của nó là
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
75 [Dấu tách, không gian] hoặc lớp hai chiều của nó là một trong
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
76,
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
77 hoặc
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
78.

________ 653 ________ 780 [][]

Trả về

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51 Nếu chuỗi là một chuỗi tiêu chuẩn và có ít nhất một ký tự, ví dụ: các ký tự chữ hoa chỉ có thể theo dõi các ký tự chưa được ghi và các ký tự chữ thường chỉ các ký tự được đặt. Trả lại
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33 nếu không.

________ 653 ________ 784 [][]

Trả về

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51 Nếu tất cả các ký tự vỏ 4 trong chuỗi là chữ hoa và có ít nhất một ký tự vỏ,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33 nếu không.

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
1

________ 653 ________ 788 [có thể lặp lại] ¶[iterable]

Trả về một chuỗi là sự kết hợp của các chuỗi trong Itable. A

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
83 sẽ được nâng lên nếu có bất kỳ giá trị không chuỗi nào trong ITable, bao gồm các đối tượng
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
38. Phân tách giữa các phần tử là chuỗi cung cấp phương pháp này.

________ 653 ________ 792 [chiều rộng [, fillchar]] ¶[width[, fillchar]]

Trả về chuỗi bên trái chính đáng trong một chuỗi chiều rộng. Đệm được thực hiện bằng cách sử dụng fillchar được chỉ định [mặc định là không gian ASCII]. Chuỗi ban đầu được trả về nếu chiều rộng nhỏ hơn hoặc bằng

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
28.

________ 653 ________ 795 [][]

Trả về một bản sao của chuỗi với tất cả các ký tự vỏ 4 được chuyển đổi thành chữ thường.

Thuật toán hạ thấp được sử dụng được mô tả trong Phần 3.13 của tiêu chuẩn Unicode.

________ 653 ________ 797 [[chars]] ¶[[chars]]

Trả về một bản sao của chuỗi với các ký tự hàng đầu bị xóa. Đối số ký tự là một chuỗi chỉ định tập hợp các ký tự sẽ được xóa. Nếu bị bỏ qua hoặc

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26, đối số ký tự mặc định sẽ loại bỏ khoảng trắng. Đối số chars không phải là tiền tố; Thay vào đó, tất cả các kết hợp các giá trị của nó đều bị tước:

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
2

Xem

[sign] ['0x'] integer ['.' fraction] ['p' exponent]
99 để biết phương thức sẽ xóa một chuỗi tiền tố duy nhất thay vì tất cả một tập hợp các ký tự. Ví dụ:

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
3

tĩnh ________ 653 ________ 801 [x [, y [, z]]] ¶[x[, y[, z]]]

Phương pháp tĩnh này trả về một bảng dịch có thể sử dụng cho

>>> float.fromhex['0x3.a7p10']
3740.0
02.

Nếu chỉ có một đối số, thì đó phải là một bản đồ từ điển unicode [số nguyên] hoặc ký tự [chuỗi có độ dài 1] thành các thứ tự Unicode, chuỗi [có độ dài tùy ý] hoặc

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26. Các phím nhân vật sau đó sẽ được chuyển đổi thành các lệnh.

Nếu có hai đối số, chúng phải là các chuỗi có độ dài bằng nhau và trong từ điển kết quả, mỗi ký tự trong X sẽ được ánh xạ tới ký tự ở cùng một vị trí trong y. Nếu có một đối số thứ ba, nó phải là một chuỗi, có các ký tự sẽ được ánh xạ tới

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26 trong kết quả.

________ 653 ________ 806 [tháng 9] ¶[sep]

Chia chuỗi ở lần xuất hiện đầu tiên của SEP và trả về 3-tuple chứa phần trước dấu phân cách, chính dấu phân cách và phần sau khi phân tách. Nếu không tìm thấy dấu phân cách, hãy trả về 3-tuple chứa chính chuỗi, theo sau là hai chuỗi trống.

________ 653 ________ 808 [tiền tố, /] ¶[prefix, /]

Nếu chuỗi bắt đầu với chuỗi tiền tố, hãy trả về

>>> float.fromhex['0x3.a7p10']
3740.0
09. Nếu không, hãy trả về một bản sao của chuỗi gốc:

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
4

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

________ 653 ________ 811 [hậu tố, /] ¶[suffix, /]

Nếu chuỗi kết thúc bằng chuỗi hậu tố và hậu tố đó không trống, hãy trả về

>>> float.fromhex['0x3.a7p10']
3740.0
12. Nếu không, hãy trả về một bản sao của chuỗi gốc:

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
5

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

________ 653 ________ 811 [hậu tố, /] ¶[old, new[, count]]

Nếu chuỗi kết thúc bằng chuỗi hậu tố và hậu tố đó không trống, hãy trả về

>>> float.fromhex['0x3.a7p10']
3740.0
12. Nếu không, hãy trả về một bản sao của chuỗi gốc:

________ 653 ________ 814 [cũ, mới [, đếm]] ¶[sub[, start[, end]]]

Trả về một bản sao của chuỗi với tất cả các lần xuất hiện của phần phụ cũ được thay thế bằng mới. Nếu số lượng đối số tùy chọn được đưa ra, chỉ có các lần xuất hiện đầu tiên được thay thế.

________ 653 ________ 816 [phụ [, bắt đầu [, kết thúc]]][sub[, start[, end]]]

Trả về chỉ số cao nhất trong chuỗi nơi tìm thấy phụ phụ, do đó phụ được chứa trong

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
89. Đối số tùy chọn bắt đầu và kết thúc được hiểu là trong ký hiệu lát cắt. Trả lại
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
37 khi thất bại.

________ 653 ________ 820 [phụ [, bắt đầu [, kết thúc]]] ¶[width[, fillchar]]

Giống như

>>> float.fromhex['0x3.a7p10']
3740.0
21 nhưng tăng
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
73 khi không tìm thấy phụ con.

________ 653 ________ 824 [chiều rộng [, fillchar]] ¶[sep]

Trả về chuỗi đúng chính đáng trong một chuỗi chiều rộng chiều dài. Đệm được thực hiện bằng cách sử dụng fillchar được chỉ định [mặc định là không gian ASCII]. Chuỗi ban đầu được trả về nếu chiều rộng nhỏ hơn hoặc bằng

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
28.

________ 653 ________ 829 [sep = none, maxsplit =- 1] ¶[sep=None, maxsplit=- 1]

Trả về một danh sách các từ trong chuỗi, sử dụng SEP làm chuỗi DELIMITER. Nếu MAXSplit được đưa ra, tại hầu hết các phân tách MaxSplit được thực hiện, những cái ngoài cùng bên phải. Nếu SEP không được chỉ định hoặc

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26, bất kỳ chuỗi khoảng trắng nào cũng là một dấu phân cách. Ngoại trừ việc chia tách từ bên phải,
>>> float.fromhex['0x3.a7p10']
3740.0
31 hoạt động như
>>> float.fromhex['0x3.a7p10']
3740.0
32 được mô tả chi tiết dưới đây.

________ 653 ________ 834 [[chars]] ¶[[chars]]

Trả về một bản sao của chuỗi với các ký tự dấu vết bị xóa. Đối số ký tự là một chuỗi chỉ định tập hợp các ký tự sẽ được xóa. Nếu bị bỏ qua hoặc

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26, đối số ký tự mặc định sẽ loại bỏ khoảng trắng. Đối số chars không phải là một hậu tố; Thay vào đó, tất cả các kết hợp các giá trị của nó đều bị tước:

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
6

Xem

>>> float.fromhex['0x3.a7p10']
3740.0
36 để biết phương thức sẽ loại bỏ một chuỗi hậu tố duy nhất thay vì tất cả một tập hợp các ký tự. Ví dụ:

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
7

________ 653 ________ 838 [sep = none, maxsplit =- 1] ¶[sep=None, maxsplit=- 1]

Trả về một danh sách các từ trong chuỗi, sử dụng SEP làm chuỗi DELIMITER. Nếu MAXSplit được đưa ra, tại hầu hết các phân tách MAXSplit được thực hiện [do đó, danh sách sẽ có nhiều nhất là các yếu tố

>>> float.fromhex['0x3.a7p10']
3740.0
39]. Nếu MAXSplit không được chỉ định hoặc
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
37, thì không có giới hạn về số lượng chia tách [tất cả các phân tách có thể được thực hiện].

Nếu SEP được đưa ra, các phân định liên tiếp không được nhóm lại với nhau và được coi là phân định các chuỗi trống [ví dụ:

>>> float.fromhex['0x3.a7p10']
3740.0
41 trả về
>>> float.fromhex['0x3.a7p10']
3740.0
42]. Đối số SEP có thể bao gồm nhiều ký tự [ví dụ:
>>> float.fromhex['0x3.a7p10']
3740.0
43 trả về
>>> float.fromhex['0x3.a7p10']
3740.0
44]. Tách một chuỗi trống với một dấu phân cách được chỉ định trả về
>>> float.fromhex['0x3.a7p10']
3740.0
45.

Ví dụ:

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
8

Nếu SEP không được chỉ định hoặc là

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26, một thuật toán phân tách khác nhau được áp dụng: Các lần chạy khoảng trắng liên tiếp được coi là một dấu phân cách duy nhất và kết quả sẽ không chứa các chuỗi trống ở đầu hoặc kết thúc nếu chuỗi có độ nào hoặc dấu vết trắng. Do đó, việc phân tách một chuỗi trống hoặc một chuỗi bao gồm chỉ khoảng trắng với một dấu phân cách
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26 trả về
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
44.

Ví dụ:

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
9

Nếu SEP không được chỉ định hoặc là
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26, một thuật toán phân tách khác nhau được áp dụng: Các lần chạy khoảng trắng liên tiếp được coi là một dấu phân cách duy nhất và kết quả sẽ không chứa các chuỗi trống ở đầu hoặc kết thúc nếu chuỗi có độ nào hoặc dấu vết trắng. Do đó, việc phân tách một chuỗi trống hoặc một chuỗi bao gồm chỉ khoảng trắng với một dấu phân cách
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26 trả về
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
44.[keepends=False]

________ 653 ________ 850 [Keepends = false] ¶

Trả về một danh sách các dòng trong chuỗi, phá vỡ các ranh giới dòng. Phá vỡ dòng không được bao gồm trong danh sách kết quả trừ khi Keepends được đưa ra và đúng.universal newlines.

Phương pháp này phân chia trên các ranh giới dòng sau. Cụ thể, các ranh giới là một siêu dòng của Newlines phổ quát.

Đại diện

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
85

Sự mô tả

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
86

Line Feed

>>> float.fromhex['0x3.a7p10']
3740.0
53

Vận chuyển trở lại

Vận chuyển trở lại + nguồn cấp dữ liệu dòng

>>> float.fromhex['0x3.a7p10']
3740.0
54 hoặc
>>> float.fromhex['0x3.a7p10']
3740.0
55

Tabline

>>> float.fromhex['0x3.a7p10']
3740.0
56 hoặc
>>> float.fromhex['0x3.a7p10']
3740.0
57

>>> float.fromhex['0x3.a7p10']
3740.0
58

Thức ăn dạng

>>> float.fromhex['0x3.a7p10']
3740.0
59

Bộ phân cách tập tin

>>> float.fromhex['0x3.a7p10']
3740.0
60

Phân tách nhóm

>>> float.fromhex['0x3.a7p10']
3740.0
61

Ghi điểm phân tách

>>> float.fromhex['0x3.a7p10']
3740.0
62

Dòng tiếp theo [Mã điều khiển C1]

>>> float.fromhex['0x3.a7p10']
3740.0
63

Dòng phân tách

Phân tách đoạn văn

>>> float.fromhex['0x3.a7p10']
3740.0
54 and
>>> float.fromhex['0x3.a7p10']
3740.0
56 added to list of line boundaries.

Ví dụ:

def bit_count[self]:
    return bin[self].count["1"]
0

Nếu SEP không được chỉ định hoặc là

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26, một thuật toán phân tách khác nhau được áp dụng: Các lần chạy khoảng trắng liên tiếp được coi là một dấu phân cách duy nhất và kết quả sẽ không chứa các chuỗi trống ở đầu hoặc kết thúc nếu chuỗi có độ nào hoặc dấu vết trắng. Do đó, việc phân tách một chuỗi trống hoặc một chuỗi bao gồm chỉ khoảng trắng với một dấu phân cách
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26 trả về
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
44.

def bit_count[self]:
    return bin[self].count["1"]
1

________ 653 ________ 850 [Keepends = false] ¶

def bit_count[self]:
    return bin[self].count["1"]
2

Trả về một danh sách các dòng trong chuỗi, phá vỡ các ranh giới dòng. Phá vỡ dòng không được bao gồm trong danh sách kết quả trừ khi Keepends được đưa ra và đúng.[prefix[, start[, end]]]

Phương pháp này phân chia trên các ranh giới dòng sau. Cụ thể, các ranh giới là một siêu dòng của Newlines phổ quát.

Đại diện[[chars]]

Sự mô tả

def bit_count[self]:
    return bin[self].count["1"]
3

Line Feed

def bit_count[self]:
    return bin[self].count["1"]
4

Vận chuyển trở lại[]

Vận chuyển trở lại + nguồn cấp dữ liệu dòng

>>> float.fromhex['0x3.a7p10']
3740.0
54 hoặc
>>> float.fromhex['0x3.a7p10']
3740.0
55[]

Tabline

Ví dụ:

def bit_count[self]:
    return bin[self].count["1"]
5

Nếu SEP không được chỉ định hoặc là

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26, một thuật toán phân tách khác nhau được áp dụng: Các lần chạy khoảng trắng liên tiếp được coi là một dấu phân cách duy nhất và kết quả sẽ không chứa các chuỗi trống ở đầu hoặc kết thúc nếu chuỗi có độ nào hoặc dấu vết trắng. Do đó, việc phân tách một chuỗi trống hoặc một chuỗi bao gồm chỉ khoảng trắng với một dấu phân cách
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26 trả về
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
44.

def bit_count[self]:
    return bin[self].count["1"]
6

Hàm

>>> float.fromhex['0x3.a7p10']
3740.0
80 không có vấn đề này, vì nó chỉ chia các từ trên khoảng trắng.

Ngoài ra, một cách giải quyết cho dấu nháy đơn có thể được xây dựng bằng cách sử dụng các biểu thức thông thường:

def bit_count[self]:
    return bin[self].count["1"]
7

________ 653 ________ 882 [Bảng][table]

Trả về một bản sao của chuỗi trong đó mỗi ký tự đã được ánh xạ qua bảng dịch đã cho. Bảng phải là một đối tượng thực hiện lập chỉ mục thông qua

>>> float.fromhex['0x3.a7p10']
3740.0
83, thường là ánh xạ hoặc trình tự. Khi được lập chỉ mục bởi một thứ tự unicode [một số nguyên], đối tượng bảng có thể thực hiện bất kỳ điều nào sau đây: trả về một thứ tự unicode hoặc một chuỗi, để ánh xạ ký tự cho một hoặc nhiều ký tự khác; Trả về
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26, để xóa ký tự khỏi chuỗi trả về; hoặc nâng một ngoại lệ
>>> float.fromhex['0x3.a7p10']
3740.0
85, để ánh xạ nhân vật đến chính nó.mapping or sequence. When indexed by a Unicode ordinal [an integer], the table object can do any of the following: return a Unicode ordinal or a string, to map the character to one or more other characters; return
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26, to delete the character from the return string; or raise a
>>> float.fromhex['0x3.a7p10']
3740.0
85 exception, to map the character to itself.

Bạn có thể sử dụng

>>> float.fromhex['0x3.a7p10']
3740.0
86 để tạo bản đồ dịch từ ánh xạ ký tự sang ký tự ở các định dạng khác nhau.

Xem thêm Mô -đun

>>> float.fromhex['0x3.a7p10']
3740.0
87 để biết cách tiếp cận linh hoạt hơn đối với ánh xạ ký tự tùy chỉnh.

________ 653 ________ 889 [][]

Trả về một bản sao của chuỗi với tất cả các ký tự vỏ 4 được chuyển đổi thành chữ hoa. Lưu ý rằng

>>> float.fromhex['0x3.a7p10']
3740.0
90 có thể là
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33 nếu
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
52 chứa các ký tự chưa được xử lý hoặc nếu danh mục Unicode của [các] ký tự kết quả không phải là LU LU [chữ cái, chữ hoa], nhưng ví dụ: LT LT [Thư, Titlecase].

Thuật toán từ trên được sử dụng được mô tả trong Phần 3.13 của tiêu chuẩn Unicode.

________ 653 ________ 894 [chiều rộng] ¶[width]

Trả về một bản sao của chuỗi bên trái chứa đầy ASCII

>>> float.fromhex['0x3.a7p10']
3740.0
95 chữ số để tạo một chuỗi chiều rộng. Một tiền tố dấu hiệu hàng đầu [________ 896/________ 897] được xử lý bằng cách chèn đệm sau ký tự dấu hiệu chứ không phải trước. Chuỗi ban đầu được trả về nếu chiều rộng nhỏ hơn hoặc bằng
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
28.

Ví dụ:

def bit_count[self]:
    return bin[self].count["1"]
8

________ 651 Định dạng chuỗi theo phong cách

Ghi chú

Các hoạt động định dạng được mô tả ở đây thể hiện một loạt các kỳ quặc dẫn đến một số lỗi phổ biến [chẳng hạn như không hiển thị chính xác các bộ dữ liệu và từ điển]. Sử dụng các chuỗi chữ được định dạng mới hơn, giao diện

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
50 hoặc chuỗi mẫu có thể giúp tránh các lỗi này. Mỗi lựa chọn thay thế này cung cấp sự đánh đổi và lợi ích của riêng họ về sự đơn giản, tính linh hoạt và/hoặc khả năng mở rộng.formatted string literals, the
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
50 interface, or template strings may help avoid these errors. Each of these alternatives provides their own trade-offs and benefits of simplicity, flexibility, and/or extensibility.

Các đối tượng chuỗi có một thao tác tích hợp duy nhất: toán tử

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
01 [modulo]. Điều này còn được gọi là toán tử định dạng chuỗi hoặc nội suy. Cho
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
02 [trong đó định dạng là một chuỗi], thông số kỹ thuật chuyển đổi
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
01 trong định dạng được thay thế bằng 0 hoặc nhiều phần tử của các giá trị. Hiệu ứng tương tự như sử dụng
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
04 trong ngôn ngữ C.

Nếu định dạng yêu cầu một đối số duy nhất, các giá trị có thể là một đối tượng không phải là một đối tượng. 5 Nếu không, các giá trị phải là một tuple với chính xác số lượng mục được chỉ định bởi chuỗi định dạng hoặc một đối tượng ánh xạ duy nhất [ví dụ: từ điển].

Trình xác định chuyển đổi chứa hai hoặc nhiều ký tự và có các thành phần sau, phải xảy ra theo thứ tự này:

  1. Ký tự

    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    05, đánh dấu sự khởi đầu của trình xác định.

  2. Khóa ánh xạ [tùy chọn], bao gồm một chuỗi các ký tự được đặt dấu ngoặc đơn [ví dụ:

    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    06].

  3. Cờ chuyển đổi [tùy chọn], ảnh hưởng đến kết quả của một số loại chuyển đổi.

  4. Chiều rộng trường tối thiểu [tùy chọn]. Nếu được chỉ định là

    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    07 [dấu hoa thị], chiều rộng thực tế được đọc từ phần tử tiếp theo của bộ tple trong các giá trị và đối tượng để chuyển đổi xuất hiện sau chiều rộng trường tối thiểu và độ chính xác tùy chọn.

  5. Độ chính xác [tùy chọn], được đưa ra dưới dạng

    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    08 [DOT] theo sau là độ chính xác. Nếu được chỉ định là
    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    07 [dấu hoa thị], độ chính xác thực tế được đọc từ phần tử tiếp theo của bộ tuple trong các giá trị và giá trị để chuyển đổi xuất hiện sau độ chính xác.

  6. Công cụ sửa đổi độ dài [tùy chọn].

  7. Loại chuyển đổi.

Khi đối số đúng là từ điển [hoặc loại ánh xạ khác], thì các định dạng trong chuỗi phải bao gồm khóa ánh xạ dấu ngoặc đơn vào từ điển đó được chèn ngay sau ký tự

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
05. Khóa ánh xạ chọn giá trị được định dạng từ ánh xạ. Ví dụ:

def bit_count[self]:
    return bin[self].count["1"]
9

Trong trường hợp này, không có nhà xác định

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
15 có thể xảy ra ở định dạng [vì chúng yêu cầu danh sách tham số tuần tự].

Các ký tự cờ chuyển đổi là:

Lá cờ

Nghĩa

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
12

Chuyển đổi giá trị sẽ sử dụng hình thức thay thế trên mạng [trong đó được xác định bên dưới].

>>> float.fromhex['0x3.a7p10']
3740.0
95

Việc chuyển đổi sẽ được đệm bằng 0 cho các giá trị số.

>>> float.fromhex['0x3.a7p10']
3740.0
97

Giá trị được chuyển đổi được điều chỉnh trái [ghi đè chuyển đổi

>>> float.fromhex['0x3.a7p10']
3740.0
95 nếu cả hai được đưa ra].

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
16

.

>>> float.fromhex['0x3.a7p10']
3740.0
96

Một ký tự dấu hiệu [

>>> float.fromhex['0x3.a7p10']
3740.0
96 hoặc
>>> float.fromhex['0x3.a7p10']
3740.0
97] sẽ đi trước chuyển đổi [ghi đè một lá cờ không gian trên mạng].

Một công cụ sửa đổi độ dài [

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
20,
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
21 hoặc
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
22] có thể xuất hiện, nhưng bị bỏ qua vì nó không cần thiết cho Python - vì vậy ví dụ:
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
23 giống hệt với
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
24.

Các loại chuyển đổi là:

Chuyển đổi

Nghĩa

Ghi chú

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
25

Đã ký số nguyên thập phân.

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
26

Đã ký số nguyên thập phân.

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
27

Đã ký giá trị bát phân.

[1]

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
28

Loại lỗi thời - Nó giống hệt với

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
25.

[6]

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
30

Đã ký tên thập lục phân [chữ thường].

[2]

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
31

Đã ký tên thập lục phân [chữ hoa].

[2]

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
32

Định dạng theo cấp số mũ [chữ thường].

[3]

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
33

Định dạng số mũ điểm nổi [chữ hoa].

[3]

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
34

Định dạng số thập phân điểm nổi.

[3]

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
35

Định dạng số thập phân điểm nổi.

[3]

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
36

Định dạng điểm nổi. Sử dụng định dạng theo cấp số nhân nếu số mũ nhỏ hơn -4 hoặc không nhỏ hơn độ chính xác, định dạng thập phân nếu không.

[4]

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
37

Định dạng điểm nổi. Sử dụng định dạng theo cấp số nhân Uppercase nếu số mũ nhỏ hơn -4 hoặc không nhỏ hơn độ chính xác, định dạng thập phân khác.

[4]

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
38

Ký tự đơn [chấp nhận số nguyên hoặc chuỗi ký tự đơn].

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
39

Chuỗi [chuyển đổi bất kỳ đối tượng Python nào bằng cách sử dụng

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
27].

[5]

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
41

Chuỗi [chuyển đổi bất kỳ đối tượng Python nào bằng cách sử dụng

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
28].

[5]

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
43

Chuỗi [chuyển đổi bất kỳ đối tượng Python nào bằng cách sử dụng

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
44].

[5]

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
05

Không có đối số được chuyển đổi, kết quả là một ký tự

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
05 trong kết quả.

Notes:

  1. Hình thức thay thế làm cho một bộ xác định octal hàng đầu [

    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    47] được chèn trước chữ số đầu tiên.

  2. Hình thức thay thế gây ra

    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    48 hoặc
    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    49 hàng đầu [tùy thuộc vào định dạng
    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    30 hay
    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    31 đã được sử dụng] để được chèn trước chữ số đầu tiên.

  3. Hình thức thay thế làm cho kết quả luôn chứa một điểm thập phân, ngay cả khi không có chữ số nào tuân theo nó.

    Độ chính xác xác định số lượng chữ số sau điểm thập phân và mặc định là 6.

  4. Hình thức thay thế gây ra kết quả luôn chứa một điểm thập phân và các số 0 không được loại bỏ như chúng sẽ có.

    Độ chính xác xác định số lượng các chữ số đáng kể trước và sau điểm thập phân và mặc định là 6.

  5. Nếu độ chính xác là

    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    52, đầu ra bị cắt giảm thành
    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    52 ký tự.

  6. Xem PEP 237.PEP 237.

Vì các chuỗi Python có độ dài rõ ràng, các chuyển đổi

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
54 không cho rằng
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
55 là kết thúc của chuỗi.

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

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
56 Chuyển đổi cho các số có giá trị tuyệt đối trên 1E50 không còn được thay thế bằng các chuyển đổi
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
57.
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
56 conversions for numbers whose absolute value is over 1e50 are no longer replaced by
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
57 conversions.

Các loại trình tự nhị phân -
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
38,
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
39, ________ 960¶

Các loại tích hợp cốt lõi để thao tác dữ liệu nhị phân là

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
38 và
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
39. Chúng được hỗ trợ bởi
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
60 sử dụng giao thức bộ đệm để truy cập bộ nhớ của các đối tượng nhị phân khác mà không cần tạo bản sao.buffer protocol to access the memory of other binary objects without needing to make a copy.

Mô-đun

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
64 hỗ trợ lưu trữ hiệu quả các loại dữ liệu cơ bản như số nguyên 32 bit và giá trị nổi có độ chính xác kép của IEEE754.

Đối tượng byte

Đối tượng byte là các chuỗi bất biến của các byte đơn. Do nhiều giao thức nhị phân chính dựa trên mã hóa văn bản ASCII, các đối tượng byte cung cấp một số phương thức chỉ hợp lệ khi làm việc với dữ liệu tương thích ASCII và liên quan chặt chẽ đến các đối tượng chuỗi theo nhiều cách khác.

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

Đầu tiên, cú pháp cho các chữ cái byte phần lớn giống như đối với các chữ viết, ngoại trừ tiền tố

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
66 được thêm vào:

  • Trích dẫn đơn:

    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    67

  • Báo giá kép:

    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    68

  • Triple Trích dẫn:

    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    69,
    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    70

Chỉ các ký tự ASCII được phép trong các chữ cái byte [bất kể mã hóa mã nguồn được khai báo]. Bất kỳ giá trị nhị phân nào trên 127 phải được nhập vào các chữ cái bằng cách sử dụng trình tự thoát thích hợp.

Như với các chữ viết, chữ byte cũng có thể sử dụng tiền tố

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
82 để vô hiệu hóa xử lý các chuỗi thoát. Xem chuỗi và byte theo nghĩa đen để biết thêm về các dạng byte khác nhau theo nghĩa đen, bao gồm các chuỗi thoát được hỗ trợ.String and Bytes literals for more about the various forms of bytes literal, including supported escape sequences.

Mặc dù các byte theo nghĩa đen và biểu diễn dựa trên văn bản ASCII, các đối tượng byte thực sự hoạt động như các chuỗi số nguyên bất biến, với mỗi giá trị trong chuỗi bị hạn chế sao cho

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
72 [cố gắng vi phạm giới hạn này sẽ kích hoạt
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
73]. Điều này được thực hiện có chủ ý để nhấn mạnh rằng trong khi nhiều định dạng nhị phân bao gồm các yếu tố dựa trên ASCII và có thể được thao tác một cách hữu ích với một số thuật toán định hướng văn bản ASCII tương thích thường sẽ dẫn đến tham nhũng dữ liệu].

Ngoài các biểu mẫu theo nghĩa đen, các đối tượng byte có thể được tạo theo một số cách khác:

  • Một đối tượng byte chứa không có độ dài được chỉ định:

    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    74

  • Từ một số nguyên của số nguyên:

    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    75

  • Sao chép dữ liệu nhị phân hiện có thông qua giao thức bộ đệm:

    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    76

Cũng xem các byte tích hợp.bytes built-in.

Vì 2 chữ số thập lục phân tương ứng chính xác với một byte duy nhất, các số thập lục phân là một định dạng thường được sử dụng để mô tả dữ liệu nhị phân. Theo đó, loại byte có một phương thức lớp bổ sung để đọc dữ liệu ở định dạng đó:

ClassMethod ________ 322 [Chuỗi] ¶[string]

Phương thức lớp

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
38 này trả về một đối tượng byte, giải mã đối tượng chuỗi đã cho. Chuỗi phải chứa hai chữ số thập lục phân trên byte, với khoảng trắng ASCII bị bỏ qua.

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
0

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

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
79 Bây giờ bỏ qua tất cả khoảng trắng ASCII trong chuỗi, không chỉ không gian.
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
79 now skips all ASCII whitespace in the string, not just spaces.

Một hàm chuyển đổi ngược tồn tại để biến một đối tượng byte thành đại diện thập lục phân của nó.

________ 318 [[sep [, byte_per_sep]]] ¶[[sep[, bytes_per_sep]]]

Trả về một đối tượng chuỗi chứa hai chữ số thập lục phân cho mỗi byte trong trường hợp.

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
1

Nếu bạn muốn làm cho chuỗi Hex dễ đọc hơn, bạn có thể chỉ định một tham số SEP của một ký tự duy nhất để đưa vào đầu ra. Theo mặc định, dấu phân cách này sẽ được bao gồm giữa mỗi byte. Một tham số tùy chọn thứ hai byte_per_sep kiểm soát khoảng cách. Các giá trị dương tính vị trí phân tách từ bên phải, các giá trị âm từ bên trái.

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
2

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

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

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
81 hiện hỗ trợ các tham số SEP và byte_per_sep tùy chọn để chèn các phân tách giữa các byte trong đầu ra hex.
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
81 now supports optional sep and bytes_per_sep parameters to insert separators between bytes in the hex output.

Vì các đối tượng byte là chuỗi số nguyên [gần giống với một tuple], đối với đối tượng byte B,

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
82 sẽ là một số nguyên, trong khi
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
83 sẽ là đối tượng byte có độ dài 1. [Điều này tương phản với các chuỗi văn bản, trong đó cả việc lập chỉ mục và cắt sẽ sẽ tạo ra một chuỗi độ dài 1]

Việc biểu diễn các đối tượng byte sử dụng định dạng theo nghĩa đen [

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
84] vì nó thường hữu ích hơn ví dụ:
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
85. Bạn luôn có thể chuyển đổi một đối tượng byte thành một danh sách các số nguyên bằng
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
86.

Đối tượng bytearray

Các đối tượng

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
39 là một đối tác có thể thay đổi đối với các đối tượng
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
38.

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

Không có cú pháp theo nghĩa đen chuyên dụng cho các đối tượng bytearray, thay vào đó chúng luôn được tạo bằng cách gọi hàm tạo:

  • Tạo một thể hiện trống:

    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    90

  • Tạo một thể hiện không chứa không có độ dài nhất định:

    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    91

  • Từ một số nguyên của số nguyên:

    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    92

  • Sao chép dữ liệu nhị phân hiện có thông qua giao thức bộ đệm:

    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    93

Vì các đối tượng bytearray có thể thay đổi, chúng hỗ trợ các hoạt động chuỗi có thể thay đổi ngoài các hoạt động byte và bytearray phổ biến được mô tả trong các hoạt động byte và bytearray.mutable sequence operations in addition to the common bytes and bytearray operations described in Bytes and Bytearray Operations.

Cũng xem bytearray tích hợp.bytearray built-in.

Vì 2 chữ số thập lục phân tương ứng chính xác với một byte duy nhất, các số thập lục phân là một định dạng thường được sử dụng để mô tả dữ liệu nhị phân. Theo đó, loại bytearray có một phương thức lớp bổ sung để đọc dữ liệu ở định dạng đó:

ClassMethod ________ 322 [Chuỗi] ¶[string]

Phương thức lớp

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
38 này trả về một đối tượng byte, giải mã đối tượng chuỗi đã cho. Chuỗi phải chứa hai chữ số thập lục phân trên byte, với khoảng trắng ASCII bị bỏ qua.

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
3

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

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
79 Bây giờ bỏ qua tất cả khoảng trắng ASCII trong chuỗi, không chỉ không gian.
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
96 now skips all ASCII whitespace in the string, not just spaces.

Một hàm chuyển đổi ngược tồn tại để biến một đối tượng byte thành đại diện thập lục phân của nó.

________ 318 [[sep [, byte_per_sep]]] ¶[[sep[, bytes_per_sep]]]

Trả về một đối tượng chuỗi chứa hai chữ số thập lục phân cho mỗi byte trong trường hợp.

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
4

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

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

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
81 hiện hỗ trợ các tham số SEP và byte_per_sep tùy chọn để chèn các phân tách giữa các byte trong đầu ra hex.Similar to
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
81,
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
99 now supports optional sep and bytes_per_sep parameters to insert separators between bytes in the hex output.

Vì các đối tượng byte là chuỗi số nguyên [gần giống với một tuple], đối với đối tượng byte B,

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
82 sẽ là một số nguyên, trong khi
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
83 sẽ là đối tượng byte có độ dài 1. [Điều này tương phản với các chuỗi văn bản, trong đó cả việc lập chỉ mục và cắt sẽ sẽ tạo ra một chuỗi độ dài 1]

Việc biểu diễn các đối tượng byte sử dụng định dạng theo nghĩa đen [

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
84] vì nó thường hữu ích hơn ví dụ:
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
85. Bạn luôn có thể chuyển đổi một đối tượng byte thành một danh sách các số nguyên bằng
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
86.

Đối tượng bytearray

Các đối tượng

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
39 là một đối tác có thể thay đổi đối với các đối tượng
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
38.common sequence operations. They interoperate not just with operands of the same type, but with any bytes-like object. Due to this flexibility, they can be freely mixed in operations without causing errors. However, the return type of the result may depend on the order of operands.

Ghi chú

Các phương pháp trên byte và các đối tượng bytearray don don chấp nhận các chuỗi là đối số của chúng, giống như các phương pháp trên các chuỗi don don chấp nhận byte như đối số của chúng. Ví dụ: bạn phải viết:

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
5

and:

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
6

Một số hoạt động byte và bytearray giả định việc sử dụng các định dạng nhị phân tương thích ASCII và do đó nên tránh khi làm việc với dữ liệu nhị phân tùy ý. Những hạn chế này được đề cập dưới đây.

Ghi chú

Sử dụng các hoạt động dựa trên ASCII này để thao tác dữ liệu nhị phân không được lưu trữ ở định dạng dựa trên ASCII có thể dẫn đến tham nhũng dữ liệu.

Các phương pháp sau trên byte và các đối tượng bytearray có thể được sử dụng với dữ liệu nhị phân tùy ý.

________ 1005 ________ 667 [sub [, start [, end]]][sub[, start[, end]]]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
67[sub[, start[, end]]]

Trả về số lần xuất hiện không chồng chéo của phụ Trại con trong phạm vi [bắt đầu, kết thúc]. Đối số tùy chọn bắt đầu và kết thúc được hiểu là trong ký hiệu lát cắt.

Phần tiếp theo để tìm kiếm có thể là bất kỳ đối tượng giống như byte hoặc một số nguyên trong phạm vi 0 đến 255.bytes-like object or an integer in the range 0 to 255.

Đã thay đổi trong phiên bản 3.3: Cũng chấp nhận một số nguyên trong phạm vi 0 đến 255 là phần sau.Also accept an integer in the range 0 to 255 as the subsequence.

________ 1005 ________ 808 [tiền tố, /][prefix, /]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
>>> float.fromhex['0x3.a7p10']
3740.0
08[prefix, /]

Nếu dữ liệu nhị phân bắt đầu với chuỗi tiền tố, hãy trả về

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
013. Nếu không, hãy trả lại một bản sao của dữ liệu nhị phân gốc:

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
7

Tiền tố có thể là bất kỳ đối tượng giống như byte.bytes-like object.

Ghi chú

Phiên bản bytearray của phương pháp này không hoạt động tại chỗ - nó luôn tạo ra một đối tượng mới, ngay cả khi không có thay đổi nào được thực hiện.

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

________ 1005 ________ 811 [hậu tố, /] ____ ____ 1007 ________ 811 [hậu tố, /] ¶[suffix, /]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
>>> float.fromhex['0x3.a7p10']
3740.0
11[suffix, /]

Nếu dữ liệu nhị phân kết thúc bằng chuỗi hậu tố và hậu tố đó không trống, hãy trả về

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
018. Nếu không, hãy trả lại một bản sao của dữ liệu nhị phân gốc:

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
8

Hậu tố có thể là bất kỳ đối tượng giống như byte.bytes-like object.

Ghi chú

Phiên bản bytearray của phương pháp này không hoạt động tại chỗ - nó luôn tạo ra một đối tượng mới, ngay cả khi không có thay đổi nào được thực hiện.

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

________ 1005 ________ 811 [hậu tố, /] ____ ____ 1007 ________ 811 [hậu tố, /] ¶[encoding='utf-8', errors='strict']
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
020[encoding='utf-8', errors='strict']

Nếu dữ liệu nhị phân kết thúc bằng chuỗi hậu tố và hậu tố đó không trống, hãy trả về

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
018. Nếu không, hãy trả lại một bản sao của dữ liệu nhị phân gốc:Error Handlers. For a list of possible encodings, see section Standard Encodings.

Hậu tố có thể là bất kỳ đối tượng giống như byte.Python Development Mode, or use a debug build to check errors.

Ghi chú

________ 1005 ________ 1020 [mã hóa = 'UTF-8', lỗi = 'nghiêm ngặt']bytes-like object directly, without needing to make a temporary bytes or bytearray object.

Trả về một chuỗi được giải mã từ các byte đã cho. Mã hóa mặc định là

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
70. Lỗi có thể được đưa ra để đặt sơ đồ xử lý lỗi khác. Mặc định cho các lỗi là
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
71, có nghĩa là các lỗi mã hóa làm tăng
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
72. Các giá trị có thể khác là
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
73,
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
74 và bất kỳ tên nào khác được đăng ký qua
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
77, xem phần xử lý lỗi phần. Để biết danh sách các mã hóa có thể, hãy xem phần Mã hóa tiêu chuẩn.Added support for keyword arguments.

Theo mặc định, đối số lỗi không được kiểm tra các hiệu suất tốt nhất, nhưng chỉ được sử dụng ở lỗi giải mã đầu tiên. Kích hoạt chế độ phát triển Python hoặc sử dụng bản dựng gỡ lỗi để kiểm tra lỗi.The errors is now checked in development mode and in debug mode.

Chuyển đối số mã hóa cho
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
37 cho phép giải mã trực tiếp bất kỳ đối tượng giống như byte nào, mà không cần phải tạo một byte tạm thời hoặc đối tượng bytearray.[suffix[, start[, end]]]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
79[suffix[, start[, end]]]

Đã thay đổi trong phiên bản 3.1: Đã thêm hỗ trợ cho các đối số từ khóa.

Đã thay đổi trong phiên bản 3.9: Các lỗi hiện được kiểm tra ở chế độ phát triển và ở chế độ gỡ lỗi.bytes-like object.

________ 1005 ________ 679 [Hậu tố [, bắt đầu [, kết thúc]]][sub[, start[, end]]]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
88[sub[, start[, end]]]

Trả về

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51 Nếu dữ liệu nhị phân kết thúc bằng hậu tố được chỉ định, nếu không, hãy trả về
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33. Hậu tố cũng có thể là một bộ hậu tố để tìm kiếm. Với bắt đầu tùy chọn, kiểm tra bắt đầu ở vị trí đó. Với kết thúc tùy chọn, dừng so sánh ở vị trí đó.

Phần tiếp theo để tìm kiếm có thể là bất kỳ đối tượng giống như byte hoặc một số nguyên trong phạm vi 0 đến 255.bytes-like object or an integer in the range 0 to 255.

Ghi chú

Đã thay đổi trong phiên bản 3.3: Cũng chấp nhận một số nguyên trong phạm vi 0 đến 255 là phần sau.

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
9

Đã thay đổi trong phiên bản 3.3: Cũng chấp nhận một số nguyên trong phạm vi 0 đến 255 là phần sau.Also accept an integer in the range 0 to 255 as the subsequence.

________ 1005 ________ 808 [tiền tố, /][sub[, start[, end]]]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
19[sub[, start[, end]]]

Nếu dữ liệu nhị phân bắt đầu với chuỗi tiền tố, hãy trả về

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
013. Nếu không, hãy trả lại một bản sao của dữ liệu nhị phân gốc:

Phần tiếp theo để tìm kiếm có thể là bất kỳ đối tượng giống như byte hoặc một số nguyên trong phạm vi 0 đến 255.bytes-like object or an integer in the range 0 to 255.

Đã thay đổi trong phiên bản 3.3: Cũng chấp nhận một số nguyên trong phạm vi 0 đến 255 là phần sau.Also accept an integer in the range 0 to 255 as the subsequence.

________ 1005 ________ 808 [tiền tố, /][iterable]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
88[iterable]

Nếu dữ liệu nhị phân bắt đầu với chuỗi tiền tố, hãy trả về

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
013. Nếu không, hãy trả lại một bản sao của dữ liệu nhị phân gốc:bytes-like objects, including
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
37 objects. The separator between elements is the contents of the bytes or bytearray object providing this method.

tĩnh ________ 1005 ________ 801 [từ, đến] ____ ____ 1007 ____ 801 [từ, đến] ¶[from, to]¶ static
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
>>> float.fromhex['0x3.a7p10']
3740.0
01[from, to]

Phương thức tĩnh này trả về một bảng dịch có thể sử dụng cho

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
060 sẽ ánh xạ từng ký tự từ vào ký tự ở cùng một vị trí trong; từ và đến cả hai đều là các đối tượng giống như byte và có cùng độ dài.bytes-like objects and have the same length.

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

________ 1005 ________ 806 [tháng 9] ____ ____ 1007 ________ 806 [tháng 9] ¶[sep]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
>>> float.fromhex['0x3.a7p10']
3740.0
06[sep]

Chia trình tự ở lần xuất hiện đầu tiên của SEP và trả về 3-tuple chứa phần trước dấu phân cách, chính bộ phân cách hoặc bản sao bytearray của nó và phần sau khi phân tách. Nếu không tìm thấy dấu phân cách, hãy trả về 3-tuple chứa một bản sao của chuỗi gốc, theo sau là hai byte trống hoặc các đối tượng bytearray.

Bộ phân cách để tìm kiếm có thể là bất kỳ đối tượng giống như byte.bytes-like object.

________ 1005 ________ 814 [cũ, mới [, đếm]] ____ ____ 1007 ________ 814 [cũ, mới [, đếm]][old, new[, count]]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
>>> float.fromhex['0x3.a7p10']
3740.0
14[old, new[, count]]

Trả về một bản sao của chuỗi với tất cả các lần xuất hiện của sau đó được thay thế bằng mới. Nếu số lượng đối số tùy chọn được đưa ra, chỉ có các lần xuất hiện đầu tiên được thay thế.

Phần tiếp theo để tìm kiếm và sự thay thế của nó có thể là bất kỳ đối tượng giống như byte nào.bytes-like object.

Ghi chú

Phiên bản bytearray của phương pháp này không hoạt động tại chỗ - nó luôn tạo ra một đối tượng mới, ngay cả khi không có thay đổi nào được thực hiện.

________ 1005 ________ 816 [sub [, start [, end]]][sub[, start[, end]]]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
>>> float.fromhex['0x3.a7p10']
3740.0
16[sub[, start[, end]]]

Trả về chỉ số cao nhất trong chuỗi nơi tìm thấy phụ con, do đó phụ được chứa trong

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
89. Đối số tùy chọn bắt đầu và kết thúc được hiểu là trong ký hiệu lát cắt. Trả lại
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
37 khi thất bại.

Phần tiếp theo để tìm kiếm có thể là bất kỳ đối tượng giống như byte hoặc một số nguyên trong phạm vi 0 đến 255.bytes-like object or an integer in the range 0 to 255.

Đã thay đổi trong phiên bản 3.3: Cũng chấp nhận một số nguyên trong phạm vi 0 đến 255 là phần sau.Also accept an integer in the range 0 to 255 as the subsequence.

________ 1005 ________ 820 [sub [, start [, end]]][sub[, start[, end]]]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
>>> float.fromhex['0x3.a7p10']
3740.0
20[sub[, start[, end]]]

Giống như

>>> float.fromhex['0x3.a7p10']
3740.0
21 nhưng tăng
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
73 khi không tìm thấy phụ sau đó.

Phần tiếp theo để tìm kiếm có thể là bất kỳ đối tượng giống như byte hoặc một số nguyên trong phạm vi 0 đến 255.bytes-like object or an integer in the range 0 to 255.

Đã thay đổi trong phiên bản 3.3: Cũng chấp nhận một số nguyên trong phạm vi 0 đến 255 là phần sau.Also accept an integer in the range 0 to 255 as the subsequence.

________ 1005 ________ 820 [sub [, start [, end]]][sep]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
>>> float.fromhex['0x3.a7p10']
3740.0
27[sep]

Giống như

>>> float.fromhex['0x3.a7p10']
3740.0
21 nhưng tăng
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
73 khi không tìm thấy phụ sau đó.

Bộ phân cách để tìm kiếm có thể là bất kỳ đối tượng giống như byte.bytes-like object.

________ 1005 ________ 814 [cũ, mới [, đếm]] ____ ____ 1007 ________ 814 [cũ, mới [, đếm]][prefix[, start[, end]]]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
>>> float.fromhex['0x3.a7p10']
3740.0
69[prefix[, start[, end]]]

Trả về một bản sao của chuỗi với tất cả các lần xuất hiện của sau đó được thay thế bằng mới. Nếu số lượng đối số tùy chọn được đưa ra, chỉ có các lần xuất hiện đầu tiên được thay thế.

Phần tiếp theo để tìm kiếm và sự thay thế của nó có thể là bất kỳ đối tượng giống như byte nào.bytes-like object.

Ghi chú[table, /, delete=b'']
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
>>> float.fromhex['0x3.a7p10']
3740.0
82[table, /, delete=b'']

Phiên bản bytearray của phương pháp này không hoạt động tại chỗ - nó luôn tạo ra một đối tượng mới, ngay cả khi không có thay đổi nào được thực hiện.

________ 1005 ________ 816 [sub [, start [, end]]]

Trả về chỉ số cao nhất trong chuỗi nơi tìm thấy phụ con, do đó phụ được chứa trong

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
89. Đối số tùy chọn bắt đầu và kết thúc được hiểu là trong ký hiệu lát cắt. Trả lại
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
37 khi thất bại.

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
0

Phần tiếp theo để tìm kiếm có thể là bất kỳ đối tượng giống như byte hoặc một số nguyên trong phạm vi 0 đến 255.delete is now supported as a keyword argument.

Đã thay đổi trong phiên bản 3.3: Cũng chấp nhận một số nguyên trong phạm vi 0 đến 255 là phần sau.

________ 1005 ________ 820 [sub [, start [, end]]][width[, fillbyte]]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
64[width[, fillbyte]]

Giống như

>>> float.fromhex['0x3.a7p10']
3740.0
21 nhưng tăng
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
73 khi không tìm thấy phụ sau đó.

Ghi chú

Phiên bản bytearray của phương pháp này không hoạt động tại chỗ - nó luôn tạo ra một đối tượng mới, ngay cả khi không có thay đổi nào được thực hiện.

________ 1005 ________ 816 [sub [, start [, end]]][width[, fillbyte]]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
92[width[, fillbyte]]

Trả về chỉ số cao nhất trong chuỗi nơi tìm thấy phụ con, do đó phụ được chứa trong

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
89. Đối số tùy chọn bắt đầu và kết thúc được hiểu là trong ký hiệu lát cắt. Trả lại
>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
37 khi thất bại.

Ghi chú

Phiên bản bytearray của phương pháp này không hoạt động tại chỗ - nó luôn tạo ra một đối tượng mới, ngay cả khi không có thay đổi nào được thực hiện.

________ 1005 ________ 797 [[chars]][[chars]]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
97[[chars]]

Trả về một bản sao của chuỗi với các byte dẫn đầu được chỉ định. Đối số chars là một chuỗi nhị phân chỉ định tập hợp các giá trị byte sẽ được xóa - tên đề cập đến thực tế phương pháp này thường được sử dụng với các ký tự ASCII. Nếu bị bỏ qua hoặc

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26, đối số ký tự mặc định sẽ loại bỏ khoảng trắng ASCII. Đối số chars không phải là tiền tố; Thay vào đó, tất cả các kết hợp các giá trị của nó đều bị tước:

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
1

Trình tự nhị phân của các giá trị byte để xóa có thể là bất kỳ đối tượng giống như byte nào. Xem

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
114 để biết phương thức sẽ xóa một chuỗi tiền tố duy nhất thay vì tất cả một tập hợp các ký tự. Ví dụ:bytes-like object. See
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
114 for a method that will remove a single prefix string rather than all of a set of characters. For example:

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
2

Ghi chú

Phiên bản bytearray của phương pháp này không hoạt động tại chỗ - nó luôn tạo ra một đối tượng mới, ngay cả khi không có thay đổi nào được thực hiện.

________ 1005 ________ 797 [[chars]][width[, fillbyte]]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
>>> float.fromhex['0x3.a7p10']
3740.0
24[width[, fillbyte]]

Trả về một bản sao của chuỗi với các byte dẫn đầu được chỉ định. Đối số chars là một chuỗi nhị phân chỉ định tập hợp các giá trị byte sẽ được xóa - tên đề cập đến thực tế phương pháp này thường được sử dụng với các ký tự ASCII. Nếu bị bỏ qua hoặc

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26, đối số ký tự mặc định sẽ loại bỏ khoảng trắng ASCII. Đối số chars không phải là tiền tố; Thay vào đó, tất cả các kết hợp các giá trị của nó đều bị tước:

Ghi chú

Phiên bản bytearray của phương pháp này không hoạt động tại chỗ - nó luôn tạo ra một đối tượng mới, ngay cả khi không có thay đổi nào được thực hiện.

________ 1005 ________ 797 [[chars]][sep=None, maxsplit=- 1]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
>>> float.fromhex['0x3.a7p10']
3740.0
29[sep=None, maxsplit=- 1]

Trả về một bản sao của chuỗi với các byte dẫn đầu được chỉ định. Đối số chars là một chuỗi nhị phân chỉ định tập hợp các giá trị byte sẽ được xóa - tên đề cập đến thực tế phương pháp này thường được sử dụng với các ký tự ASCII. Nếu bị bỏ qua hoặc

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26, đối số ký tự mặc định sẽ loại bỏ khoảng trắng ASCII. Đối số chars không phải là tiền tố; Thay vào đó, tất cả các kết hợp các giá trị của nó đều bị tước:

Trình tự nhị phân của các giá trị byte để xóa có thể là bất kỳ đối tượng giống như byte nào. Xem
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
114 để biết phương thức sẽ xóa một chuỗi tiền tố duy nhất thay vì tất cả một tập hợp các ký tự. Ví dụ:[[chars]]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
>>> float.fromhex['0x3.a7p10']
3740.0
34[[chars]]

________ 1005 ________ 824 [chiều rộng [, fillbyte]]

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
3

Trả về một bản sao của đối tượng đúng được chứng minh theo một chuỗi chiều dài chiều dài. Đệm được thực hiện bằng cách sử dụng fillbyte được chỉ định [mặc định là không gian ASCII]. Đối với các đối tượng

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
38, chuỗi gốc được trả về nếu chiều rộng nhỏ hơn hoặc bằng
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
28.bytes-like object. See
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
133 for a method that will remove a single suffix string rather than all of a set of characters. For example:

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
4

Ghi chú

Phiên bản bytearray của phương pháp này không hoạt động tại chỗ - nó luôn tạo ra một đối tượng mới, ngay cả khi không có thay đổi nào được thực hiện.

________ 1005 ________ 797 [[chars]][sep=None, maxsplit=- 1]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
>>> float.fromhex['0x3.a7p10']
3740.0
38[sep=None, maxsplit=- 1]

Trả về một bản sao của chuỗi với các byte dẫn đầu được chỉ định. Đối số chars là một chuỗi nhị phân chỉ định tập hợp các giá trị byte sẽ được xóa - tên đề cập đến thực tế phương pháp này thường được sử dụng với các ký tự ASCII. Nếu bị bỏ qua hoặc

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26, đối số ký tự mặc định sẽ loại bỏ khoảng trắng ASCII. Đối số chars không phải là tiền tố; Thay vào đó, tất cả các kết hợp các giá trị của nó đều bị tước:

Trình tự nhị phân của các giá trị byte để xóa có thể là bất kỳ đối tượng giống như byte nào. Xem

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
114 để biết phương thức sẽ xóa một chuỗi tiền tố duy nhất thay vì tất cả một tập hợp các ký tự. Ví dụ:bytes-like object.

________ 1005 ________ 824 [chiều rộng [, fillbyte]]

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
5

Trả về một bản sao của đối tượng đúng được chứng minh theo một chuỗi chiều dài chiều dài. Đệm được thực hiện bằng cách sử dụng fillbyte được chỉ định [mặc định là không gian ASCII]. Đối với các đối tượng

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
38, chuỗi gốc được trả về nếu chiều rộng nhỏ hơn hoặc bằng
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
28.

________ 1005 ________ 824 [chiều rộng [, fillbyte]]

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
6

Trả về một bản sao của đối tượng đúng được chứng minh theo một chuỗi chiều dài chiều dài. Đệm được thực hiện bằng cách sử dụng fillbyte được chỉ định [mặc định là không gian ASCII]. Đối với các đối tượng
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
38, chuỗi gốc được trả về nếu chiều rộng nhỏ hơn hoặc bằng
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
28.[[chars]]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
>>> float.fromhex['0x3.a7p10']
3740.0
73[[chars]]

________ 1005 ________ 829 [SEP = none, MaxSplit =- 1]

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
7

Trình tự nhị phân của các giá trị byte để xóa có thể là bất kỳ đối tượng giống như byte nào.bytes-like object.

Ghi chú

Phiên bản bytearray của phương pháp này không hoạt động tại chỗ - nó luôn tạo ra một đối tượng mới, ngay cả khi không có thay đổi nào được thực hiện.

Các phương pháp sau trên byte và các đối tượng bytearray giả định việc sử dụng các định dạng nhị phân tương thích ASCII và không nên được áp dụng cho dữ liệu nhị phân tùy ý. Lưu ý rằng tất cả các phương thức bytearray trong phần này không hoạt động tại chỗ và thay vào đó tạo ra các đối tượng mới.

________ 1005 ________ 654 [] ____ ____ 1007 ________ 654 [] ¶[]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
54[]

Trả về một bản sao của chuỗi với mỗi byte được hiểu là một ký tự ASCII, và byte đầu tiên được viết hoa và phần còn lại. Các giá trị byte không ASCII được truyền qua không thay đổi.

Ghi chú

Phiên bản bytearray của phương pháp này không hoạt động tại chỗ - nó luôn tạo ra một đối tượng mới, ngay cả khi không có thay đổi nào được thực hiện.

Các phương pháp sau trên byte và các đối tượng bytearray giả định việc sử dụng các định dạng nhị phân tương thích ASCII và không nên được áp dụng cho dữ liệu nhị phân tùy ý. Lưu ý rằng tất cả các phương thức bytearray trong phần này không hoạt động tại chỗ và thay vào đó tạo ra các đối tượng mới.[tabsize=8]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
83[tabsize=8]

________ 1005 ________ 654 [] ____ ____ 1007 ________ 654 [] ¶

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
8

Ghi chú

Phiên bản bytearray của phương pháp này không hoạt động tại chỗ - nó luôn tạo ra một đối tượng mới, ngay cả khi không có thay đổi nào được thực hiện.

Các phương pháp sau trên byte và các đối tượng bytearray giả định việc sử dụng các định dạng nhị phân tương thích ASCII và không nên được áp dụng cho dữ liệu nhị phân tùy ý. Lưu ý rằng tất cả các phương thức bytearray trong phần này không hoạt động tại chỗ và thay vào đó tạo ra các đối tượng mới.[]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
23[]

________ 1005 ________ 654 [] ____ ____ 1007 ________ 654 [] ¶

Trả về một bản sao của chuỗi với mỗi byte được hiểu là một ký tự ASCII, và byte đầu tiên được viết hoa và phần còn lại. Các giá trị byte không ASCII được truyền qua không thay đổi.

>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
9

________ 1005 ________ 683 [TabSize = 8][]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
33[]

Trả về một bản sao của chuỗi trong đó tất cả các ký tự tab ASCII được thay thế bằng một hoặc nhiều không gian ASCII, tùy thuộc vào cột hiện tại và kích thước tab đã cho. Vị trí tab xảy ra mọi byte tabsize [mặc định là 8, đưa ra các vị trí tab tại các cột 0, 8, 16, v.v.]. Để mở rộng trình tự, cột hiện tại được đặt thành 0 và trình tự được kiểm tra byte bằng byte. Nếu byte là ký tự tab ASCII [

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
161], một hoặc nhiều ký tự không gian được chèn vào kết quả cho đến khi cột hiện tại bằng vị trí tab tiếp theo. . Bất kỳ giá trị byte nào khác được sao chép không thay đổi và cột hiện tại được tăng lên bởi một giá trị bất kể giá trị byte được biểu diễn như thế nào khi được in:

Trả về một bản sao của chuỗi với mỗi byte được hiểu là một ký tự ASCII, và byte đầu tiên được viết hoa và phần còn lại. Các giá trị byte không ASCII được truyền qua không thay đổi.

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
0

________ 1005 ________ 683 [TabSize = 8][]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
37[]

Trả về một bản sao của chuỗi trong đó tất cả các ký tự tab ASCII được thay thế bằng một hoặc nhiều không gian ASCII, tùy thuộc vào cột hiện tại và kích thước tab đã cho. Vị trí tab xảy ra mọi byte tabsize [mặc định là 8, đưa ra các vị trí tab tại các cột 0, 8, 16, v.v.]. Để mở rộng trình tự, cột hiện tại được đặt thành 0 và trình tự được kiểm tra byte bằng byte. Nếu byte là ký tự tab ASCII [

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
161], một hoặc nhiều ký tự không gian được chèn vào kết quả cho đến khi cột hiện tại bằng vị trí tab tiếp theo. . Bất kỳ giá trị byte nào khác được sao chép không thay đổi và cột hiện tại được tăng lên bởi một giá trị bất kể giá trị byte được biểu diễn như thế nào khi được in:

________ 1005 ________ 723 [] ________ 1007 ________ 723 [] ¶

Trả về
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51 Nếu tất cả các byte trong chuỗi là các ký tự ASCII theo thứ tự chữ cái hoặc các chữ số thập phân ASCII và trình tự không trống,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33 nếu không. Các ký tự ASCII chữ cái là các giá trị byte trong chuỗi
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
170. Các chữ số thập phân ASCII là các giá trị byte trong chuỗi
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
171.[]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
45[]

Ví dụ:

Trả về một bản sao của chuỗi với mỗi byte được hiểu là một ký tự ASCII, và byte đầu tiên được viết hoa và phần còn lại. Các giá trị byte không ASCII được truyền qua không thay đổi.

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
1

________ 1005 ________ 683 [TabSize = 8][]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
56[]

Trả về một bản sao của chuỗi trong đó tất cả các ký tự tab ASCII được thay thế bằng một hoặc nhiều không gian ASCII, tùy thuộc vào cột hiện tại và kích thước tab đã cho. Vị trí tab xảy ra mọi byte tabsize [mặc định là 8, đưa ra các vị trí tab tại các cột 0, 8, 16, v.v.]. Để mở rộng trình tự, cột hiện tại được đặt thành 0 và trình tự được kiểm tra byte bằng byte. Nếu byte là ký tự tab ASCII [

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
161], một hoặc nhiều ký tự không gian được chèn vào kết quả cho đến khi cột hiện tại bằng vị trí tab tiếp theo. . Bất kỳ giá trị byte nào khác được sao chép không thay đổi và cột hiện tại được tăng lên bởi một giá trị bất kể giá trị byte được biểu diễn như thế nào khi được in:

Trả về một bản sao của chuỗi với mỗi byte được hiểu là một ký tự ASCII, và byte đầu tiên được viết hoa và phần còn lại. Các giá trị byte không ASCII được truyền qua không thay đổi.

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
2

________ 1005 ________ 683 [TabSize = 8]

Trả về một bản sao của chuỗi trong đó tất cả các ký tự tab ASCII được thay thế bằng một hoặc nhiều không gian ASCII, tùy thuộc vào cột hiện tại và kích thước tab đã cho. Vị trí tab xảy ra mọi byte tabsize [mặc định là 8, đưa ra các vị trí tab tại các cột 0, 8, 16, v.v.]. Để mở rộng trình tự, cột hiện tại được đặt thành 0 và trình tự được kiểm tra byte bằng byte. Nếu byte là ký tự tab ASCII [
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
161], một hoặc nhiều ký tự không gian được chèn vào kết quả cho đến khi cột hiện tại bằng vị trí tab tiếp theo. . Bất kỳ giá trị byte nào khác được sao chép không thay đổi và cột hiện tại được tăng lên bởi một giá trị bất kể giá trị byte được biểu diễn như thế nào khi được in:[]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
71[]

________ 1005 ________ 723 [] ________ 1007 ________ 723 [] ¶

Trả về
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51 Nếu tất cả các byte trong chuỗi là các ký tự ASCII theo thứ tự chữ cái hoặc các chữ số thập phân ASCII và trình tự không trống,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33 nếu không. Các ký tự ASCII chữ cái là các giá trị byte trong chuỗi
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
170. Các chữ số thập phân ASCII là các giá trị byte trong chuỗi
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
171.[]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
80[]

Ví dụ:

Trả về một bản sao của chuỗi với mỗi byte được hiểu là một ký tự ASCII, và byte đầu tiên được viết hoa và phần còn lại. Các giá trị byte không ASCII được truyền qua không thay đổi.

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
3

________ 1005 ________ 683 [TabSize = 8][]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
84[]

Trả về một bản sao của chuỗi trong đó tất cả các ký tự tab ASCII được thay thế bằng một hoặc nhiều không gian ASCII, tùy thuộc vào cột hiện tại và kích thước tab đã cho. Vị trí tab xảy ra mọi byte tabsize [mặc định là 8, đưa ra các vị trí tab tại các cột 0, 8, 16, v.v.]. Để mở rộng trình tự, cột hiện tại được đặt thành 0 và trình tự được kiểm tra byte bằng byte. Nếu byte là ký tự tab ASCII [

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
161], một hoặc nhiều ký tự không gian được chèn vào kết quả cho đến khi cột hiện tại bằng vị trí tab tiếp theo. . Bất kỳ giá trị byte nào khác được sao chép không thay đổi và cột hiện tại được tăng lên bởi một giá trị bất kể giá trị byte được biểu diễn như thế nào khi được in:

Trả về một bản sao của chuỗi với mỗi byte được hiểu là một ký tự ASCII, và byte đầu tiên được viết hoa và phần còn lại. Các giá trị byte không ASCII được truyền qua không thay đổi.

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
4

________ 1005 ________ 683 [TabSize = 8]

Trả về một bản sao của chuỗi trong đó tất cả các ký tự tab ASCII được thay thế bằng một hoặc nhiều không gian ASCII, tùy thuộc vào cột hiện tại và kích thước tab đã cho. Vị trí tab xảy ra mọi byte tabsize [mặc định là 8, đưa ra các vị trí tab tại các cột 0, 8, 16, v.v.]. Để mở rộng trình tự, cột hiện tại được đặt thành 0 và trình tự được kiểm tra byte bằng byte. Nếu byte là ký tự tab ASCII [
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
161], một hoặc nhiều ký tự không gian được chèn vào kết quả cho đến khi cột hiện tại bằng vị trí tab tiếp theo. . Bất kỳ giá trị byte nào khác được sao chép không thay đổi và cột hiện tại được tăng lên bởi một giá trị bất kể giá trị byte được biểu diễn như thế nào khi được in:[]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
95[]

________ 1005 ________ 723 [] ________ 1007 ________ 723 [] ¶

Ví dụ:

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
5

Các ký tự ASCII viết thường là các giá trị byte trong chuỗi

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
198. Các ký tự ASCII từ trên là các giá trị byte trong chuỗi
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
199.

Ghi chú

Phiên bản bytearray của phương pháp này không hoạt động tại chỗ - nó luôn tạo ra một đối tượng mới, ngay cả khi không có thay đổi nào được thực hiện.

________ 1005 ________ 850 [Keepends = false][keepends=False]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
>>> float.fromhex['0x3.a7p10']
3740.0
50[keepends=False]

Trả về một danh sách các dòng trong chuỗi nhị phân, phá vỡ các ranh giới dòng ASCII. Phương pháp này sử dụng phương pháp tiếp cận NewLines phổ quát để phân tách các dòng. Phá vỡ dòng không được bao gồm trong danh sách kết quả trừ khi Keepends được đưa ra và đúng.universal newlines approach to splitting lines. Line breaks are not included in the resulting list unless keepends is given and true.

Ví dụ:

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
6

Không giống như

>>> float.fromhex['0x3.a7p10']
3740.0
32 khi một chuỗi phân cách được đưa ra, phương thức này trả về một danh sách trống cho chuỗi trống và ngắt dòng đầu cuối không dẫn đến một dòng bổ sung:

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
7

________ 1005 ________ 876 [] ____ ____ 1007 ________ 876 [] ¶[]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
>>> float.fromhex['0x3.a7p10']
3740.0
76[]

Trả về một bản sao của chuỗi với tất cả các ký tự ASCII viết thường được chuyển đổi thành đối tác chữ hoa tương ứng của chúng và ngược lại.

Ví dụ:

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
8

Các ký tự ASCII viết thường là các giá trị byte trong chuỗi

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
198. Các ký tự ASCII từ trên là các giá trị byte trong chuỗi
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
199.

Ghi chú

Ghi chú

Phiên bản bytearray của phương pháp này không hoạt động tại chỗ - nó luôn tạo ra một đối tượng mới, ngay cả khi không có thay đổi nào được thực hiện.

________ 1005 ________ 850 [Keepends = false][]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
>>> float.fromhex['0x3.a7p10']
3740.0
79[]

Trả về một danh sách các dòng trong chuỗi nhị phân, phá vỡ các ranh giới dòng ASCII. Phương pháp này sử dụng phương pháp tiếp cận NewLines phổ quát để phân tách các dòng. Phá vỡ dòng không được bao gồm trong danh sách kết quả trừ khi Keepends được đưa ra và đúng.

Ví dụ:

>>> [-2.0].is_integer[]
True
>>> [3.2].is_integer[]
False
9

Không giống như

>>> float.fromhex['0x3.a7p10']
3740.0
32 khi một chuỗi phân cách được đưa ra, phương thức này trả về một danh sách trống cho chuỗi trống và ngắt dòng đầu cuối không dẫn đến một dòng bổ sung:

________ 1005 ________ 876 [] ____ ____ 1007 ________ 876 [] ¶

[sign] ['0x'] integer ['.' fraction] ['p' exponent]
0

Trả về một bản sao của chuỗi với tất cả các ký tự ASCII viết thường được chuyển đổi thành đối tác chữ hoa tương ứng của chúng và ngược lại.

[sign] ['0x'] integer ['.' fraction] ['p' exponent]
1

Ghi chú

Phiên bản bytearray của phương pháp này không hoạt động tại chỗ - nó luôn tạo ra một đối tượng mới, ngay cả khi không có thay đổi nào được thực hiện.

________ 1005 ________ 850 [Keepends = false][]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
>>> float.fromhex['0x3.a7p10']
3740.0
89[]

Trả về một danh sách các dòng trong chuỗi nhị phân, phá vỡ các ranh giới dòng ASCII. Phương pháp này sử dụng phương pháp tiếp cận NewLines phổ quát để phân tách các dòng. Phá vỡ dòng không được bao gồm trong danh sách kết quả trừ khi Keepends được đưa ra và đúng.

Ví dụ:

[sign] ['0x'] integer ['.' fraction] ['p' exponent]
2

Các ký tự ASCII viết thường là các giá trị byte trong chuỗi

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
198. Các ký tự ASCII từ trên là các giá trị byte trong chuỗi
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
199.

Ghi chú

Phiên bản bytearray của phương pháp này không hoạt động tại chỗ - nó luôn tạo ra một đối tượng mới, ngay cả khi không có thay đổi nào được thực hiện.

________ 1005 ________ 850 [Keepends = false][width]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
007
>>> float.fromhex['0x3.a7p10']
3740.0
94[width]

Trả về một danh sách các dòng trong chuỗi nhị phân, phá vỡ các ranh giới dòng ASCII. Phương pháp này sử dụng phương pháp tiếp cận NewLines phổ quát để phân tách các dòng. Phá vỡ dòng không được bao gồm trong danh sách kết quả trừ khi Keepends được đưa ra và đúng.

Ví dụ:

[sign] ['0x'] integer ['.' fraction] ['p' exponent]
3

Ghi chú

Phiên bản bytearray của phương pháp này không hoạt động tại chỗ - nó luôn tạo ra một đối tượng mới, ngay cả khi không có thay đổi nào được thực hiện.

________ 1005 ________ 850 [Keepends = false]

Ghi chú

Phiên bản bytearray của phương pháp này không hoạt động tại chỗ - nó luôn tạo ra một đối tượng mới, ngay cả khi không có thay đổi nào được thực hiện.

________ 1005 ________ 850 [Keepends = false]

Trả về một danh sách các dòng trong chuỗi nhị phân, phá vỡ các ranh giới dòng ASCII. Phương pháp này sử dụng phương pháp tiếp cận NewLines phổ quát để phân tách các dòng. Phá vỡ dòng không được bao gồm trong danh sách kết quả trừ khi Keepends được đưa ra và đúng.

Không giống như

>>> float.fromhex['0x3.a7p10']
3740.0
32 khi một chuỗi phân cách được đưa ra, phương thức này trả về một danh sách trống cho chuỗi trống và ngắt dòng đầu cuối không dẫn đến một dòng bổ sung:

  1. Ký tự

    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    05, đánh dấu sự khởi đầu của trình xác định.

  2. Khóa ánh xạ [tùy chọn], bao gồm một chuỗi các ký tự được đặt dấu ngoặc đơn [ví dụ:

    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    06].

  3. Cờ chuyển đổi [tùy chọn], ảnh hưởng đến kết quả của một số loại chuyển đổi.

  4. Chiều rộng trường tối thiểu [tùy chọn]. Nếu được chỉ định là

    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    07 [dấu hoa thị], chiều rộng thực tế được đọc từ phần tử tiếp theo của bộ tple trong các giá trị và đối tượng để chuyển đổi xuất hiện sau chiều rộng trường tối thiểu và độ chính xác tùy chọn.

  5. Độ chính xác [tùy chọn], được đưa ra dưới dạng

    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    08 [DOT] theo sau là độ chính xác. Nếu được chỉ định là
    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    07 [dấu hoa thị], độ chính xác thực tế được đọc từ phần tử tiếp theo của bộ tuple trong các giá trị và giá trị để chuyển đổi xuất hiện sau độ chính xác.

  6. Công cụ sửa đổi độ dài [tùy chọn].

  7. Loại chuyển đổi.

Khi đối số đúng là một từ điển [hoặc loại ánh xạ khác], thì các định dạng trong đối tượng byte phải bao gồm khóa ánh xạ dấu ngoặc đơn vào từ điển đó được chèn ngay sau ký tự

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
05. Khóa ánh xạ chọn giá trị được định dạng từ ánh xạ. Ví dụ:

[sign] ['0x'] integer ['.' fraction] ['p' exponent]
4

Trong trường hợp này, không có nhà xác định

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
15 có thể xảy ra ở định dạng [vì chúng yêu cầu danh sách tham số tuần tự].

Các ký tự cờ chuyển đổi là:

Lá cờ

Nghĩa

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
12

Chuyển đổi giá trị sẽ sử dụng hình thức thay thế trên mạng [trong đó được xác định bên dưới].

>>> float.fromhex['0x3.a7p10']
3740.0
95

Việc chuyển đổi sẽ được đệm bằng 0 cho các giá trị số.

>>> float.fromhex['0x3.a7p10']
3740.0
97

Giá trị được chuyển đổi được điều chỉnh trái [ghi đè chuyển đổi

>>> float.fromhex['0x3.a7p10']
3740.0
95 nếu cả hai được đưa ra].

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
16

.

>>> float.fromhex['0x3.a7p10']
3740.0
96

Một ký tự dấu hiệu [

>>> float.fromhex['0x3.a7p10']
3740.0
96 hoặc
>>> float.fromhex['0x3.a7p10']
3740.0
97] sẽ đi trước chuyển đổi [ghi đè một lá cờ không gian trên mạng].

Một công cụ sửa đổi độ dài [

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
20,
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
21 hoặc
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
22] có thể xuất hiện, nhưng bị bỏ qua vì nó không cần thiết cho Python - vì vậy ví dụ:
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
23 giống hệt với
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
24.

Các loại chuyển đổi là:

Chuyển đổi

Nghĩa

Chuyển đổi giá trị sẽ sử dụng hình thức thay thế trên mạng [trong đó được xác định bên dưới].

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
25

Việc chuyển đổi sẽ được đệm bằng 0 cho các giá trị số.

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
26

Việc chuyển đổi sẽ được đệm bằng 0 cho các giá trị số.

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
27

Giá trị được chuyển đổi được điều chỉnh trái [ghi đè chuyển đổi

>>> float.fromhex['0x3.a7p10']
3740.0
95 nếu cả hai được đưa ra].

[1]

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
28

.

[8]

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
30

Một ký tự dấu hiệu [

>>> float.fromhex['0x3.a7p10']
3740.0
96 hoặc
>>> float.fromhex['0x3.a7p10']
3740.0
97] sẽ đi trước chuyển đổi [ghi đè một lá cờ không gian trên mạng].

[2]

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
31

Một công cụ sửa đổi độ dài [

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
20,
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
21 hoặc
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
22] có thể xuất hiện, nhưng bị bỏ qua vì nó không cần thiết cho Python - vì vậy ví dụ:
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
23 giống hệt với
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
24.

[2]

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
32

Các loại chuyển đổi là:

[3]

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
33

Chuyển đổi

[3]

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
34

Ghi chú

[3]

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
35

Ghi chú

[3]

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
36

Đã ký số nguyên thập phân.

[4]

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
37

Đã ký giá trị bát phân.

[4]

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
38

Loại lỗi thời - Nó giống hệt với

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
25.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
303

Đã ký tên thập lục phân [chữ thường].buffer protocol or has

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
304].

[5]

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
41

Đã ký tên thập lục phân [chữ hoa].

[6]

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
43

Định dạng theo cấp số mũ [chữ thường].

[5]

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
39

Định dạng số mũ điểm nổi [chữ hoa].

[7]

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
05

Định dạng số thập phân điểm nổi.

Notes:

  1. Định dạng điểm nổi. Sử dụng định dạng theo cấp số nhân nếu số mũ nhỏ hơn -4 hoặc không nhỏ hơn độ chính xác, định dạng thập phân nếu không.

  2. Định dạng điểm nổi. Sử dụng định dạng theo cấp số nhân Uppercase nếu số mũ nhỏ hơn -4 hoặc không nhỏ hơn độ chính xác, định dạng thập phân khác.

  3. Byte đơn [chấp nhận số nguyên hoặc các đối tượng byte đơn].

    Byte [bất kỳ đối tượng nào tuân theo giao thức bộ đệm hoặc có

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    304].

  4. >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    41 là bí danh cho
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    303 và chỉ nên được sử dụng cho các cơ sở mã Python2/3.

    Byte [chuyển đổi bất kỳ đối tượng Python nào bằng cách sử dụng

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    309].

  5. >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    39 là bí danh cho
    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    43 và chỉ nên được sử dụng cho các cơ sở mã Python2/3.

  6. Không có đối số được chuyển đổi, kết quả là một ký tự

    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    05 trong kết quả.

  7. Hình thức thay thế làm cho một bộ xác định octal hàng đầu [

    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    47] được chèn trước chữ số đầu tiên.

  8. Hình thức thay thế gây ra

    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    48 hoặc
    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    49 hàng đầu [tùy thuộc vào định dạng
    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    30 hay
    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    31 đã được sử dụng] để được chèn trước chữ số đầu tiên.PEP 237.

Hình thức thay thế làm cho kết quả luôn chứa một điểm thập phân, ngay cả khi không có chữ số nào tuân theo nó.

Độ chính xác xác định số lượng chữ số sau điểm thập phân và mặc định là 6.

Hình thức thay thế gây ra kết quả luôn chứa một điểm thập phân và các số 0 không được loại bỏ như chúng sẽ có.

Độ chính xác xác định số lượng các chữ số đáng kể trước và sau điểm thập phân và mặc định là 6. - Adding % formatting to bytes and bytearray

Nếu độ chính xác là

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
52, đầu ra bị cắt giảm thành
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
52 ký tự.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
322 không được chấp nhận, nhưng sẽ không bị xóa trong chuỗi 3.x.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
323 không được chấp nhận, nhưng sẽ không bị xóa trong chuỗi 3.x.buffer protocol without copying.

Lớp ________ 1325 [đối tượng] ¶[object]

Tạo một

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
60 tham chiếu đối tượng. Đối tượng phải hỗ trợ giao thức bộ đệm. Các đối tượng tích hợp hỗ trợ giao thức bộ đệm bao gồm
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
38 và
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
39.

Một

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
60 có khái niệm về một phần tử, đó là đơn vị bộ nhớ nguyên tử được xử lý bởi đối tượng gốc. Đối với nhiều loại đơn giản như
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
38 và
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
39, một phần tử là một byte duy nhất, nhưng các loại khác như
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
332 có thể có các phần tử lớn hơn.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
333 bằng chiều dài của
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
334. Nếu
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
335, độ dài là 1. Nếu
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
336, độ dài bằng số lượng phần tử trong chế độ xem. Đối với kích thước cao hơn, chiều dài bằng chiều dài của biểu diễn danh sách lồng nhau của chế độ xem. Thuộc tính
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
337 sẽ cung cấp cho bạn số lượng byte trong một phần tử.

Một

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
60 hỗ trợ cắt và lập chỉ mục để hiển thị dữ liệu của nó. Cắt một chiều sẽ dẫn đến một mục tiêu phụ:

[sign] ['0x'] integer ['.' fraction] ['p' exponent]
5

Nếu

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
339 là một trong những nhà xác định định dạng gốc từ mô -đun
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
340, thì việc lập chỉ mục với số nguyên hoặc một bộ số nguyên cũng được hỗ trợ và trả về một phần tử duy nhất với đúng loại. MemoryViews một chiều có thể được lập chỉ mục bằng một số nguyên hoặc một tuple một số nguyên. Bộ nhớ đa chiều có thể được lập chỉ mục với các bộ dữ liệu chính xác của các số nguyên NDIM trong đó NDIM là số lượng kích thước. MemoryViews không có chiều có thể được lập chỉ mục với bộ gốc trống.

Dưới đây là một ví dụ với định dạng không byte:

[sign] ['0x'] integer ['.' fraction] ['p' exponent]
6

Nếu đối tượng cơ bản có thể ghi, MemoryView hỗ trợ gán lát cắt một chiều. Không cho phép thay đổi kích thước:

[sign] ['0x'] integer ['.' fraction] ['p' exponent]
7

Bộ nhớ một chiều của các loại có thể băm [chỉ đọc] với các định dạng ‘B,‘ B, hoặc ‘C, cũng có thể băm. Hash được định nghĩa là

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
341:

[sign] ['0x'] integer ['.' fraction] ['p' exponent]
8

Đã thay đổi trong phiên bản 3.3: MemoryViews một chiều hiện có thể được cắt lát. MemoryViews một chiều với các định dạng ‘B,‘ B, hoặc ‘C, hiện có thể băm.One-dimensional memoryviews can now be sliced. One-dimensional memoryviews with formats ‘B’, ‘b’ or ‘c’ are now hashable.

Đã thay đổi trong phiên bản 3.5: MemoryViews hiện có thể được lập chỉ mục với bộ số của số nguyên.memoryviews can now be indexed with tuple of integers.

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
60 có một số phương pháp:

________ 1343 [xuất khẩu] ¶[exporter]

Một bộ nhớ và một nhà xuất khẩu PEP 3118 bằng nhau nếu hình dạng của chúng tương đương và nếu tất cả các giá trị tương ứng đều bằng nhau khi các mã định dạng tương ứng của operands được giải thích bằng cách sử dụng cú pháp

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
340.PEP 3118 exporter are equal if their shapes are equivalent and if all corresponding values are equal when the operands’ respective format codes are interpreted using
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
340 syntax.

Đối với tập hợp con của các chuỗi định dạng

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
340 hiện được hỗ trợ bởi
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
346,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
347 và
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
348 bằng nhau nếu
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
349:

[sign] ['0x'] integer ['.' fraction] ['p' exponent]
9

Nếu một trong hai chuỗi định dạng không được hỗ trợ bởi mô -đun

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
340, thì các đối tượng sẽ luôn so sánh là không đồng đều [ngay cả khi các chuỗi định dạng và nội dung bộ đệm là giống hệt nhau]:

>>> float.fromhex['0x3.a7p10']
3740.0
0

Lưu ý rằng, như với các số điểm nổi,

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
351 không ngụ ý
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
352 cho các đối tượng MemoryView.

Thay đổi trong phiên bản 3.3: Các phiên bản trước đã so sánh bộ nhớ thô không quan tâm đến định dạng vật phẩm và cấu trúc mảng logic.Previous versions compared the raw memory disregarding the item format and the logical array structure.

________ 1353 [đặt hàng = không] ¶[order=None]

Trả về dữ liệu trong bộ đệm dưới dạng bytestring. Điều này tương đương với việc gọi hàm tạo

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
38 trên MemoryView.

>>> float.fromhex['0x3.a7p10']
3740.0
1

Đối với các mảng không liên tục, kết quả bằng với biểu diễn danh sách phẳng với tất cả các phần tử được chuyển đổi thành byte.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
355 hỗ trợ tất cả các chuỗi định dạng, bao gồm cả các chuỗi không có trong cú pháp mô -đun
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
340.

Mới trong phiên bản 3.8: Đơn hàng có thể là {‘C,‘ f, ‘A,}. Khi đặt hàng là ‘C, hoặc‘ F, dữ liệu của mảng ban đầu được chuyển đổi thành thứ tự C hoặc Fortran. Đối với các chế độ xem tiếp giáp, ‘A, trả về một bản sao chính xác của bộ nhớ vật lý. Đặc biệt, trật tự Fortran trong bộ nhớ được bảo tồn. Đối với các chế độ xem không liên tục, dữ liệu được chuyển đổi thành C trước. Đặt hàng = không giống như thứ tự = xông c.order can be {‘C’, ‘F’, ‘A’}. When order is ‘C’ or ‘F’, the data of the original array is converted to C or Fortran order. For contiguous views, ‘A’ returns an exact copy of the physical memory. In particular, in-memory Fortran order is preserved. For non-contiguous views, the data is converted to C first. order=None is the same as order=’C’.

________ 318 [[sep [, byte_per_sep]]] ¶[[sep[, bytes_per_sep]]]

Trả về một đối tượng chuỗi chứa hai chữ số thập lục phân cho mỗi byte trong bộ đệm.

>>> float.fromhex['0x3.a7p10']
3740.0
2

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

Đã thay đổi trong phiên bản 3.8: Tương tự như

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
81,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
359 hiện hỗ trợ các tham số tùy chọn SEP và BYTES_PER_SEP để chèn các phân tách giữa các byte trong đầu ra hex.Similar to
>>> float.hex[3740.0]
'0x1.d380000000000p+11'
81,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
359 now supports optional sep and bytes_per_sep parameters to insert separators between bytes in the hex output.

________ 1360 [][]

Trả về dữ liệu trong bộ đệm dưới dạng danh sách các yếu tố.

>>> float.fromhex['0x3.a7p10']
3740.0
3

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

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
346 hiện hỗ trợ tất cả các định dạng gốc ký tự đơn trong cú pháp mô-đun
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
340 cũng như các biểu diễn đa chiều.
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
346 now supports all single character native formats in
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
340 module syntax as well as multi-dimensional representations.

________ 1363 [][]

Trả về một phiên bản đọc của đối tượng MemoryView. Đối tượng MemoryView ban đầu không thay đổi.

>>> float.fromhex['0x3.a7p10']
3740.0
4

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

________ 1364 [][]

Phát hành bộ đệm cơ bản được hiển thị bởi đối tượng MemoryView. Nhiều đối tượng thực hiện các hành động đặc biệt khi một chế độ xem được giữ trên chúng [ví dụ:

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
39 sẽ tạm thời cấm thay đổi kích thước]; Do đó, gọi phát hành [] rất tiện dụng để loại bỏ các hạn chế này [và giải phóng bất kỳ tài nguyên lơ lửng nào] càng sớm càng tốt.

Sau khi phương thức này đã được gọi, bất kỳ hoạt động nào nữa trong chế độ xem đều tăng

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
73 [ngoại trừ
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
367 có thể được gọi là nhiều lần]:

>>> float.fromhex['0x3.a7p10']
3740.0
5

Giao thức quản lý bối cảnh có thể được sử dụng cho một hiệu ứng tương tự, sử dụng câu lệnh

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
368:

>>> float.fromhex['0x3.a7p10']
3740.0
6

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

________ 1369 [định dạng [, hình]] ¶[format[, shape]]

Đúc một bộ nhớ vào một định dạng hoặc hình dạng mới. Hình dạng mặc định là

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
370, có nghĩa là chế độ xem kết quả sẽ là một chiều. Giá trị trả về là một bộ nhớ mới, nhưng bản thân bộ đệm không được sao chép. Các phôi được hỗ trợ là 1D -> C -TIÊU CHUẨN và C -TIÊU CHUẨN -> 1D.contiguous and C-contiguous -> 1D.

Định dạng đích được giới hạn ở một định dạng gốc nguyên tố trong cú pháp

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
340. Một trong những định dạng phải là định dạng byte [‘B,‘ B, hoặc ‘C,]. Độ dài byte của kết quả phải giống như chiều dài ban đầu.

Đúc 1D/dài đến 1D/byte không dấu:

>>> float.fromhex['0x3.a7p10']
3740.0
7

Đúc 1D/byte không dấu thành 1D/char:

>>> float.fromhex['0x3.a7p10']
3740.0
8

Đúc 1D/byte đến 3D/INTS đến 1D/đã ký char:

>>> float.fromhex['0x3.a7p10']
3740.0
9

Diễn viên 1D/không dấu dài đến 2D/không dấu dài:

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
0

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

Đã thay đổi trong phiên bản 3.5: Định dạng nguồn không còn bị hạn chế khi đúc vào chế độ xem byte.The source format is no longer restricted when casting to a byte view.

Ngoài ra còn có một số thuộc tính đọc có sẵn:

________ 1372¶

Đối tượng bên dưới của bộ nhớview:

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
1

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

Đã thay đổi trong phiên bản 3.5: Định dạng nguồn không còn bị hạn chế khi đúc vào chế độ xem byte.

Ngoài ra còn có một số thuộc tính đọc có sẵn:

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
2

________ 1372¶

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
3

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

Đã thay đổi trong phiên bản 3.5: Định dạng nguồn không còn bị hạn chế khi đúc vào chế độ xem byte.

Ngoài ra còn có một số thuộc tính đọc có sẵn:

________ 1372¶

Đối tượng bên dưới của bộ nhớview:

________ 1373¶format

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
380 is now handled according to the struct module syntax. This means that
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
381.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
374. Đây là lượng không gian trong byte mà mảng sẽ sử dụng trong một biểu diễn tiếp giáp. Nó không nhất thiết phải bằng
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
375:

Mảng đa chiều:

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
4

________ 1376¶

Một bool chỉ ra liệu bộ nhớ chỉ được đọc.

________ 694¶

Một chuỗi chứa định dạng [theo kiểu mô -đun

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
340] cho mỗi phần tử trong chế độ xem. Một bộ nhớ có thể được tạo từ các nhà xuất khẩu có chuỗi định dạng tùy ý, nhưng một số phương thức [ví dụ:
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
346] bị giới hạn ở các định dạng phần tử đơn lẻ.

Đã thay đổi trong phiên bản 3.3: Định dạng

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
380 hiện được xử lý theo cú pháp mô -đun cấu trúc. Điều này có nghĩa là
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
381.An empty tuple instead of
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26 when ndim = 0.

________ 1382¶

Kích thước tính bằng byte của mỗi phần tử của bộ nhớView:

Đã thay đổi trong phiên bản 3.3: Định dạng

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
380 hiện được xử lý theo cú pháp mô -đun cấu trúc. Điều này có nghĩa là
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
381.An empty tuple instead of
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26 when ndim = 0.

________ 1382¶

Kích thước tính bằng byte của mỗi phần tử của bộ nhớView:

________ 1383¶

Một số nguyên cho biết có bao nhiêu kích thước của một mảng đa chiều mà bộ nhớ đại diện.contiguous.

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

Đã thay đổi trong phiên bản 3.5: Định dạng nguồn không còn bị hạn chế khi đúc vào chế độ xem byte.

Ngoài ra còn có một số thuộc tính đọc có sẵn:contiguous.

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

Đã thay đổi trong phiên bản 3.5: Định dạng nguồn không còn bị hạn chế khi đúc vào chế độ xem byte.

Ngoài ra còn có một số thuộc tính đọc có sẵn:contiguous.

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

Đã thay đổi trong phiên bản 3.5: Định dạng nguồn không còn bị hạn chế khi đúc vào chế độ xem byte.

Ngoài ra còn có một số thuộc tính đọc có sẵn:hashable objects. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference. [For other containers see the built-in

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
83,
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
08, and
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
09 classes, and the
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
399 module.]

________ 1372¶

Đối tượng bên dưới của bộ nhớview:hashable — its contents cannot be altered after it is created; it can therefore be used as a dictionary key or as an element of another set.

________ 1373¶

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
374. Đây là lượng không gian trong byte mà mảng sẽ sử dụng trong một biểu diễn tiếp giáp. Nó không nhất thiết phải bằng
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
375:

Mảng đa chiều:[[iterable]]¶ class
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
412[[iterable]]

________ 1376¶hashable. To represent sets of sets, the inner sets must be

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
85 objects. If iterable is not specified, a new empty set is returned.

Sets can be created by several means:

  • Use a comma-separated list of elements within braces:

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    409

  • Use a set comprehension:

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    415

  • Use the type constructor:

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    46,
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    417,
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    418

Instances of

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
84 and
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
85 provide the following operations:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
421

Return the number of elements in set s [cardinality of s].

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
422

Test x for membership in s.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
423

Test x for non-membership in s.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
424[other]

Return

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51 if the set has no elements in common with other. Sets are disjoint if and only if their intersection is the empty set.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
426[other]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
427

Test whether every element in the set is in other.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
428

Test whether the set is a proper subset of other, that is,

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
429.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
430[other]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
431

Test whether every element in other is in the set.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
432

Test whether the set is a proper superset of other, that is,

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
433.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
434[*others]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
435

Return a new set with elements from the set and all others.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
436[*others]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
437

Return a new set with elements common to the set and all others.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
438[*others]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
439

Return a new set with elements in the set that are not in the others.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
440[other]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
441

Return a new set with elements in either the set or other but not both.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
442[]

Return a shallow copy of the set.

Note, the non-operator versions of

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
443,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
444,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
445,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
446,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
447, and
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
448 methods will accept any iterable as an argument. In contrast, their operator based counterparts require their arguments to be sets. This precludes error-prone constructions like
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
449 in favor of the more readable
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
450.

Both

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
84 and
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
85 support set to set comparisons. Two sets are equal if and only if every element of each set is contained in the other [each is a subset of the other]. A set is less than another set if and only if the first set is a proper subset of the second set [is a subset, but is not equal]. A set is greater than another set if and only if the first set is a proper superset of the second set [is a superset, but is not equal].

Instances of

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
84 are compared to instances of
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
85 based on their members. For example,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
455 returns
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51 and so does
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
457.

The subset and equality comparisons do not generalize to a total ordering function. For example, any two nonempty disjoint sets are not equal and are not subsets of each other, so all of the following return

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33:
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
459,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
460, or
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
461.

Since sets only define partial ordering [subset relationships], the output of the

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
462 method is undefined for lists of sets.

Set elements, like dictionary keys, must be hashable.

Binary operations that mix

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
84 instances with
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
85 return the type of the first operand. For example:
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
465 returns an instance of
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
85.

The following table lists operations available for

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
84 that do not apply to immutable instances of
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
85:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
469[*others]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
470

Update the set, adding elements from all others.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
471[*others]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
472

Update the set, keeping only elements found in it and all others.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
473[*others]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
474

Update the set, removing elements found in others.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
475[other]
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
476

Update the set, keeping only elements found in either set, but not in both.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
477[elem]

Add element elem to the set.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
478[elem]

Remove element elem from the set. Raises

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
479 if elem is not contained in the set.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
480[elem]

Remove element elem from the set if it is present.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
481[]

Remove and return an arbitrary element from the set. Raises

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
479 if the set is empty.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
483[]

Remove all elements from the set.

Note, the non-operator versions of the

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
484,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
485,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
486, and
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
487 methods will accept any iterable as an argument.

Note, the elem argument to the

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
95,
>>> int.from_bytes[b'\x00\x10', byteorder='big']
16
>>> int.from_bytes[b'\x00\x10', byteorder='little']
4096
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=True]
-1024
>>> int.from_bytes[b'\xfc\x00', byteorder='big', signed=False]
64512
>>> int.from_bytes[[255, 0, 0], byteorder='big']
16711680
16, and
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
490 methods may be a set. To support searching for an equivalent frozenset, a temporary one is created from elem.

Mapping Types —
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
83¶

A mapping object maps hashable values to arbitrary objects. Mappings are mutable objects. There is currently only one standard mapping type, the dictionary. [For other containers see the built-in

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
08,
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
84, and
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
09 classes, and the
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
399 module.]

A dictionary’s keys are almost arbitrary values. Values that are not hashable, that is, values containing lists, dictionaries or other mutable types [that are compared by value rather than by object identity] may not be used as keys. Numeric types used for keys obey the normal rules for numeric comparison: if two numbers compare equal [such as

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
50 and
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
497] then they can be used interchangeably to index the same dictionary entry. [Note however, that since computers store floating-point numbers as approximations it is usually unwise to use them as dictionary keys.]

Lớp ________ 1498 [** kwargs] ¶ Class ________ 1498 [Bản đồ, ** kwargs] Lớp ____ 1498 [có thể sử dụng được, ** kwargs][**kwargs]¶ class
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
498[mapping, **kwargs] class
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
498[iterable, **kwargs]

Trả về một từ điển mới được khởi tạo từ một đối số vị trí tùy chọn và một tập hợp các đối số từ khóa có thể trống.

Từ điển có thể được tạo bằng một số phương tiện:

  • Sử dụng danh sách các cặp

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    501 được phân tách bằng dấu phẩy trong niềng răng:
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    502 hoặc
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    503

  • Sử dụng sự hiểu biết chính thống:

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    45,
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    505

  • Sử dụng hàm tạo loại:

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    506,
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    507,
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    508

Nếu không có đối số vị trí nào được đưa ra, một từ điển trống được tạo. Nếu một đối số vị trí được đưa ra và nó là một đối tượng ánh xạ, một từ điển được tạo với cùng các cặp giá trị khóa như đối tượng ánh xạ. Nếu không, đối số vị trí phải là một đối tượng có thể lặp lại. Mỗi mục trong Iterable phải tự nó là một đối tượng chính xác với hai đối tượng. Đối tượng đầu tiên của mỗi mục trở thành một khóa trong từ điển mới và đối tượng thứ hai là giá trị tương ứng. Nếu một khóa xảy ra nhiều lần, giá trị cuối cùng cho khóa đó trở thành giá trị tương ứng trong từ điển mới.iterable object. Each item in the iterable must itself be an iterable with exactly two objects. The first object of each item becomes a key in the new dictionary, and the second object the corresponding value. If a key occurs more than once, the last value for that key becomes the corresponding value in the new dictionary.

Nếu các đối số từ khóa được đưa ra, các đối số từ khóa và giá trị của chúng được thêm vào từ điển được tạo từ đối số vị trí. Nếu một khóa được thêm vào đã có mặt, giá trị từ đối số từ khóa sẽ thay thế giá trị từ đối số vị trí.

Để minh họa, tất cả các ví dụ sau đều trả về từ điển bằng

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
509:

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
5

Cung cấp các đối số từ khóa như trong ví dụ đầu tiên chỉ hoạt động cho các khóa là định danh python hợp lệ. Nếu không, bất kỳ khóa hợp lệ nào cũng có thể được sử dụng.

Đây là các hoạt động mà từ điển hỗ trợ [và do đó, các loại ánh xạ tùy chỉnh cũng sẽ hỗ trợ]:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
510

Trả về một danh sách tất cả các khóa được sử dụng trong từ điển d.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
511

Trả lại số lượng các mục trong từ điển d.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
512

Trả lại mục của D với khóa khóa. Tăng

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
479 nếu khóa không có trong bản đồ.

Nếu một lớp con của dict định nghĩa một phương thức

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
514 và khóa không có, thì hoạt động
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
515 sẽ gọi phương thức đó với khóa khóa làm đối số. Hoạt động
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
515 sau đó trả về hoặc tăng bất cứ thứ gì được trả lại hoặc tăng lên bởi cuộc gọi
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
517. Không có hoạt động hoặc phương pháp nào khác gọi
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
514. Nếu
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
514 không được xác định,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
479 sẽ được nâng lên.
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
514 phải là một phương pháp; Nó không thể là một biến thể hiện:

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
6

Ví dụ trên cho thấy một phần của việc thực hiện

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
522. Một phương pháp
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
523 khác nhau được sử dụng bởi
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
524.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
525

Đặt

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
515 thành Giá trị.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
527

Hủy bỏ

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
515 khỏi d. Tăng
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
479 nếu khóa không có trong bản đồ.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
530

Trả về

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51 Nếu D có khóa khóa, nếu không
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
533

Tương đương với

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
534.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
535

Trả lại một trình lặp qua các khóa của từ điển. Đây là một lối tắt cho

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
536.

________ 1483 [][]

Hủy bỏ tất cả các mục khỏi từ điển.

________ 1442 [][]

Trả lại một bản sao nông của từ điển.

ClassMethod ________ 1539 [Itable [, value]] ¶[iterable[, value]]

Tạo một từ điển mới với các khóa từ Itable và các giá trị được đặt thành giá trị.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
540 là một phương pháp lớp trả về một từ điển mới. Giá trị mặc định là
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26. Tất cả các giá trị chỉ đề cập đến một trường hợp duy nhất, do đó, nó thường không có ý nghĩa đối với giá trị là một đối tượng có thể thay đổi như một danh sách trống. Để có được các giá trị khác biệt, thay vào đó hãy sử dụng sự hiểu biết của Dict.dict comprehension instead.

________ 1542 [khóa [, mặc định]] ¶[key[, default]]

Trả về giá trị cho khóa nếu có trong từ điển, mặc định khác. Nếu mặc định không được đưa ra, nó mặc định là

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26, do đó phương thức này không bao giờ tăng
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
479.

________ 1545 [][]

Trả về một cái nhìn mới về các mục từ điển [

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
546 cặp]. Xem tài liệu của các đối tượng xem.documentation of view objects.

________ 1547 [][]

Trả lại một cái nhìn mới về các khóa từ điển. Xem tài liệu của các đối tượng xem.documentation of view objects.

________ 1481 [khóa [, mặc định]] ¶[key[, default]]

Nếu khóa nằm trong từ điển, hãy xóa nó và trả về giá trị của nó, khác trả về mặc định. Nếu mặc định không được đưa ra và khóa không có trong từ điển,

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
479 sẽ được nâng lên.

________ 1550 [][]

Xóa và trả về một cặp

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
546 khỏi từ điển. Các cặp được trả lại theo thứ tự LIFO.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
552 rất hữu ích để lặp lại một cách phá hủy trên một từ điển, như thường được sử dụng trong các thuật toán đã đặt. Nếu từ điển trống, gọi
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
552 sẽ tăng
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
479.

Đã thay đổi trong phiên bản 3.7: Thứ tự LIFO hiện được đảm bảo. Trong các phiên bản trước,

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
552 sẽ trả về một cặp khóa/giá trị tùy ý.LIFO order is now guaranteed. In prior versions,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
552 would return an arbitrary key/value pair.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
556

Trả lại một trình lặp ngược qua các khóa của từ điển. Đây là một lối tắt cho

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
557.

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

________ 1558 [khóa [, mặc định]] ¶[key[, default]]

Nếu khóa nằm trong từ điển, hãy trả về giá trị của nó. Nếu không, chèn phím với giá trị mặc định và trả về mặc định. Mặc định mặc định là

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26.

________ 1469 [[Khác]][[other]]

Cập nhật từ điển với các cặp khóa/giá trị từ các khóa hiện có, ghi đè lên. Trả lại

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
484 chấp nhận một đối tượng từ điển khác hoặc có thể lặp lại của các cặp khóa/giá trị [như bộ dữ liệu hoặc các vòng lặp khác có độ dài hai]. Nếu các đối số từ khóa được chỉ định, từ điển sau đó được cập nhật với các cặp khóa/giá trị đó:
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
563.

________ 1564 [][]

Trả về một cái nhìn mới về các giá trị từ điển. Xem tài liệu của các đối tượng xem.documentation of view objects.

Một so sánh bình đẳng giữa một chế độ xem

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
565 và một chế độ khác sẽ luôn quay trở lại
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33. Điều này cũng áp dụng khi so sánh
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
565 với chính nó:

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
7

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
568

Tạo một từ điển mới với các khóa và giá trị được hợp nhất của D và khác, cả hai đều phải là từ điển. Các giá trị của sự ưu tiên khác khi D và các khóa chia sẻ khác.

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

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
569

Cập nhật từ điển D với các khóa và giá trị từ khác, có thể là ánh xạ hoặc có thể lặp lại các cặp khóa/giá trị. Các giá trị của sự ưu tiên khác khi D và các khóa chia sẻ khác.mapping or an iterable of key/value pairs. The values of other take priority when d and other share keys.

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

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
569

Cập nhật từ điển D với các khóa và giá trị từ khác, có thể là ánh xạ hoặc có thể lặp lại các cặp khóa/giá trị. Các giá trị của sự ưu tiên khác khi D và các khóa chia sẻ khác.

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
8

Từ điển so sánh bằng nhau khi và chỉ khi chúng có cùng cặp

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
570 [bất kể đặt hàng]. So sánh đơn hàng [‘] tăng
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
83.Dictionary order is guaranteed to be insertion order. This behavior was an implementation detail of CPython from 3.6.

Từ điển bảo tồn thứ tự chèn. Lưu ý rằng việc cập nhật khóa không ảnh hưởng đến thứ tự. Các khóa được thêm vào sau khi xóa được chèn vào cuối.

>>> float.hex[3740.0]
'0x1.d380000000000p+11'
9

Thay đổi trong phiên bản 3.7: Thứ tự từ điển được đảm bảo là thứ tự chèn. Hành vi này là một chi tiết thực hiện của CPython từ 3.6.Dictionaries are now reversible.

Từ điển và quan điểm từ điển có thể đảo ngược.

Thay đổi trong phiên bản 3.8: Từ điển hiện có thể đảo ngược.

View Dictionary View Object;

Các đối tượng được trả về bởi
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
572,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
565 và
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
574 là các đối tượng xem. Chúng cung cấp một cái nhìn động trên các mục từ điển, điều đó có nghĩa là khi từ điển thay đổi, quan điểm phản ánh những thay đổi này.

Các chế độ xem từ điển có thể được lặp lại để mang lại dữ liệu tương ứng của họ và hỗ trợ các bài kiểm tra thành viên:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
575

Trả lại số lượng mục trong từ điển.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
576

Trả về một trình lặp qua các khóa, giá trị hoặc mục [được biểu thị dưới dạng các bộ dữ liệu của

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
546] trong từ điển.

Các khóa và giá trị được lặp lại theo thứ tự chèn. Điều này cho phép tạo các cặp

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
578 bằng cách sử dụng
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
579:
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
580. Một cách khác để tạo cùng một danh sách là
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
581.Dictionary order is guaranteed to be insertion order.

Lặp lại các chế độ xem trong khi thêm hoặc xóa các mục trong từ điển có thể tăng
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
582 hoặc không lặp lại tất cả các mục.

Thay đổi trong phiên bản 3.7: Thứ tự từ điển được đảm bảo là thứ tự chèn.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
583

Trả về

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51 Nếu X nằm trong các khóa, giá trị hoặc mục từ điển cơ bản [trong trường hợp sau, X nên là một bộ
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
546].

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
586Dictionary views are now reversible.

Trả về một trình lặp ngược qua các khóa, giá trị hoặc mục của từ điển. Quan điểm sẽ được lặp lại theo thứ tự ngược của chèn.

Đã thay đổi trong phiên bản 3.8: Quan điểm từ điển hiện có thể đảo ngược.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
587

Trả về một

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
588 kết thúc từ điển ban đầu mà chế độ xem đề cập.

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

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
00

Các chế độ xem khóa được đặt giống như các mục của chúng là duy nhất và có thể băm. Nếu tất cả các giá trị có thể băm, do đó các cặp
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
546 là duy nhất và có thể băm, thì chế độ xem các mục cũng giống như được đặt. .

Một ví dụ về cách sử dụng từ điển xem:

Trình quản lý bối cảnh Loại[]

Tuyên bố của Python từ

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
368 hỗ trợ khái niệm về bối cảnh thời gian chạy được xác định bởi người quản lý bối cảnh. Điều này được triển khai bằng cách sử dụng một cặp phương thức cho phép các lớp do người dùng xác định xác định bối cảnh thời gian chạy được nhập trước khi phần thân câu lệnh được thực thi và thoát khi câu lệnh kết thúc:

________ 1595 ________ 1596 []file object. File objects return themselves from __enter__[] to allow

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
599 to be used as the context expression in a
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
368 statement.

Nhập bối cảnh thời gian chạy và trả về đối tượng này hoặc đối tượng khác liên quan đến bối cảnh thời gian chạy. Giá trị được trả về bởi phương thức này được liên kết với định danh trong mệnh đề

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
597 của các câu lệnh
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
368 bằng cách sử dụng trình quản lý ngữ cảnh này.

________ 1595 ________ 1605 [exc_type, exc_val, exc_tb] ¶[exc_type, exc_val, exc_tb]

Thoát khỏi bối cảnh thời gian chạy và trả lại cờ Boolean cho biết nếu có bất kỳ ngoại lệ nào xảy ra nên bị triệt tiêu. Nếu một ngoại lệ xảy ra trong khi thực hiện phần thân của câu lệnh

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
368, các đối số chứa loại ngoại lệ, giá trị và thông tin theo dõi. Mặt khác, cả ba đối số là
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26.

Trả về một giá trị thực từ phương thức này sẽ khiến câu lệnh

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
368 triệt tiêu ngoại lệ và tiếp tục thực thi với câu lệnh ngay sau câu lệnh
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
368. Nếu không, ngoại lệ tiếp tục lan truyền sau khi phương pháp này đã hoàn tất việc thực hiện. Các ngoại lệ xảy ra trong quá trình thực hiện phương pháp này sẽ thay thế bất kỳ ngoại lệ nào xảy ra trong phần thân của câu lệnh
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
368.

Ngoại lệ được thông qua không bao giờ nên được đọc lại một cách rõ ràng - thay vào đó, phương pháp này sẽ trả về một giá trị sai để chỉ ra rằng phương pháp đã hoàn thành thành công và không muốn triệt tiêu ngoại lệ được nâng lên. Điều này cho phép mã quản lý bối cảnh dễ dàng phát hiện xem phương thức

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
611 có thực sự thất bại hay không.

Python xác định một số người quản lý bối cảnh để hỗ trợ đồng bộ hóa luồng dễ dàng, đóng nhanh các tệp hoặc các đối tượng khác và thao tác đơn giản hơn về bối cảnh số học thập phân hoạt động. Các loại cụ thể không được đối xử đặc biệt ngoài việc thực hiện giao thức quản lý bối cảnh. Xem mô -đun

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
612 để biết một số ví dụ.

Máy phát điện Python và máy trang trí

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
613 cung cấp một cách thuận tiện để thực hiện các giao thức này. Nếu một hàm máy phát được trang trí với trình trang trí
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
613, nó sẽ trả về một trình quản lý bối cảnh thực hiện các phương thức
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
615 và
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
611 cần thiết, thay vì trình lặp được tạo ra bởi hàm máy phát không được trang trí.generators and the
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
613 decorator provide a convenient way to implement these protocols. If a generator function is decorated with the
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
613 decorator, it will return a context manager implementing the necessary
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
615 and
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
611 methods, rather than the iterator produced by an undecorated generator function.

Lưu ý rằng không có khe cắm cụ thể nào cho bất kỳ phương pháp nào trong cấu trúc loại cho các đối tượng Python trong API Python/C. Các loại mở rộng muốn xác định các phương pháp này phải cung cấp cho chúng như một phương thức có thể truy cập Python bình thường. So với chi phí của việc thiết lập bối cảnh thời gian chạy, chi phí của một tra cứu từ điển lớp là không đáng kể.

Loại chú thích các loại - Bí danh chung, Union¶Generic Alias, Union

Các loại tích hợp cốt lõi cho các chú thích loại là bí danh chung và liên minh.type annotations are Generic Alias and Union.

Bí danh chung Loại

Các đối tượng

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
617 thường được tạo bằng cách đăng ký một lớp. Chúng thường được sử dụng với các lớp container, chẳng hạn như
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
08 hoặc
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
83. Ví dụ:
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
620 là đối tượng
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
617 được tạo bằng cách đăng ký lớp
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
08 với đối số
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
96. Các đối tượng
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
617 được dự định chủ yếu để sử dụng với các chú thích loại.subscripting a class. They are most often used with container classes, such as
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
08 or
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
83. For example,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
620 is a
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
617 object created by subscripting the
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
08 class with the argument
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
96.
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
617 objects are intended primarily for use with type annotations.

Ghi chú

Nhìn chung chỉ có thể đăng ký một lớp nếu lớp thực hiện phương pháp đặc biệt

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
625.

Đối tượng

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
617 hoạt động như một proxy cho một loại chung, thực hiện các thuốc generic được tham số hóa.generic type, implementing parameterized generics.

Đối với một lớp container, [các] đối số được cung cấp cho một thuê bao của lớp có thể chỉ ra loại [các] các phần tử mà một đối tượng chứa. Ví dụ,

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
627 có thể được sử dụng trong các chú thích loại để biểu thị
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
84 trong đó tất cả các yếu tố thuộc loại
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
38.subscription of the class may indicate the type[s] of the elements an object contains. For example,
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
627 can be used in type annotations to signify a
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
84 in which all the elements are of type
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
38.

Đối với một lớp xác định

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
625 nhưng không phải là container, [các] đối số được cung cấp cho đăng ký của lớp thường sẽ chỉ ra loại trả về của một hoặc nhiều phương thức được xác định trên một đối tượng. Ví dụ:
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
631 có thể được sử dụng trên cả loại dữ liệu
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
37 và kiểu dữ liệu
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
38:

  • Nếu

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    634,
    >>> n = 19
    >>> bin[n]
    '0b10011'
    >>> n.bit_count[]
    3
    >>> [-n].bit_count[]
    3
    
    80 sẽ là đối tượng Re.Match trong đó các giá trị trả về của
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    636 và
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    637 sẽ cả hai loại
    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    37. Chúng ta có thể đại diện cho loại đối tượng này trong các chú thích loại với
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    617
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    640.re.Match object where the return values of
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    636 and
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    637 will both be of type
    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    37. We can represent this kind of object in type annotations with the
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    617
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    640.

  • Nếu

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    641, [lưu ý
    >>> float.hex[3740.0]
    '0x1.d380000000000p+11'
    
    66 cho
    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    38],
    def bit_count[self]:
        return bin[self].count["1"]
    
    40 cũng sẽ là một ví dụ của
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    645, nhưng các giá trị trả về của
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    646 và
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    647 sẽ cả hai đều thuộc loại
    >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    38. Trong các chú thích loại, chúng tôi sẽ đại diện cho sự đa dạng của các đối tượng Re.Match này với
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    649.re.Match objects with
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    649.

Các đối tượng

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
617 là các trường hợp của lớp
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
651, cũng có thể được sử dụng để tạo trực tiếp các đối tượng
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
617.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
653

Tạo một

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
617 đại diện cho loại
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
655 được tham số hóa theo loại X, Y và nhiều hơn tùy thuộc vào
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
655 được sử dụng. Ví dụ: một hàm mong đợi một
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
08 chứa các yếu tố
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
97:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
01

Một ví dụ khác để ánh xạ các đối tượng, sử dụng

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
83, đây là loại chung mong đợi hai tham số loại đại diện cho loại khóa và loại giá trị. Trong ví dụ này, hàm này mong đợi một
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
83 với các khóa loại
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
37 và các giá trị của loại
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
96:mapping objects, using a
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
83, which is a generic type expecting two type parameters representing the key type and the value type. In this example, the function expects a
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
83 with keys of type
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
37 and values of type
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
96:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
02

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

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
663 và
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
664 không chấp nhận các loại
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
617 cho đối số thứ hai của họ:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
03

Thời gian chạy Python không thực thi các chú thích loại. Điều này mở rộng sang các loại chung và các tham số loại của chúng. Khi tạo một đối tượng container từ

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
617, các phần tử trong thùng chứa không được kiểm tra so với loại của chúng. Ví dụ: mã sau không được khuyến khích, nhưng sẽ chạy mà không có lỗi:type annotations. This extends to generic types and their type parameters. When creating a container object from a
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
617, the elements in the container are not checked against their type. For example, the following code is discouraged, but will run without errors:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
04

Hơn nữa, các tham số loại thuốc generic được tham số hóa trong quá trình tạo đối tượng:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
05

Gọi

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
27 hoặc
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
28 trên một loại chung hiển thị loại tham số hóa:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
06

Phương pháp

>>> float.fromhex['0x3.a7p10']
3740.0
83 của các thùng chứa chung sẽ nêu ra một ngoại lệ đối với các lỗi không cho phép như
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
670:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
07

Tuy nhiên, các biểu thức như vậy là hợp lệ khi các biến loại được sử dụng. Chỉ mục phải có nhiều yếu tố như có các mục biến loại trong đối tượng

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
617
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
672.type variables are used. The index must have as many elements as there are type variable items in the
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
617 object’s
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
672.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
08

Các lớp học chung tiêu chuẩn

Các lớp thư viện tiêu chuẩn sau đây hỗ trợ các thuốc generic được tham số hóa. Danh sách này là không khởi công.

  • >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    09

  • >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    08

  • >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    83

  • >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    84

  • >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    85

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    678

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    679

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    524

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    681

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    522

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    683

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    684

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    685

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    686

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    687

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    688

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    689

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    690

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    691

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    692

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    693

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    694

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    695

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    590

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    697

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    698

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    699

  • >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    11

  • >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    87

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    702

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    703

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    704

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    705

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    706

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    707

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    708

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    709

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    710

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    711

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    712

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    713

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    714

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    715

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    716

  • re.Pattern

  • re.Match

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    717

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    718

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    719

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    588

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    721

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    722

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    723

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    724

Các thuộc tính đặc biệt của
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
617 Đối tượng

Tất cả các chung chung tham số thực hiện các thuộc tính chỉ đọc đặc biệt.

________ 1726 ________ 1727¶

Thuộc tính này chỉ vào lớp chung không tham số:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
09

________ 1726 ________ 1729¶

Thuộc tính này là

>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
09 [có thể là độ dài 1] của các loại chung được truyền cho
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
625 gốc của lớp chung:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
10

________ 1726 ________ 1733¶

Thuộc tính này là một tuple được tính toán một cách lười biếng [có thể trống] của các biến loại duy nhất được tìm thấy trong

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
672:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
11

Ghi chú

Một đối tượng

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
617 với các tham số
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
736 có thể không đúng
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
737 sau khi thay thế vì
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
736 chủ yếu được dự định để kiểm tra loại tĩnh.

Xem thêm

PEP 484 - Loại gợi ý - Type Hints

Giới thiệu khung Python sườn cho các chú thích loại.

PEP 585 - Loại Generics Generics trong các bộ sưu tập tiêu chuẩn - Type Hinting Generics In Standard Collections

Giới thiệu khả năng tham số hóa các lớp thư viện tiêu chuẩn, miễn là họ thực hiện phương pháp lớp đặc biệt

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
625.

Generics, Generics do người dùng định nghĩa và
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
740
, user-defined generics and
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
740

Tài liệu về cách thực hiện các lớp chung có thể được tham số hóa trong thời gian chạy và được hiểu bởi các trình kiểm tra loại tĩnh.

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

Loại Liên minh

Một đối tượng Union giữ giá trị của hoạt động

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
741 [bitwise hoặc] trên nhiều đối tượng loại. Những loại này được dự định chủ yếu cho các chú thích loại. Biểu thức loại liên minh cho phép loại cú pháp gợi ý loại sạch hơn so với
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
742.type objects. These types are intended primarily for type annotations. The union type expression enables cleaner type hinting syntax compared to
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
742.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
743

Xác định một đối tượng Liên minh giữ các loại X, Y, v.v.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
744 có nghĩa là X hoặc Y. Nó tương đương với
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
745. Ví dụ: hàm sau đây mong đợi một đối số của loại
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
96 hoặc
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
97:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
12

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
748

Các đối tượng Liên minh có thể được kiểm tra sự bình đẳng với các đối tượng liên minh khác. Thông tin chi tiết:

  • Các công đoàn bị san phẳng:

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    13

  • Các loại dự phòng đã bị xóa:

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    14

  • Khi so sánh các công đoàn, thứ tự bị bỏ qua:

  • Nó tương thích với

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    742:

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    15

  • Các loại tùy chọn có thể được đánh vần là một liên minh với

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    26:

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    16

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
751
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
752

Các cuộc gọi đến

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
663 và
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
664 cũng được hỗ trợ với một đối tượng Liên minh:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
17

Tuy nhiên, không thể sử dụng các đối tượng liên minh chứa chung chung được tham số:parameterized generics cannot be used:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
18

Loại tiếp xúc với người dùng cho đối tượng Liên minh có thể được truy cập từ

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
755 và được sử dụng để kiểm tra
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
663. Một đối tượng không thể được khởi tạo từ loại:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
19

Ghi chú

Một đối tượng

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
617 với các tham số
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
736 có thể không đúng
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
737 sau khi thay thế vì
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
736 chủ yếu được dự định để kiểm tra loại tĩnh.

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
20

Xem thêm

PEP 484 - Loại gợi ý – PEP proposing the

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
744 syntax and the Union type.

Giới thiệu khung Python sườn cho các chú thích loại.

PEP 585 - Loại Generics Generics trong các bộ sưu tập tiêu chuẩn

Giới thiệu khả năng tham số hóa các lớp thư viện tiêu chuẩn, miễn là họ thực hiện phương pháp lớp đặc biệt

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
625.

Generics, Generics do người dùng định nghĩa và
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
740

Tài liệu về cách thực hiện các lớp chung có thể được tham số hóa trong thời gian chạy và được hiểu bởi các trình kiểm tra loại tĩnh.

Một thuộc tính đặc biệt của mỗi mô -đun là

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
764. Đây là từ điển chứa bảng ký hiệu mô -đun. Việc sửa đổi từ điển này thực sự sẽ thay đổi bảng ký hiệu mô -đun, nhưng không thể gán trực tiếp cho thuộc tính
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
764 là không thể [bạn có thể viết
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
766, xác định
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
767 là
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
50, nhưng bạn có thể viết
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
769]. Sửa đổi
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
764 trực tiếp không được khuyến nghị.

Các mô -đun được xây dựng vào phiên dịch được viết như thế này:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
771. Nếu được tải từ một tệp, chúng được viết là
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
772.

Các lớp học và trường hợp lớp

Xem các đối tượng, giá trị và loại và định nghĩa lớp cho những điều này.Objects, values and types and Class definitions for these.

Chức năng¶

Các đối tượng chức năng được tạo bởi các định nghĩa chức năng. Hoạt động duy nhất trên một đối tượng hàm là gọi nó:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
773.

Thực sự có hai hương vị của các đối tượng chức năng: các chức năng tích hợp và các chức năng do người dùng xác định. Cả hai đều hỗ trợ cùng một hoạt động [để gọi hàm], nhưng việc triển khai là khác nhau, do đó các loại đối tượng khác nhau.

Xem định nghĩa chức năng để biết thêm thông tin.Function definitions for more information.

Phương pháp bình

Các phương thức là các hàm được gọi bằng cách sử dụng ký hiệu thuộc tính. Có hai hương vị: các phương thức tích hợp [như

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
774 trên danh sách] và phương thức thể hiện lớp. Các phương pháp tích hợp được mô tả với các loại hỗ trợ chúng.

Nếu bạn truy cập một phương thức [một hàm được xác định trong không gian tên lớp] thông qua một thể hiện, bạn sẽ nhận được một đối tượng đặc biệt: một phương thức ràng buộc [còn gọi là phương thức thể hiện] đối tượng. Khi được gọi, nó sẽ thêm đối số

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
775 vào danh sách đối số. Các phương thức ràng buộc có hai thuộc tính chỉ đọc đặc biệt:
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
776 là đối tượng mà phương thức hoạt động và
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
777 là chức năng thực hiện phương thức. Gọi
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
778 hoàn toàn tương đương với việc gọi
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
779.

Giống như các đối tượng chức năng, các đối tượng phương thức ràng buộc hỗ trợ nhận các thuộc tính tùy ý. Tuy nhiên, do các thuộc tính phương thức thực sự được lưu trữ trên đối tượng hàm cơ bản [

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
780], nên việc thiết lập các thuộc tính phương thức trên các phương thức bị ràng buộc không được phép. Cố gắng đặt một thuộc tính trên một phương thức dẫn đến một
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
781 đang được nâng lên. Để đặt thuộc tính phương thức, bạn cần đặt nó một cách rõ ràng trên đối tượng hàm cơ bản:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
21

Xem hệ thống phân cấp loại tiêu chuẩn để biết thêm thông tin.The standard type hierarchy for more information.

Mã đối tượng

Các đối tượng mã được sử dụng bởi việc triển khai để thể hiện mã Python thực thi của Pseudo được biên dịch bằng cách sử dụng như một cơ thể chức năng. Chúng khác với các đối tượng chức năng vì chúng không chứa một tham chiếu đến môi trường thực hiện toàn cầu của chúng. Các đối tượng mã được trả về bởi hàm

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
782 tích hợp và có thể được trích xuất từ ​​các đối tượng hàm thông qua thuộc tính
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
783 của chúng. Xem thêm Mô -đun
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
784.

Truy cập

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
783 làm tăng một sự kiện kiểm toán
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
786 với các đối số
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
787 và
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
788.auditing event
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
786 with arguments
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
787 and
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
788.

Một đối tượng mã có thể được thực thi hoặc đánh giá bằng cách chuyển nó [thay vì chuỗi nguồn] cho các hàm tích hợp

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
789 hoặc
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
790.

Xem hệ thống phân cấp loại tiêu chuẩn để biết thêm thông tin.The standard type hierarchy for more information.

Mã đối tượng

Các đối tượng mã được sử dụng bởi việc triển khai để thể hiện mã Python thực thi của Pseudo được biên dịch bằng cách sử dụng như một cơ thể chức năng. Chúng khác với các đối tượng chức năng vì chúng không chứa một tham chiếu đến môi trường thực hiện toàn cầu của chúng. Các đối tượng mã được trả về bởi hàm

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
782 tích hợp và có thể được trích xuất từ ​​các đối tượng hàm thông qua thuộc tính
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
783 của chúng. Xem thêm Mô -đun
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
784.

Truy cập

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
783 làm tăng một sự kiện kiểm toán
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
786 với các đối số
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
787 và
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
788.

Một đối tượng mã có thể được thực thi hoặc đánh giá bằng cách chuyển nó [thay vì chuỗi nguồn] cho các hàm tích hợp
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
789 hoặc
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
790.

Nhập đối tượng Jo

Loại đối tượng đại diện cho các loại đối tượng khác nhau. Một loại đối tượng được truy cập bởi chức năng tích hợp

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
791. Không có hoạt động đặc biệt trên các loại. Mô-đun tiêu chuẩn
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
792 xác định tên cho tất cả các loại tích hợp tiêu chuẩn.

Các loại được viết như thế này:
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
793.

Đối tượng nullSlicings]. It supports no special operations. There is exactly one ellipsis object, named

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
797 [a built-in name].
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
798 produces the
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
797 singleton.

Đối tượng này được trả về bởi các chức năng don don rõ ràng trả lại một giá trị. Nó hỗ trợ không có hoạt động đặc biệt. Có chính xác một đối tượng null, được đặt tên là

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26 [một tên tích hợp].
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
795 sản xuất cùng một singleton.

Nó được viết là
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
26.

Đối tượng EllipsisComparisons for more information. There is exactly one

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
802 object.
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
803 produces the singleton instance.

Đối tượng này thường được sử dụng bằng cách cắt [xem lát cắt]. Nó hỗ trợ không có hoạt động đặc biệt. Có chính xác một đối tượng Ellipsis, được đặt tên là

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
797 [một tên tích hợp].
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
798 sản xuất singleton
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
797.

Nó được viết là
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
797 hoặc
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
801.

Đối tượng được thực hiệnTruth Value Testing above].

Chúng được viết tương ứng là

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
33 và
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
51.

Đối tượng bên trong

Xem hệ thống phân cấp loại tiêu chuẩn cho thông tin này. Nó mô tả các đối tượng khung ngăn xếp, đối tượng Traceback và các đối tượng lát cắt.The standard type hierarchy for this information. It describes stack frame objects, traceback objects, and slice objects.

Thuộc tính đặc biệt

Việc triển khai thêm một vài thuộc tính chỉ đọc đặc biệt vào một số loại đối tượng, trong đó chúng có liên quan. Một số trong số này không được báo cáo bởi chức năng tích hợp

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
810.

________ 1811 ________ 1812¶

Một từ điển hoặc đối tượng ánh xạ khác được sử dụng để lưu trữ các thuộc tính đối tượng [có thể ghi].

________ 1813 ________ 1814¶

Lớp mà một thể hiện lớp thuộc về.

________ 1815 ________ 1816¶

Tuple của các lớp cơ sở của một đối tượng lớp.

________ 1817 ________ 1818¶

Tên của lớp, chức năng, phương thức, mô tả hoặc thể hiện trình tạo.

________ 1817 ________ 1820¶

Tên đủ điều kiện của lớp, chức năng, phương thức, mô tả hoặc thể hiện máy phát.qualified name of the class, function, method, descriptor, or generator instance.

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

________ 1815 ____ ____ 18222¶

Thuộc tính này là một bộ phận của các lớp được xem xét khi tìm kiếm các lớp cơ sở trong quá trình giải quyết phương thức.

________ 1815 ________ 1824 [][]

Phương pháp này có thể được ghi đè bởi Metaclass để tùy chỉnh thứ tự độ phân giải phương thức cho các trường hợp của nó. Nó được gọi là khởi tạo lớp và kết quả của nó được lưu trữ trong

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
825.

________ 1815 ________ 1827 [][]

Mỗi lớp giữ một danh sách các tài liệu tham khảo yếu đến các lớp con ngay lập tức của nó. Phương pháp này trả về một danh sách tất cả các tài liệu tham khảo vẫn còn sống. Danh sách theo thứ tự định nghĩa. Thí dụ:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
22

Giới hạn độ dài chuyển đổi chuỗi số nguyên

CPYThon có giới hạn toàn cầu để chuyển đổi giữa

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
96 và
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
37 để giảm thiểu các cuộc tấn công dịch vụ từ chối. Giới hạn này chỉ áp dụng cho các cơ sở số thập phân hoặc không có năng lực khác. Phương pháp thập lục phân, bát phân và nhị phân là không giới hạn. Giới hạn có thể được cấu hình.

Loại

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
96 trong CPython là số chiều dài abitry được lưu trữ ở dạng nhị phân [thường được gọi là Bignum Hồi]. Không tồn tại thuật toán nào có thể chuyển đổi chuỗi thành số nguyên nhị phân hoặc số nguyên nhị phân thành một chuỗi trong thời gian tuyến tính, trừ khi cơ sở là sức mạnh của 2. Ngay cả các thuật toán nổi tiếng nhất cho cơ sở 10 cũng có độ phức tạp của phương pháp điều khiển phụ. Chuyển đổi một giá trị lớn như
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
831 có thể tiếp quản một giây trên CPU nhanh.

Giới hạn kích thước chuyển đổi cung cấp một cách thực tế để tránh CVE-2020-10735.

Giới hạn được áp dụng cho số lượng ký tự chữ số trong chuỗi đầu vào hoặc đầu ra khi thuật toán chuyển đổi phi tuyến tính sẽ được tham gia. Gạch dưới và dấu hiệu không được tính vào giới hạn.

Khi một hoạt động vượt quá giới hạn,

>>> n = 19
>>> bin[n]
'0b10011'
>>> n.bit_count[]
3
>>> [-n].bit_count[]
3
73 được nâng lên:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
23

Giới hạn mặc định là 4300 chữ số như được cung cấp trong

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
833. Giới hạn thấp nhất có thể được cấu hình là 640 chữ số như được cung cấp trong
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
834.

Verification:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
24

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

API bị ảnh hưởng

Giới hạn chỉ áp dụng cho các chuyển đổi có khả năng chậm giữa

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
96 và
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
37 hoặc
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
38:

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    838 với cơ sở mặc định 10.

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    839 cho tất cả các cơ sở không phải là sức mạnh của 2.

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    840.

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    841

  • Bất kỳ chuyển đổi chuỗi nào khác sang cơ sở 10, ví dụ

    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    842,
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    843 hoặc
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    844.

Các hạn chế không áp dụng cho các chức năng với thuật toán tuyến tính:

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    839 với cơ sở 2, 4, 8, 16 hoặc 32.

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    846 và
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    847.

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    848,
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    849,
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    850.

  • Đặc điểm kỹ thuật định dạng ngôn ngữ nhỏ cho số lục giác, bát phân và nhị phân. for hex, octal, and binary numbers.

  • >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    37 đến
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    97.

  • >>> [1024].to_bytes[2, byteorder='big']
    b'\x04\x00'
    >>> [1024].to_bytes[10, byteorder='big']
    b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
    >>> [-1024].to_bytes[10, byteorder='big', signed=True]
    b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
    >>> x = 1000
    >>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
    b'\xe8\x03'
    
    37 đến
    >>> n = 19
    >>> bin[n]
    '0b10011'
    >>> n.bit_count[]
    3
    >>> [-n].bit_count[]
    3
    
    04.

Định cấu hình giới hạn

Trước khi Python khởi động, bạn có thể sử dụng biến môi trường hoặc cờ dòng lệnh để định cấu hình giới hạn:

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    855, ví dụ:
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    856 để đặt giới hạn thành 640 hoặc
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    857 để vô hiệu hóa giới hạn.

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    858, ví dụ:
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    859

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    860 chứa giá trị của
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    855 hoặc
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    858. Nếu cả ENV VAR và tùy chọn
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    863 được đặt, tùy chọn
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    863 được ưu tiên. Giá trị -1 chỉ ra rằng cả hai đều không được đặt, do đó giá trị
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    833 đã được sử dụng trong quá trình khởi tạo.
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    855 or
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    858. If both the env var and the
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    863 option are set, the
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    863 option takes precedence. A value of -1 indicates that both were unset, thus a value of
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    833 was used during initilization.

Từ mã, bạn có thể kiểm tra giới hạn hiện tại và đặt cái mới bằng API

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
866 này:

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    867 và
    def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    868 là một getter và setter cho giới hạn toàn trình phiên dịch. Trình tự phụ có giới hạn riêng của họ.

Thông tin về mặc định và tối thiểu có thể được tìm thấy trong

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
869:

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    833 là giới hạn mặc định được biên dịch.

  • def bit_length[self]:
        s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
        s = s.lstrip['-0b'] # remove leading zeros and minus sign
        return len[s]       # len['100101'] --> 6
    
    834 là giá trị được chấp nhận thấp nhất cho giới hạn [trừ 0 vô hiệu hóa nó].

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

API bị ảnh hưởng

Giới hạn chỉ áp dụng cho các chuyển đổi có khả năng chậm giữa

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
96 và
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
37 hoặc
>>> [1024].to_bytes[2, byteorder='big']
b'\x04\x00'
>>> [1024].to_bytes[10, byteorder='big']
b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
>>> [-1024].to_bytes[10, byteorder='big', signed=True]
b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
>>> x = 1000
>>> x.to_bytes[[x.bit_length[] + 7] // 8, byteorder='little']
b'\xe8\x03'
38:

Kiểm tra ứng dụng của bạn một cách kỹ lưỡng nếu bạn sử dụng giới hạn thấp. Đảm bảo các thử nghiệm của bạn chạy với giới hạn được đặt sớm thông qua môi trường hoặc cờ để áp dụng trong khi khởi động và thậm chí trong bất kỳ bước cài đặt nào có thể gọi Python vào các nguồn

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
874 cho các tệp
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
872.

Cấu hình được đề xuất

Mặc định

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
833 dự kiến ​​sẽ hợp lý cho hầu hết các ứng dụng. Nếu ứng dụng của bạn yêu cầu giới hạn khác, hãy đặt nó từ điểm nhập chính của bạn bằng mã bất khả tri phiên bản Python vì các API này đã được thêm vào các bản phát hành bản vá bảo mật trong các phiên bản trước 3.11.

Example:

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
25

Nếu bạn cần vô hiệu hóa nó hoàn toàn, hãy đặt nó thành

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
37.

Chú thích

1

Thông tin bổ sung về các phương pháp đặc biệt này có thể được tìm thấy trong Hướng dẫn tham khảo Python [tùy chỉnh cơ bản].Basic customization].

2

Kết quả là, danh sách

def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
878 được coi là bằng
def bit_length[self]:
    s = bin[self]       # binary representation:  bin[-37] --> '-0b100101'
    s = s.lstrip['-0b'] # remove leading zeros and minus sign
    return len[s]       # len['100101'] --> 6
879 và tương tự cho các bộ đếm.

3

Họ phải có vì trình phân tích cú pháp có thể nói với loại toán hạng.

4[1,2,3,4][1,2,3,4]

Các ký tự vỏ là những nhân vật có thuộc tính danh mục chung là một trong những người LU [chữ cái, chữ hoa], LL LL [chữ cái, chữ thường], hoặc LT LT [chữ cái, Titlecase].

5[1,2][1,2]

Do đó, để định dạng một tuple, bạn nên cung cấp một bộ phận đơn lẻ mà phần tử duy nhất là tuple được định dạng.

Hai loại toán tử chuỗi là gì?

Có hai toán tử chuỗi. Đầu tiên là toán tử nối ['.'], Trả về sự kết hợp của các đối số bên phải và trái của nó. Thứ hai là toán tử gán kết hợp ['.concatenation operator ['. '], which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator [' .

Các loại hoạt động chuỗi là gì?

Các hoạt động chuỗi bao gồm nối, quét, phụ, dịch và xác minh.concatenation, scanning, substringing, translation, and verification.

Các chuỗi trong Python là gì?

Python có một lớp chuỗi tích hợp có tên "str" với nhiều tính năng tiện dụng [có một mô-đun cũ hơn có tên là "chuỗi" mà bạn không nên sử dụng].Chuỗi chữ có thể được bao quanh bởi các trích dẫn kép hoặc đơn, mặc dù các trích dẫn đơn được sử dụng phổ biến hơn.a built-in string class named "str" with many handy features [there is an older module named "string" which you should not use]. String literals can be enclosed by either double or single quotes, although single quotes are more commonly used.

Kiểu dữ liệu chuỗi trong Python là gì?

1] Chuỗi.Trong Python, các chuỗi là các mảng byte đại diện cho các ký tự Unicode.Một chuỗi là một tập hợp của một hoặc nhiều ký tự được đặt trong một trích dẫn, trích dẫn kép hoặc ba.Trong Python không có kiểu dữ liệu ký tự, một ký tự là một chuỗi độ dài.Nó được đại diện bởi lớp str.arrays of bytes representing Unicode characters. A string is a collection of one or more characters put in a single quote, double-quote or triple quote. In python there is no character data type, a character is a string of length one. It is represented by str class.

Bài Viết Liên Quan

Chủ Đề