Hướng dẫn built-in types in python - các kiểu tích hợp trong python

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.

Show

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

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
29 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
30 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
31.

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
32 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 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
34 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
35 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
36 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
    
    28 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
    
    35.

  • 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
    
    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,
    def bit_length(self):
        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

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

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
39 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
35 cho FALSE 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
52 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
53 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 55, def bit_length(self): s = bin(self) # binary representation: bin(-37) --> '-0b100101' s = s.lstrip('-0b') # remove leading zeros and minus sign return len(s) # len('100101') --> 6 54, ________ 158¶

Đâ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
59

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
60

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
61

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
53, 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
35

(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
    
    58 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
    
    65 đượ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
    
    66 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
    
    67 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
68 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
69, 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
70 đượ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
71

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
72

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
73

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
74

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
53, 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
35

def bit_length(self):
    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

Đâ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
76

Đâ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
77

def bit_length(self):
    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 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
65 đượ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
66 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
67 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
78

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
68 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
69, 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
70 đượ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
97 method.

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

lớn hơn hoặc bằngdouble in C; information about the precision and internal representation of floating point numbers for the machine on which your program is running is available in

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
01. Complex numbers have a real and imaginary part, which are each a floating point number. To extract these parts from a complex number z, use
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
02 and
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
03. (The standard library includes the additional numeric types
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
04, for rationals, and
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
05, for floating-point numbers with user-definable precision.)

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
06 hoặc
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
07 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 giáo đượ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
08,
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
09 và
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
10 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
11

tổng của x và y

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
12

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

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
13

sản phẩm 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

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
15

chỉ số của x và y

(1)

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
16

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

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
14

(2)

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
18

x phủ định

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
19

x Không thay đổi

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
20

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
21

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
22

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

(3)(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
24

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
09

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
26

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
10

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
28

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

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
29

Cặp

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
30

(2)

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
31

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
32

x đến sức mạnh y

(5)

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
33

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
34

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
    
    35 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
    
    39,
    >>> n = 19
    >>> bin(n)
    '0b10011'
    >>> n.bit_count()
    3
    >>> (-n).bit_count()
    3
    
    37 là
    >>> n = 19
    >>> bin(n)
    '0b10011'
    >>> n.bit_count()
    3
    >>> (-n).bit_count()
    3
    
    38,
    >>> n = 19
    >>> bin(n)
    '0b10011'
    >>> n.bit_count()
    3
    >>> (-n).bit_count()
    3
    
    39 là
    >>> n = 19
    >>> bin(n)
    '0b10011'
    >>> n.bit_count()
    3
    >>> (-n).bit_count()
    3
    
    38 và
    >>> n = 19
    >>> bin(n)
    '0b10011'
    >>> n.bit_count()
    3
    >>> (-n).bit_count()
    3
    
    41 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
    
    39.

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

    >>> n = 19
    >>> bin(n)
    '0b10011'
    >>> n.bit_count()
    3
    >>> (-n).bit_count()
    3
    
    21 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
    
    44 và
    >>> n = 19
    >>> bin(n)
    '0b10011'
    >>> n.bit_count()
    3
    >>> (-n).bit_count()
    3
    
    45 để 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
    
    46 và
    >>> n = 19
    >>> bin(n)
    '0b10011'
    >>> n.bit_count()
    3
    >>> (-n).bit_count()
    3
    
    47 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
    
    52, 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
    
    39 đến
    >>> n = 19
    >>> bin(n)
    '0b10011'
    >>> n.bit_count()
    3
    >>> (-n).bit_count()
    3
    
    50 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
    
    51).

    Xem https://www.unicode.org/public/14.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
    
    51.

Tất cả các loại

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
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
98 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
99) 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
56

Ghi chú

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
58

Tài liệu đầy đủ

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
59

tổng của x và y

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
61

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
14

x phủ định

Hoạt động

Kết quả

Ghi chú

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
68

Tài liệu đầy đủ

(4)

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
69

tổng của x và y

(4)

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
70

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

(4)

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
71

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

(1)(2)

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
72

chỉ số của x và y

(1)(3)

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
73

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
    
    14

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

int.bit_length () ¶bit_length()

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
79 là khác nhau, thì
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
80 là số nguyên dương duy nhất
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
81 sao cho
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
82. Tương đương, khi
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
20 đủ nhỏ để có logarit tròn chính xác, sau đó
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
84. Nếu
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
79 bằng không, thì
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
80 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
39.

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.

int.bit_count ()bit_count()

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.

int.bit_count ()to_bytes(length=1, byteorder='big', *, 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.

int.to_bytes (length = 1, byteorder = 'lớn', *, đã 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 độ dài và mặc định là 1. Một

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

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

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)

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

int.bit_count ()Added default argument values for

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
96 and
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
97.

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ụ:int.from_bytes(bytes, byteorder='big', *, signed=False)

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

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

int.to_bytes (length = 1, byteorder = 'lớn', *, đã ký = false) ¶bytes-like object or an iterable producing bytes.

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 độ dài và mặc định là 1. Một

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

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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n

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

int.bit_count ()Added default argument value for

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
97.

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ụ:as_integer_ratio()

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

int.to_bytes (length = 1, byteorder = 'lớn', *, đã 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 độ dài và mặc định là 1. Một

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
88 được nâng lên nếu số nguyên không thể đại diện với số byte đã cho.abstract base class. float also has the following additional methods.

Đối số byteorder xác định thứ tự byte được sử dụng để biểu diễn số nguyên và mặc định là
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
89. Nếu byteorder là
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
89, 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
91, byte quan trọng nhất là ở cuối mảng byte.
as_integer_ratio()

Đố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
35 và số nguyên âm được đưa ra, một
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
88 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
35.

Các giá trị mặc định có thể được sử dụng để thuận tiện biến một số nguyên thành một đối tượng byte duy nhất. Tuy nhiên, khi sử dụng các đối số mặc định, đừng cố gắng chuyển đổi giá trị lớn hơn 255 hoặc bạn sẽ nhận được
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
88:
is_integer()

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

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False

Đã thay đổi trong phiên bản 3.11: Đã thêm các giá trị đối số mặc định cho

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
96 và
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
97.

classMethodint.from_bytes (byte, byteorder = 'lớn', *, đã ký = false) ¶hex()

Trả về số nguyên được biểu thị bằng mảng byte đã cho.

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.float.fromhex(s)

Đối số byteorder xác định thứ tự byte được sử dụng để biểu diễn số nguyên và mặc định là

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
89. Nếu byteorder là
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
89, 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
91, 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
def bit_count(self):
    return bin(self).count("1")
01 làm giá trị đơn hàng byte.

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

Đã thay đổi trong phiên bản 3.11: Đã thêm giá trị đối số mặc định cho

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
97.

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

int.as_integer_ratio () ¶

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")
23 đại diện cho số điểm nổi
def bit_count(self):
    return bin(self).count("1")
24 hoặc
def bit_count(self):
    return bin(self).count("1")
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
0

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

def bit_count(self):
    return bin(self).count("1")
25 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ố:

def bit_length(self):
    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

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
79 và
def bit_count(self):
    return bin(self).count("1")
28, 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")
29 bất cứ khi nào
def bit_count(self):
    return bin(self).count("1")
30 (xem tài liệu phương thức
def bit_count(self):
    return bin(self).count("1")
31 để 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
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
99,
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
05 và
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
04) 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ả các trường hợp của ____.
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
04, 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
99 và
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
05. 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")
40 cho một nguyên tố chính cố định
def bit_count(self):
    return bin(self).count("1")
40. Giá trị của
def bit_count(self):
    return bin(self).count("1")
40 được cung cấp cho Python dưới dạng thuộc tính
def bit_count(self):
    return bin(self).count("1")
43 của
def bit_count(self):
    return bin(self).count("1")
44.

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")
45 trên các máy có độ dài C 32 bit và
def bit_count(self):
    return bin(self).count("1")
46 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")
45 on machines with 32-bit C longs and
def bit_count(self):
    return bin(self).count("1")
46 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")
    
    47 là một số hợp lý không âm và
    def bit_count(self):
        return bin(self).count("1")
    
    48 không chia hết cho
    def bit_count(self):
        return bin(self).count("1")
    
    40, hãy xác định
    def bit_count(self):
        return bin(self).count("1")
    
    50 là
    def bit_count(self):
        return bin(self).count("1")
    
    51, trong đó
    def bit_count(self):
        return bin(self).count("1")
    
    52 cung cấp nghịch đảo của
    def bit_count(self):
        return bin(self).count("1")
    
    48 modulo
    def bit_count(self):
        return bin(self).count("1")
    
    40.

  • Nếu

    def bit_count(self):
        return bin(self).count("1")
    
    47 là một số hợp lý không âm và
    def bit_count(self):
        return bin(self).count("1")
    
    48 là chia hết cho
    def bit_count(self):
        return bin(self).count("1")
    
    40 (nhưng
    def bit_count(self):
        return bin(self).count("1")
    
    58 thì không) thì
    def bit_count(self):
        return bin(self).count("1")
    
    48 không có modulo nghịch đảo
    def bit_count(self):
        return bin(self).count("1")
    
    40 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")
    
    50 là giá trị không đổi
    def bit_count(self):
        return bin(self).count("1")
    
    62.

  • Nếu

    def bit_count(self):
        return bin(self).count("1")
    
    47 là số hợp lý âm định nghĩa
    def bit_count(self):
        return bin(self).count("1")
    
    50 là
    def bit_count(self):
        return bin(self).count("1")
    
    65. Nếu băm kết quả là
    >>> n = 19
    >>> bin(n)
    '0b10011'
    >>> n.bit_count()
    3
    >>> (-n).bit_count()
    3
    
    38, hãy thay thế nó bằng
    def bit_count(self):
        return bin(self).count("1")
    
    67.

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

    def bit_count(self):
        return bin(self).count("1")
    
    62 và
    def bit_count(self):
        return bin(self).count("1")
    
    69 đượ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ố

    >>> n = 19
    >>> bin(n)
    '0b10011'
    >>> n.bit_count()
    3
    >>> (-n).bit_count()
    3
    
    00
    def bit_count(self):
        return bin(self).count("1")
    
    71, 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")
    
    72, giảm modulo
    def bit_count(self):
        return bin(self).count("1")
    
    73 để nó nằm trong
    def bit_count(self):
        return bin(self).count("1")
    
    74. 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
    
    38, nó đã thay thế bằng
    def bit_count(self):
        return bin(self).count("1")
    
    67.

Để 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
99 hoặc
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
00:

def bit_length(self):
    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

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:

container .__ iter __ () ¶__iter__()

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")
79 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:

iterator .__ iter __ () ¶__iter__()

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

def bit_count(self):
    return bin(self).count("1")
80 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
95. Phương pháp này tương ứng với khe
def bit_count(self):
    return bin(self).count("1")
79 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")
80 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
95 statements. This method corresponds to the
def bit_count(self):
    return bin(self).count("1")
79 slot of the type structure for Python objects in the Python/C API.

iterator .__ Tiếp theo __ () ¶__next__()

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ệ

def bit_count(self):
    return bin(self).count("1")
83. Phương pháp này tương ứng với khe
def bit_count(self):
    return bin(self).count("1")
84 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
def bit_count(self):
    return bin(self).count("1")
83 exception. This method corresponds to the
def bit_count(self):
    return bin(self).count("1")
84 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 lặp ____ ____385 tăng

def bit_count(self):
    return bin(self).count("1")
83, 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 ____ ____387 đượ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

def bit_count(self):
    return bin(self).count("1")
87 và
def bit_count(self):
    return bin(self).count("1")
85. 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
def bit_count(self):
    return bin(self).count("1")
87 method is implemented as a generator, it will automatically return an iterator object (technically, a generator object) supplying the
def bit_count(self):
    return bin(self).count("1")
87 and
def bit_count(self):
    return bin(self).count("1")
85 methods. More information about generators can be found in the documentation for the yield expression.

Các loại trình tự - def bit_count(self): return bin(self).count("1") 90, def bit_count(self): return bin(self).count("1") 91, ________ 392¶

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.

def bit_count(self):
    return bin(self).count("1")
93 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.

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
95 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
96 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
66 (nối) và
def bit_count(self):
    return bin(self).count("1")
97 (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ú

def bit_count(self):
    return bin(self).count("1")
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
53 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
35

(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'
01

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

(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'
04

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'
05 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'
06

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

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

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

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

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

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

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

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

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'
15 hoặc
def bit_count(self):
    return bin(self).count("1")
83 (hoặc khi chỉ số 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
    
    95 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
    
    96 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'
    
    19,
    >>> (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'
    
    20 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'
    
    21) 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
    
    39 đượ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
    
    39 (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
    
    3

    Đ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'
    
    24 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'
    
    25 đề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'
    
    26 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
    
    4

    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'
    
    27 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'
    
    28 đượ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'
    
    29 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
    
    39.

  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'
    
    31. 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'
    
    10, 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'
    
    10. 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
    
    28, 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
    
    39. 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
    
    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'
    
    10. 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'
    
    38 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'
    
    39. 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'
    
    40,
    >>> (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'
    
    41,
    >>> (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,
    >>> (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, 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'
    
    10 nếu chúng lớn hơn. Khi K âm, I và J 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'
    
    45 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
    
    28, 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
    
    28, 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
    
    52.

  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'
      
      19, bạn có thể xây dựng 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'
      
      50 ở 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'
      
      51 và truy xuất giá trị của nó khi hoàn thành

    • 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'
      
      20, 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'
      
      53 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'
      
      54 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'
      
      21. 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'
      
      21 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

      def bit_count(self):
          return bin(self).count("1")
      
      91, hãy mở rộng
      def bit_count(self):
          return bin(self).count("1")
      
      90 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ư

    def bit_count(self):
        return bin(self).count("1")
    
    92) 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'
    
    60 tăng
    >>> n = 19
    >>> bin(n)
    '0b10011'
    >>> n.bit_count()
    3
    >>> (-n).bit_count()
    3
    
    74 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'
    
    62, 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 chuỗi bất biến thường thực hiện cũng 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'
63 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

def bit_count(self):
    return bin(self).count("1")
91, đượ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'
65 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'
66 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'
67.

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

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'
69 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'
21 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'
71).

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

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

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

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

>>> (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'
76

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'
09 được thay thế bởi 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'
78

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'
09 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'
80

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

>>> (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'
82

Xóa tất cả các mục khỏi S (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'
83)

(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'
84

Tạo một bản sao nông của S (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'
85)

(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'
86 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'
87

Mở rộng s với nội dung của t (phần lớn 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'
88)

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

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

(6)

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

Chèn X vào S tại chỉ mục được đưa ra bởi 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'
91)

>>> (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 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'
93

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

(2)

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

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'
07 bằng x

(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'
96

đả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
    
    38, do đó theo mặc định, mục cuối cùng được xóa và trả về.

  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'
    
    98 tăng
    >>> n = 19
    >>> bin(n)
    '0b10011'
    >>> n.bit_count()
    3
    >>> (-n).bit_count()
    3
    
    74 khi X không được tìm thấy trong s.

  4. Phương pháp

    def to_bytes(n, length=1, byteorder='big', signed=False):
        if byteorder == 'little':
            order = range(length)
        elif byteorder == 'big':
            order = reversed(range(length))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        return bytes((n >> i*8) & 0xff for i in order)
    
    00 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. def to_bytes(n, length=1, byteorder='big', signed=False):
        if byteorder == 'little':
            order = range(length)
        elif byteorder == 'big':
            order = reversed(range(length))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        return bytes((n >> i*8) & 0xff for i in order)
    
    01 và
    def to_bytes(n, length=1, byteorder='big', signed=False):
        if byteorder == 'little':
            order = range(length)
        elif byteorder == 'big':
            order = reversed(range(length))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        return bytes((n >> i*8) & 0xff for i in order)
    
    02 đượ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'
    
    65 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'
    
    66).
    def to_bytes(n, length=1, byteorder='big', signed=False):
        if byteorder == 'little':
            order = range(length)
        elif byteorder == 'big':
            order = reversed(range(length))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        return bytes((n >> i*8) & 0xff for i in order)
    
    02 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'
    
    69 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:

    def to_bytes(n, length=1, byteorder='big', signed=False):
        if byteorder == 'little':
            order = range(length)
        elif byteorder == 'big':
            order = reversed(range(length))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        return bytes((n >> i*8) & 0xff for i in order)
    
    01 và
    def to_bytes(n, length=1, byteorder='big', signed=False):
        if byteorder == 'little':
            order = range(length)
        elif byteorder == 'big':
            order = reversed(range(length))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        return bytes((n >> i*8) & 0xff for i in order)
    
    02.
    def to_bytes(n, length=1, byteorder='big', signed=False):
        if byteorder == 'little':
            order = range(length)
        elif byteorder == 'big':
            order = reversed(range(length))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        return bytes((n >> i*8) & 0xff for i in order)
    
    01 and
    def to_bytes(n, length=1, byteorder='big', signed=False):
        if byteorder == 'little':
            order = range(length)
        elif byteorder == 'big':
            order = reversed(range(length))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        return bytes((n >> i*8) & 0xff for i in order)
    
    02 methods.

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

    def to_bytes(n, length=1, byteorder='big', signed=False):
        if byteorder == 'little':
            order = range(length)
        elif byteorder == 'big':
            order = reversed(range(length))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        return bytes((n >> i*8) & 0xff for i in order)
    
    09. 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'
    
    05 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 học ([itable]) ¶ list([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
    
    46

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

    def to_bytes(n, length=1, byteorder='big', signed=False):
        if byteorder == 'little':
            order = range(length)
        elif byteorder == 'big':
            order = reversed(range(length))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        return bytes((n >> i*8) & 0xff for i in order)
    
    12,
    def to_bytes(n, length=1, byteorder='big', signed=False):
        if byteorder == 'little':
            order = range(length)
        elif byteorder == 'big':
            order = reversed(range(length))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        return bytes((n >> i*8) & 0xff for i in order)
    
    13

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

    def to_bytes(n, length=1, byteorder='big', signed=False):
        if byteorder == 'little':
            order = range(length)
        elif byteorder == 'big':
            order = reversed(range(length))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        return bytes((n >> i*8) & 0xff for i in order)
    
    14

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

    def to_bytes(n, length=1, byteorder='big', signed=False):
        if byteorder == 'little':
            order = range(length)
        elif byteorder == 'big':
            order = reversed(range(length))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        return bytes((n >> i*8) & 0xff for i in order)
    
    15 hoặc
    def to_bytes(n, length=1, byteorder='big', signed=False):
        if byteorder == 'little':
            order = range(length)
        elif byteorder == 'big':
            order = reversed(range(length))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        return bytes((n >> i*8) & 0xff for i in order)
    
    16

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ư

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
17. Ví dụ:
def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
18 trả về
def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
19 và
def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
20 trả về
def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
21. 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
46.

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

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
23 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:

Sắp xếp (*, key = none, đảo ngược = 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
71 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).

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
25 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ụ:

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
26). 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
28 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

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
28 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
53, 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

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
23 để 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

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
25 đượ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
74 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
74 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

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
33 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'
66 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'
65).

classtupl tuple([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ị 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
    
    45

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

    def to_bytes(n, length=1, byteorder='big', signed=False):
        if byteorder == 'little':
            order = range(length)
        elif byteorder == 'big':
            order = reversed(range(length))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        return bytes((n >> i*8) & 0xff for i in order)
    
    37 hoặc
    def to_bytes(n, length=1, byteorder='big', signed=False):
        if byteorder == 'little':
            order = range(length)
        elif byteorder == 'big':
            order = reversed(range(length))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        return bytes((n >> i*8) & 0xff for i in order)
    
    38

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

    def to_bytes(n, length=1, byteorder='big', signed=False):
        if byteorder == 'little':
            order = range(length)
        elif byteorder == 'big':
            order = reversed(range(length))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        return bytes((n >> i*8) & 0xff for i in order)
    
    39 hoặc
    def to_bytes(n, length=1, byteorder='big', signed=False):
        if byteorder == 'little':
            order = range(length)
        elif byteorder == 'big':
            order = reversed(range(length))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        return bytes((n >> i*8) & 0xff for i in order)
    
    40

  • Sử dụng

    def to_bytes(n, length=1, byteorder='big', signed=False):
        if byteorder == 'little':
            order = range(length)
        elif byteorder == 'big':
            order = reversed(range(length))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        return bytes((n >> i*8) & 0xff for i in order)
    
    41 tích hợp:
    def to_bytes(n, length=1, byteorder='big', signed=False):
        if byteorder == 'little':
            order = range(length)
        elif byteorder == 'big':
            order = reversed(range(length))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        return bytes((n >> i*8) & 0xff for i in order)
    
    41 hoặc
    def to_bytes(n, length=1, byteorder='big', signed=False):
        if byteorder == 'little':
            order = range(length)
        elif byteorder == 'big':
            order = reversed(range(length))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        return bytes((n >> i*8) & 0xff for i in order)
    
    43

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

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
44 trả về
def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
45 và
def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
46 trả về
def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
47. 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
45.

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

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
49 là một cuộc gọi chức năng với ba đối số, trong khi
def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
50 là một cuộc gọi chức năng với 3-tuple làm đố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,

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
51 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

def bit_count(self):
    return bin(self).count("1")
92 đạ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")
80.

ClassRange (Dừng) ¶ ClassRange (Bắt đầu, Dừng [, Bước])range(stop)class range(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
98 hoặc bất kỳ đối tượng nào thực hiện phương thức đặc biệt
def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
09). 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
52. 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
39. Nếu bước bằng không,
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
74 được nâng lên.

Đối với một bước dương, nội dung của một phạm vi

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
59 được xác định bởi công thức
def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
60 trong đó
def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
61 và
def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
62.

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

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
60, nhưng các ràng buộc là
def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
61 và
def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
65.

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

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
66 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

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
67 được cho phép nhưng một số tính năng (như
def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
68) có thể tăng
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
88.

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
5

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

bắt đầu¶

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
39 nếu tham số không được cung cấp)

dừng lại¶

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

bươc¶

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
52 nếu tham số không được cung cấp)

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

def bit_count(self):
    return bin(self).count("1")
92 so với
def bit_count(self):
    return bin(self).count("1")
90 hoặc
def bit_count(self):
    return bin(self).count("1")
91 thông thường là một đối tượng
def bit_count(self):
    return bin(self).count("1")
92 sẽ luôn có cùng lượng bộ nhớ (nhỏ), bất kể kích thước của phạm vi nó đại diện cho , 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

def bit_count(self):
    return bin(self).count("1")
93 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
6

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
75 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
76 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
98 Đố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
98 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

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
76,
def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
77 và
def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
78.The
def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
76,
def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
77 and
def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
78 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 - ________ 419¶

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'
19 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:

    def to_bytes(n, length=1, byteorder='big', signed=False):
        if byteorder == 'little':
            order = range(length)
        elif byteorder == 'big':
            order = reversed(range(length))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        return bytes((n >> i*8) & 0xff for i in order)
    
    93

  • Báo giá kép:

    def to_bytes(n, length=1, byteorder='big', signed=False):
        if byteorder == 'little':
            order = range(length)
        elif byteorder == 'big':
            order = reversed(range(length))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        return bytes((n >> i*8) & 0xff for i in order)
    
    94

  • Triple Trích dẫn:

    def to_bytes(n, length=1, byteorder='big', signed=False):
        if byteorder == 'little':
            order = range(length)
        elif byteorder == 'big':
            order = reversed(range(length))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        return bytes((n >> i*8) & 0xff for i in order)
    
    95,
    def to_bytes(n, length=1, byteorder='big', signed=False):
        if byteorder == 'little':
            order = range(length)
        elif byteorder == 'big':
            order = reversed(range(length))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        return bytes((n >> i*8) & 0xff for i in order)
    
    96

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

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
97.

Xem Chuỗi và Byte theo nghĩa đen để biết thêm về các hình thức 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ố

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
59 (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
def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
59 (“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'
19.

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 một chuỗi không trố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
00.

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'
50 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'
51 có thể được sử dụng để xây dựng các chuỗi một cách 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ố

>>> 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 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ố
def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
59.For backwards compatibility with the Python 2 series, 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
03 prefix is once again permitted on string literals. It has no effect on the meaning of string literals and cannot be combined with the
def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
59 prefix.

classstr (object = '') ¶ classStr (object = b '', expoding = 'utf-8', error = 'nghiêm ngặt')str(object='')class str(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
30 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
30 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,

>>> 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 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
07, đó 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
>>> 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, 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
30 sẽ quay trở lại trở lạ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
10.

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'
20 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'
21). 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'
20 (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'
21), thì
>>> 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
15 tương đương vớ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
16. 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
>>> 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
17. 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'
20 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'
21). 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'
20 (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'
21) object, then
>>> 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
15 is equivalent to
>>> 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. Otherwise, the bytes object underlying the buffer object is obtained before calling
>>> 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
17. 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'
20 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
30 mà không có cá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
>>> 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 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
7

Để 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'
19 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

>>> 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
22, 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
>>> 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
23. Để 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
>>> 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
23 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

>>> 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
24).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
>>> 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
24 module).

str.capitalize ()capitalize()

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.

str.case Fold () ¶casefold()

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

>>> 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
25 tương đương vớ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
26. Vì nó đã là chữ thườ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
27 sẽ không làm gì vớ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
25;
>>> 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
29 chuyển đổi nó thà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
26.

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.

str.center (chiều rộng [, fillchar]) ¶center(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'
10.

str.count (sub [, start [, end]]) ¶count(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.

Nếu phụ trống, hãy trả về số chuỗi trống giữa các ký tự là độ dài của chuỗi cộng với một chuỗi.

str.encode (mã hóa = 'utf-8', lỗi = 'nghiêm ngặt') ¶encode(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à

>>> 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. Lỗi có thể được đưa ra để đặt sơ đồ xử lý lỗi khác. Mặc định cho các lỗi 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
33, có nghĩa là các lỗi mã hóa làm tă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
34. Các giá trị có thể khá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
35,
>>> 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,
>>> 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,
>>> 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à bất kỳ tên nào khác được đăng ký qua
>>> 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, 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.

str.endswith (hậu tố [, bắt đầu [, kết thúc]]) ¶endswith(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
53 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
35. 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í đó.

str.expandtabs (tabsize = 8) ¶expandtabs(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 (

>>> 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), 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
8

str.find (sub [, start [, end]]) ¶find(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

>>> 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. Đố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
38 nếu không tìm thấy phụ.

Ghi chú

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
47 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
95:

def bit_length(self):
    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

str.format (*args, ** kwargs) ¶format(*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
47. 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.

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
0

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
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
99,
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
00,
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
05 và các lớp con) với loại
def bit_count(self):
    return bin(self).count("1")
48 (Xh:
>>> 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
55) hoặc dài hơn 1 byte và địa phươ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
57 khác với địa phươ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
56. 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")
48, hàm đặt tạm thời locale
>>> 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
56 thành locle
>>> 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
57 trong một số trường hợp.When formatting a number with the
def bit_count(self):
    return bin(self).count("1")
48 type, the function sets temporarily 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
56 locale to 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
57 locale in some cases.

str.format_map (ánh xạ) ¶format_map(mapping)

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
66, ngoại trừ
>>> 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 đượ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'
65. Điều này rất hữu ích nếu 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
67 là một lớp con Dict:

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
1

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

str.index (sub [, start [, end]]) ¶index(sub[, start[, end]])

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
47, nhưng tăng
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
74 khi không tìm thấy chuỗi con.

str.isalnum ()isalnum()

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
53 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
35 nếu không. Một ký 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
74 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
53:
>>> 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
76,
>>> 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
77,
>>> 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
78 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
79.

str.isalpha () ¶isalpha()

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

str.isascii ()isascii()

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
53 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
35 khác. 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.

str.isdecimal ()isdecimal()

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

str.isdigit ()isdigit()

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
53 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
35 khác. 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.

str.isidentifier ()isidentifier()

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

>>> 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 để kiểm tra xem chuỗ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
90 có phải là mã định danh dành riêng, chẳng hạn 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 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
92.

Example:

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
2

str.islower () ¶islower()

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
53 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
35 nếu không.

str.isnumeric ()isnumeric()

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

str.isprintable () ¶isprintable()

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
53 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
35 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. (Lưu ý rằng các ký tự có thể in trong bối cảnh này là các ký tự không nên thoát ra 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
29 được gọi trên một chuỗi.

str.isspace () ¶isspace()

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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
04), loại chung của nó là
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
05 (Dấu tách, không gian) hoặc lớp hai chiều của nó là một trong
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
06,
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
07 hoặc
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
08.

str.istitle () ¶istitle()

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
53 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
35 nếu không.

str.isupper () ¶isupper()

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
53 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
35 nếu không.

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
3

str.join (có thể lặp lại) ¶join(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
85 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'
20. Phân tách giữa các phần tử là chuỗi cung cấp phương pháp này.

str.ljust (chiều rộng [, fillchar]) ¶ljust(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'
10.

str.lower ()lower()

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.

str.lstrip ([chars]) ¶lstrip([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
28, đố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
4

Xem

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
17 để 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
5

Staticstr.Maketrans (x [, y [, z]]) ¶str.maketrans(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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
18.

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) đến 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
28. 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
28 trong kết quả.

str.partition (sep) ¶partition(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.

str.removeprefix (tiền tố, /) ¶removeprefix(prefix, /)

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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
21. 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
6

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

str.removesuffix (hậu tố, /) ¶removesuffix(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ề

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
22. 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
7

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

str.removesuffix (hậu tố, /) ¶replace(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ề

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
22. Nếu không, hãy trả về một bản sao của chuỗi gốc:

str.replace (cũ, mới [, đếm]) ¶rfind(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ế.

str.rfind (sub [, start [, end]]) ¶rindex(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

>>> 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. Đố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
38 khi thất bại.

str.rindex (sub [, start [, end]]) ¶rjust(width[, fillchar])

Giống như

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
25 nhưng tăng
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
74 khi không tìm thấy phụ con.

str.rjust (chiều rộng [, fillchar]) ¶rpartition(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'
10.

str.rpartition (sep) ¶rsplit(sep=None, maxsplit=- 1)

Chia chuỗi ở lần xuất hiện cuối cùng 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 hai chuỗi trống, theo sau là chuỗi.

str.rstrip ([chars]) ¶rstrip([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
28, đố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
8

Xem

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
32 để 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
9

str.split (sep = none, maxsplit = -1) ¶split(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ố

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
33). Nếu MAXSplit không được chỉ định hoặc
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
38, 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ụ:

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
35 trả về
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
36). Đối số SEP có thể bao gồm nhiều ký tự (ví dụ:
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
37 trả về
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
38). Tách một chuỗi trống với một dấu phân cách được chỉ định trả về
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
39.

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
28, 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
28 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
46.

Ví dụ:

def bit_count(self):
    return bin(self).count("1")
1

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
28, 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
28 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
46.
splitlines(keepends=False)

Str.SplitLines (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

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

Sự mô 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
44

Line Feed

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
45

Vận chuyển trở lại

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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
46 hoặc
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
47

Tabline

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
48 hoặc
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
49

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
50

Thức ăn dạng

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
51

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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
52

Phân tách nhóm

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
53

Ghi điểm phân tách

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
54

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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
55

Dòng phân tách

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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
46 and
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
48 added to list of line boundaries.

Ví dụ:

def bit_count(self):
    return bin(self).count("1")
2

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
28, 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
28 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
46.

def bit_count(self):
    return bin(self).count("1")
3

Str.SplitLines (Keepends = false) ¶

def bit_count(self):
    return bin(self).count("1")
4

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.startswith(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ệnstrip([chars])

Sự mô tả

def bit_count(self):
    return bin(self).count("1")
5

Line Feed

Vận chuyển trở lại

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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
46 hoặc
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
47

Tablinetitle()

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
48 hoặc
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
49

Ví dụ:

def bit_count(self):
    return bin(self).count("1")
7

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
28, 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
28 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
46.

def bit_count(self):
    return bin(self).count("1")
8

Str.SplitLines (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.

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ệntranslate(table)

Sự mô tả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
28, to delete the character from the return string; or raise a
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
67 exception, to map the character to itself.

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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
68 để 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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
69 để biết cách tiếp cận linh hoạt hơn đối với ánh xạ ký tự tùy chỉnh.

str.upper () ¶upper()

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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
70 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
35 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
90 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.

str.zfill (chiều rộng) ¶zfill(width)

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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
73 chữ số để tạo một chuỗi chiều rộng. Một tiền tố dấu hiệu hàng đầu (________ 774/________ 775) đượ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'
10.

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

________ 623 Đị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

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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
79 (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
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
80 (trong đó định dạng là một chuỗi),
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
79 Thông số kỹ thuật chuyển đổi 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
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
82 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ự

    def from_bytes(bytes, byteorder='big', signed=False):
        if byteorder == 'little':
            little_ordered = list(bytes)
        elif byteorder == 'big':
            little_ordered = list(reversed(bytes))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        n = sum(b << i*8 for i, b in enumerate(little_ordered))
        if signed and little_ordered and (little_ordered[-1] & 0x80):
            n -= 1 << 8*len(little_ordered)
    
        return n
    
    83, đá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ụ:

    def from_bytes(bytes, byteorder='big', signed=False):
        if byteorder == 'little':
            little_ordered = list(bytes)
        elif byteorder == 'big':
            little_ordered = list(reversed(bytes))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        n = sum(b << i*8 for i, b in enumerate(little_ordered))
        if signed and little_ordered and (little_ordered[-1] & 0x80):
            n -= 1 << 8*len(little_ordered)
    
        return n
    
    84).

  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à

    def from_bytes(bytes, byteorder='big', signed=False):
        if byteorder == 'little':
            little_ordered = list(bytes)
        elif byteorder == 'big':
            little_ordered = list(reversed(bytes))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        n = sum(b << i*8 for i, b in enumerate(little_ordered))
        if signed and little_ordered and (little_ordered[-1] & 0x80):
            n -= 1 << 8*len(little_ordered)
    
        return n
    
    85 (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

    def from_bytes(bytes, byteorder='big', signed=False):
        if byteorder == 'little':
            little_ordered = list(bytes)
        elif byteorder == 'big':
            little_ordered = list(reversed(bytes))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        n = sum(b << i*8 for i, b in enumerate(little_ordered))
        if signed and little_ordered and (little_ordered[-1] & 0x80):
            n -= 1 << 8*len(little_ordered)
    
        return n
    
    86 (DOT) theo sau là độ chính xác. Nếu được chỉ định là
    def from_bytes(bytes, byteorder='big', signed=False):
        if byteorder == 'little':
            little_ordered = list(bytes)
        elif byteorder == 'big':
            little_ordered = list(reversed(bytes))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        n = sum(b << i*8 for i, b in enumerate(little_ordered))
        if signed and little_ordered and (little_ordered[-1] & 0x80):
            n -= 1 << 8*len(little_ordered)
    
        return n
    
    85 (dấu hoa thị), độ chính xác thực tế được đọc từ phần tử tiếp theo của bộ tple 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ự

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
83. Khóa ánh xạ chọn giá trị được định dạng từ ánh xạ. 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'
1

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

def bit_count(self):
    return bin(self).count("1")
97 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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
90

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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
73

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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
75

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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
73 nếu cả hai được đưa ra).

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
94

.

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
74

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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
74 hoặc
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
75) 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 (

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
98,
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
99 hoặc
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
00) có thể có mặt, nhưng bị bỏ qua vì nó không cần thiết cho Python - vì vậy ví dụ:
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
01 giống hệt với
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
02.

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

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
03

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

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
04

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

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
05

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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
73 nếu cả hai được đưa ra).

(1)

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
06

.

(6)

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
08

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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
74 hoặc
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
75) sẽ đi trước chuyển đổi (ghi đè một lá cờ không gian trên mạng).

(2)

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
09

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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
98,
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
99 hoặc
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
00) có thể có mặt, nhưng bị bỏ qua vì nó không cần thiết cho Python - vì vậy ví dụ:
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
01 giống hệt với
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
02.

(2)

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
10

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

(3)

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
11

Chuyển đổi

(3)

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
12

Ghi chú

(3)

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
13

Ghi chú

(3)

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
14

Đị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)

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
15

Đị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)

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
16

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

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
17

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

(5)

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
19

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

(5)

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
21

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

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
22).

(5)

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
83

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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
83 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 (

    >>> (-2.0).is_integer()
    True
    >>> (3.2).is_integer()
    False
    
    25) được chèn trước chữ số đầu tiên.

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

    >>> (-2.0).is_integer()
    True
    >>> (3.2).is_integer()
    False
    
    26 hoặc
    >>> (-2.0).is_integer()
    True
    >>> (3.2).is_integer()
    False
    
    27 hàng đầu (tùy thuộc vào định dạng
    >>> (-2.0).is_integer()
    True
    >>> (3.2).is_integer()
    False
    
    08 hay
    >>> (-2.0).is_integer()
    True
    >>> (3.2).is_integer()
    False
    
    09 đã đượ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à

    >>> (-2.0).is_integer()
    True
    >>> (3.2).is_integer()
    False
    
    30, đầu ra bị cắt giảm thành
    >>> (-2.0).is_integer()
    True
    >>> (3.2).is_integer()
    False
    
    30 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

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
32 không cho rằng
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
33 là kết thúc của chuỗi.

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

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
34 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
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
35.
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
34 conversions for numbers whose absolute value is over 1e50 are no longer replaced by
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
35 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' 20, >>> (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' 21, ________ 838¶

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'
20 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'
21. Chúng được hỗ trợ bởi
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
38 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

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
42 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.

classBytes ([Nguồn [, mã hóa [, lỗi]]])bytes([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ố

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
43 được thêm vào:

  • Trích dẫn đơn:

    >>> (-2.0).is_integer()
    True
    >>> (3.2).is_integer()
    False
    
    44

  • Báo giá kép:

    >>> (-2.0).is_integer()
    True
    >>> (3.2).is_integer()
    False
    
    45

  • Triple Trích dẫn:

    >>> (-2.0).is_integer()
    True
    >>> (3.2).is_integer()
    False
    
    46,
    >>> (-2.0).is_integer()
    True
    >>> (3.2).is_integer()
    False
    
    47

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, các chữ cái byte cũng có thể sử dụng tiền tố

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
59 để vô hiệu hóa việc 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

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
49 (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
74). Đ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:

    >>> (-2.0).is_integer()
    True
    >>> (3.2).is_integer()
    False
    
    51

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

    >>> (-2.0).is_integer()
    True
    >>> (3.2).is_integer()
    False
    
    52

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

    >>> (-2.0).is_integer()
    True
    >>> (3.2).is_integer()
    False
    
    53

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 đó:

classMethodFromHex (chuỗi) ¶fromhex(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'
20 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'
2

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

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
55 hiện bỏ qua tất cả khoảng trắng ASCII trong chuỗi, không chỉ không gian.
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
55 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ó.

hex ([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'
3

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

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

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

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
56 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.
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
56 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,

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
57 sẽ là một số nguyên, trong khi
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
58 sẽ là một đố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 (

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
59) vì nó thường hữu ích hơn ví dụ:
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
60. 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 sử dụng
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
61.

Đố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'
21 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'
20.

classByteArray ([Nguồn [, mã hóa [, lỗi]]])bytearray([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 trường hợp trống:

    >>> (-2.0).is_integer()
    True
    >>> (3.2).is_integer()
    False
    
    64

  • Tạo một thể hiện không đầy đủ với độ dài nhất định:

    >>> (-2.0).is_integer()
    True
    >>> (3.2).is_integer()
    False
    
    65

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

    >>> (-2.0).is_integer()
    True
    >>> (3.2).is_integer()
    False
    
    66

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

    >>> (-2.0).is_integer()
    True
    >>> (3.2).is_integer()
    False
    
    67

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 đó:

classMethodFromHex (chuỗi) ¶fromhex(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'
21 này trả về đối tượng bytearray, 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'
5

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

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
69 Bây giờ bỏ qua tất cả khoảng trắng ASCII trong chuỗi, không chỉ không gian.
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
69 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 bytearray thành đại diện thập lục phân của nó.

hex ([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'
6

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

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

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
56 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
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
56,
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
71 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,

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
57 sẽ là một số nguyên, trong khi
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
58 sẽ là một đố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 (

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
59) vì nó thường hữu ích hơn ví dụ:
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
60. 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 sử dụng
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
61.

Đố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'
21 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'
20.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.

classByteArray ([Nguồn [, mã hóa [, lỗi]]])

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:

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

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

Tạo một trường hợp trống:

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
64

classByteArray ([Nguồn [, mã hóa [, lỗi]]])

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 trường hợp trống:

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
64

Tạo một thể hiện không đầy đủ với độ dài nhất định:
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
65
count(sub[, start[, end]])bytearray.count(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.

Nếu phụ trống, hãy trả về số lượng lát trống giữa các ký tự là độ dài của đối tượng byte cộng với một.

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

byte.removeprefix (tiền tố, /) ¶ bytearray.removeprefix (tiền tố, /) ¶removeprefix(prefix, /)bytearray.removeprefix(prefix, /)

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

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
77. 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'
9

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.

byte.removesuffix (hậu tố, /) ¶ bytearray.removesuffix (hậu tố, /) ¶removesuffix(suffix, /)bytearray.removesuffix(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ề

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
78. Nếu không, hãy trả lại một bản sao của dữ liệu nhị phân gốc:

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
0

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.

byte.removesuffix (hậu tố, /) ¶ bytearray.removesuffix (hậu tố, /) ¶decode(encoding='utf-8', errors='strict')bytearray.decode(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ề

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
78. 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ú

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.bytes-like object directly, without needing to make a temporary bytes or bytearray object.

Mới trong phiên bản 3.9.Added support for keyword arguments.

byte.removesuffix (hậu tố, /) ¶ bytearray.removesuffix (hậu tố, /) ¶The errors is now checked in development mode and in debug mode.

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ề
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
78. Nếu không, hãy trả lại một bản sao của dữ liệu nhị phân gốc:
endswith(suffix[, start[, end]])bytearray.endswith(suffix[, start[, end]])

Hậu tố có thể là bất kỳ đối tượng giống như byte.

byte.decode (mã hóa = 'utf-8', error = 'nghiêm ngặt')bytes-like object.

Trả về một chuỗi được giải mã từ các byte đã cho. Mã hóa mặc định 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
32. Lỗi có thể được đưa ra để đặt sơ đồ xử lý lỗi khác. Mặc định cho các lỗi 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
33, có nghĩa là các lỗi mã hóa làm tă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
34. Các giá trị có thể khá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
35,
>>> 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à bất kỳ tên nào khác được đăng ký qua
>>> 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, 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.
find(sub[, start[, end]])bytearray.find(sub[, start[, end]])

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.

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ú

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.

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
1

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

byte.removeprefix (tiền tố, /) ¶ bytearray.removeprefix (tiền tố, /) ¶index(sub[, start[, end]])bytearray.index(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ề

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
77. 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.

Tiền tố có thể là bất kỳ đối tượng giống như byte.join(iterable)bytearray.join(iterable)

Ghi chú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'
19 objects. The separator between elements is the contents of the bytes or bytearray object providing this method.

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.bytes.maketrans(from, to)staticbytearray.maketrans(from, to)

Mới trong phiên bản 3.9.bytes-like objects and have the same length.

byte.removesuffix (hậu tố, /) ¶ bytearray.removesuffix (hậu tố, /) ¶

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ề
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
78. Nếu không, hãy trả lại một bản sao của dữ liệu nhị phân gốc:
partition(sep)bytearray.partition(sep)

Hậu tố có thể là bất kỳ đối tượng giống như byte.

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.

byte.replace (cũ, mới [, đếm]) ¶ bytearray.replace (cũ, mới [, đếm]) ¶replace(old, new[, count])bytearray.replace(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.

byte.rfind (sub [, start [, end]]) ¶ bytearray.rfind (sub [, start [, end]]) ¶rfind(sub[, start[, end]])bytearray.rfind(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

>>> 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. Đố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
38 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.

byte.rindex (sub [, start [, end]]) ¶ bytearray.rindex (sub [, start [, end]]) ¶rindex(sub[, start[, end]])bytearray.rindex(sub[, start[, end]])

Giống như

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
25 nhưng tăng
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
74 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.

byte.rindex (sub [, start [, end]]) ¶ bytearray.rindex (sub [, start [, end]]) ¶rpartition(sep)bytearray.rpartition(sep)

Giống như

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
25 nhưng tăng
>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
74 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.

byte.rpartition (sep) ¶ bytearray.rpartition (sep) ¶startswith(prefix[, start[, end]])bytearray.startswith(prefix[, start[, end]])

Chia trình tự ở lần xuất hiện cuối cùng 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 hai byte trống hoặc các đối tượng bytearray, theo sau là một bản sao của chuỗi gốc.

byte.startswith (tiền tố [, start [, end]]) ¶ bytearray.startswith (tiền tố [, start [, end]])bytes-like object.

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
53 Nếu dữ liệu nhị phân bắt đầu với tiền tố được chỉ định, nếu không thì 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
35. Tiền tố cũng có thể là một bộ tiền 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í đó.
translate(table, /, delete=b'')bytearray.translate(table, /, delete=b'')

Tiền tố (ES) để tìm kiếm có thể là bất kỳ đối tượng giống như byte nào.

byte.translate (bảng, /, xóa = b '') ¶ bytearray.translate (bảng, /, xóa = b '')

Trả về một bản sao của các byte hoặc đối tượng bytearray trong đó tất cả các byte xảy ra trong xóa đối số tùy chọn được xóa và các byte còn lại đã được ánh xạ qua bảng dịch đã cho, phải là đối tượng byte có độ dài 256.

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
2

Bạn có thể sử dụng phương thức

[sign] ['0x'] integer ['.' fraction] ['p' exponent]
03 để tạo bảng dịch.delete is now supported as a keyword argument.

Đặt đối số bảng 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
28 cho các bản dịch chỉ xóa các ký tự:

Thay đổi trong phiên bản 3.6: Xóa hiện được hỗ trợ như một đối số từ khóa.center(width[, fillbyte])bytearray.center(width[, fillbyte])

Các phương pháp sau trên byte và các đối tượng bytearray có các hành vi mặc định giả sử việc sử dụng các định dạng nhị phân tương thích ASCII, nhưng vẫn có thể được sử dụng với dữ liệu nhị phân tùy ý bằng cách truyền các đối số thích hợp. 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.

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.

byte.rfind (sub [, start [, end]]) ¶ bytearray.rfind (sub [, start [, end]]) ¶ljust(width[, fillbyte])bytearray.ljust(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

>>> 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. Đố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
38 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.

byte.rfind (sub [, start [, end]]) ¶ bytearray.rfind (sub [, start [, end]]) ¶lstrip([chars])bytearray.lstrip([chars])

Trả về chỉ số cao nhất trong chuỗi nơi tìm thấy phụ con, do đó phụ được chứa 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
45. Đố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
38 khi thất bại.

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
3

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

[sign] ['0x'] integer ['.' fraction] ['p' exponent]
10 for a method that will remove a single prefix string rather than all of a set of characters. For example:

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
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.

Byte.rjust (chiều rộng [, fillbyte]) ¶ bytearray.rjust (chiều rộng [, fillbyte]) ¶rjust(width[, fillbyte])bytearray.rjust(width[, fillbyte])

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'
20, 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'
10.

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.

Byte.rjust (chiều rộng [, fillbyte]) ¶ bytearray.rjust (chiều rộng [, fillbyte]) ¶rsplit(sep=None, maxsplit=- 1)bytearray.rsplit(sep=None, maxsplit=- 1)

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'
20, 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'
10.

byte.rsplit (sep = none, maxsplit = -1) ¶ bytearray.rsplit (sep = none, maxsplit = -1) ¶rstrip([chars])bytearray.rstrip([chars])

Chia trình tự nhị phân thành các chuỗi cùng loạ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
28, bất kỳ phần sau nào chỉ bao gồm khoảng trắng ASCII là một dấu phân cách. Ngoại trừ việc chia tách từ bên phải,
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
29 hoạt động như
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
30 được mô tả chi tiết dưới đây.

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
5

byte.rstrip ([chars]) ¶ bytearray.rstrip ([chars]) ¶bytes-like object. See

[sign] ['0x'] integer ['.' fraction] ['p' exponent]
17 for a method that will remove a single suffix string rather than all of a set of characters. For example:

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
6

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.

Byte.rjust (chiều rộng [, fillbyte]) ¶ bytearray.rjust (chiều rộng [, fillbyte]) ¶split(sep=None, maxsplit=- 1)bytearray.split(sep=None, maxsplit=- 1)

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'
20, 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'
10.

byte.rsplit (sep = none, maxsplit = -1) ¶ bytearray.rsplit (sep = none, maxsplit = -1) ¶bytes-like object.

Chia trình tự nhị phân thành các chuỗi cùng loạ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
28, bất kỳ phần sau nào chỉ bao gồm khoảng trắng ASCII là một dấu phân cách. Ngoại trừ việc chia tách từ bên phải,
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
29 hoạt động như
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
30 được mô tả chi tiết dưới đây.

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
7

byte.rstrip ([chars]) ¶ bytearray.rstrip ([chars]) ¶

Chia trình tự nhị phân thành các chuỗi cùng loạ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
28, bất kỳ phần sau nào chỉ bao gồm khoảng trắng ASCII là một dấu phân cách. Ngoại trừ việc chia tách từ bên phải,
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
29 hoạt động như
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
30 được mô tả chi tiết dưới đây.

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
8

byte.rstrip ([chars]) ¶ bytearray.rstrip ([chars]) ¶strip([chars])bytearray.strip([chars])

Trả về một bản sao của chuỗi với byte byte được chỉ định được xóa. Đố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
28, đối số ký tự mặc định sẽ loại bỏ khoảng trắng ASCII. Đố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:

def to_bytes(n, length=1, byteorder='big', signed=False):
    if byteorder == 'little':
        order = range(length)
    elif byteorder == 'big':
        order = reversed(range(length))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    return bytes((n >> i*8) & 0xff for i in order)
9

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

[sign] ['0x'] integer ['.' fraction] ['p' exponent]
17 để biết mộ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ụ: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.

Byte.rjust (chiều rộng [, fillbyte]) ¶ bytearray.rjust (chiều rộng [, fillbyte]) ¶

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'
20, 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'
10.
capitalize()bytearray.capitalize()

byte.rsplit (sep = none, maxsplit = -1) ¶ bytearray.rsplit (sep = none, maxsplit = -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.

Byte.ExpandTabs (tabSize = 8) ¶ bytearray.expandtabs (tabsize = 8) ¶expandtabs(tabsize=8)bytearray.expandtabs(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 (

[sign] ['0x'] integer ['.' fraction] ['p' exponent]
29), 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ới 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:

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

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.

byte.isalnum () bytearray.alsalnum ()isalnum()bytearray.isalnum()

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
53 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
35 khác. Các ký tự ASCII chữ cái là các giá trị byte trong chuỗi
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
34. Các chữ số thập phân ASCII là các giá trị byte trong chuỗi
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
35.

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
1

byte.isalpha () bytearray.isalpha () ¶isalpha()bytearray.isalpha()

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
53 Nếu tất cả các byte trong chuỗi là các ký tự ASCII chữ cái 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
35 khác. Các ký tự ASCII chữ cái là các giá trị byte trong chuỗi
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
34.

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
2

byte.isalpha () bytearray.isalpha () ¶isascii()bytearray.isascii()

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
53 Nếu tất cả các byte trong chuỗi là các ký tự ASCII chữ cái 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
35 khác. Các ký tự ASCII chữ cái là các giá trị byte trong chuỗi
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
34.

byte.isascii () bytearray.isascii () ¶

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
53 Nếu chuỗi trống hoặc tất cả các byte 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
35 khác. Byte ASCII nằm trong phạm vi 0-0x7F.
isdigit()bytearray.isdigit()

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

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
3

byte.isalpha () bytearray.isalpha () ¶islower()bytearray.islower()

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
53 Nếu tất cả các byte trong chuỗi là các ký tự ASCII chữ cái 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
35 khác. Các ký tự ASCII chữ cái là các giá trị byte trong chuỗi
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
34.

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
4

byte.isalpha () bytearray.isalpha () ¶

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
53 Nếu tất cả các byte trong chuỗi là các ký tự ASCII chữ cái 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
35 khác. Các ký tự ASCII chữ cái là các giá trị byte trong chuỗi
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
34.
isspace()bytearray.isspace()

byte.isascii () bytearray.isascii () ¶

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
53 Nếu chuỗi trống hoặc tất cả các byte 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
35 khác. Byte ASCII nằm trong phạm vi 0-0x7F.
istitle()bytearray.istitle()

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

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
5

byte.isalpha () bytearray.isalpha () ¶isupper()bytearray.isupper()

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
53 Nếu tất cả các byte trong chuỗi là các ký tự ASCII chữ cái 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
35 khác. Các ký tự ASCII chữ cái là các giá trị byte trong chuỗi
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
34.

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
6

byte.isalpha () bytearray.isalpha () ¶

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
53 Nếu tất cả các byte trong chuỗi là các ký tự ASCII chữ cái 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
35 khác. Các ký tự ASCII chữ cái là các giá trị byte trong chuỗi
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
34.
lower()bytearray.lower()

byte.isascii () bytearray.isascii () ¶

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
7

byte.isalpha () bytearray.isalpha () ¶

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.

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
53 Nếu tất cả các byte trong chuỗi là các ký tự ASCII chữ cái 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
35 khác. Các ký tự ASCII chữ cái là các giá trị byte trong chuỗi
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
34.
splitlines(keepends=False)bytearray.splitlines(keepends=False)

byte.isascii () bytearray.isascii () ¶universal newlines approach to splitting lines. Line breaks are not included in the resulting list unless keepends is given and true.

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
8

byte.isalpha () bytearray.isalpha () ¶

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

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
53 Nếu tất cả các byte trong chuỗi là các ký tự ASCII chữ cái 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
35 khác. Các ký tự ASCII chữ cái là các giá trị byte trong chuỗi
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
34.
swapcase()bytearray.swapcase()

byte.isascii () bytearray.isascii () ¶

Ví dụ:

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
0

Các ký tự ASCII viết thường là các giá trị byte trong chuỗi

[sign] ['0x'] integer ['.' fraction] ['p' exponent]
46. Các ký tự ASCII từ trên là các giá trị byte trong chuỗi
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
47.

Không giống như

[sign] ['0x'] integer ['.' fraction] ['p' exponent]
63, luôn luôn là trường hợp
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
64 cho các phiên bản nhị phân. Chuyển đổi trường hợp là đối xứng trong ASCII, mặc dù điều đó thường không đúng với các điểm mã Unicode tùy ý.

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.

byte.title () bytearray.title () ¶title()bytearray.title()

Trả về một phiên bản tiêu chuẩn của chuỗi nhị phân trong đó các từ bắt đầu bằng ký tự ASCII chữ hoa và các ký tự còn lại là chữ thường. Các giá trị byte không bị bỏ lại không được sửa đổi.

Ví dụ:

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
1

Các ký tự ASCII viết thường là các giá trị byte trong chuỗi

[sign] ['0x'] integer ['.' fraction] ['p' exponent]
46. Các ký tự ASCII từ trên là các giá trị byte trong chuỗi
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
47. Tất cả các giá trị byte khác đều không được giới thiệu.

Thuật toán sử dụng một định nghĩa độc lập ngôn ngữ đơn giản của một từ là các nhóm chữ cái liên tiếp. Định nghĩa hoạt động trong nhiều bối cảnh nhưng nó có nghĩa là các dấu nháy đơn trong các cơn co thắt và sở hữu hình thành ranh giới từ, có thể không phải là kết quả mong muốn:

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
2

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 from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

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

byte.title () bytearray.title () ¶upper()bytearray.upper()

Trả về một phiên bản tiêu chuẩn của chuỗi nhị phân trong đó các từ bắt đầu bằng ký tự ASCII chữ hoa và các ký tự còn lại là chữ thường. Các giá trị byte không bị bỏ lại không được sửa đổi.

Ví dụ:

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
4

Các ký tự ASCII viết thường là các giá trị byte trong chuỗi

[sign] ['0x'] integer ['.' fraction] ['p' exponent]
46. Các ký tự ASCII từ trên là các giá trị byte trong chuỗi
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
47.

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.

byte.title () bytearray.title () ¶zfill(width)bytearray.zfill(width)

Trả về một phiên bản tiêu chuẩn của chuỗi nhị phân trong đó các từ bắt đầu bằng ký tự ASCII chữ hoa và các ký tự còn lại là chữ thường. Các giá trị byte không bị bỏ lại không được sửa đổi.

Ví dụ:

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
5

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.

byte.title () bytearray.title () ¶

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.

byte.title () bytearray.title () ¶

Trả về một phiên bản tiêu chuẩn của chuỗi nhị phân trong đó các từ bắt đầu bằng ký tự ASCII chữ hoa và các ký tự còn lại là chữ thường. Các giá trị byte không bị bỏ lại không được sửa đổi.

Các ký tự ASCII viết thường là các giá trị byte trong chuỗi

[sign] ['0x'] integer ['.' fraction] ['p' exponent]
46. Các ký tự ASCII từ trên là các giá trị byte trong chuỗi
[sign] ['0x'] integer ['.' fraction] ['p' exponent]
47. Tất cả các giá trị byte khác đều không được giới thiệu.

  1. Thuật toán sử dụng một định nghĩa độc lập ngôn ngữ đơn giản của một từ là các nhóm chữ cái liên tiếp. Định nghĩa hoạt động trong nhiều bối cảnh nhưng nó có nghĩa là các dấu nháy đơn trong các cơn co thắt và sở hữu hình thành ranh giới từ, có thể không phải là kết quả mong muốn:

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

  3. byte.upper () bytearray.upper () ¶

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

  5. byte.zfill (chiều rộng) ¶ bytearray.zfill (chiều rộng) ¶

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

    [sign] ['0x'] integer ['.' fraction] ['p' exponent]
    
    69 chữ số để tạo ra một chuỗi chiều rộng. Một tiền tố dấu hiệu hàng đầu (________ 970/
    [sign] ['0x'] integer ['.' fraction] ['p' exponent]
    
    71) được xử lý bằng cách chèn đệm sau ký tự dấu hiệu chứ không phải trước. Đố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'
    
    20, chuỗi gốc được trả về nếu chiều rộng nhỏ hơn hoặc bằng
    [sign] ['0x'] integer ['.' fraction] ['p' exponent]
    
    73.

  7. ________ 623 Byte theo phong cá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). Nếu giá trị được in có thể là một tuple hoặc từ điển, hãy bọc nó thành một tuple.

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
6

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

def bit_count(self):
    return bin(self).count("1")
97 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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
90

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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
73

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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
75

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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
73 nếu cả hai được đưa ra).

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
94

.

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
74

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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
74 hoặc
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
75) 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 (

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
98,
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
99 hoặc
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
00) có thể có mặt, nhưng bị bỏ qua vì nó không cần thiết cho Python - vì vậy ví dụ:
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
01 giống hệt với
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
02.

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

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
03

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

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
04

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

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
05

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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
73 nếu cả hai được đưa ra).

(1)

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
06

.

(8)

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
08

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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
74 hoặc
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
75) sẽ đi trước chuyển đổi (ghi đè một lá cờ không gian trên mạng).

(2)

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
09

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

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
98,
def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
99 hoặc
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
00) có thể có mặt, nhưng bị bỏ qua vì nó không cần thiết cho Python - vì vậy ví dụ:
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
01 giống hệt với
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
02.

(2)

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
10

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

(3)

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
11

Chuyển đổi

(3)

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
12

Ghi chú

(3)

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
13

Ghi chú

(3)

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
14

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

(4)

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
15

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

(4)

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
16

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

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
03.

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

Đã 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
016).

(5)

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
19

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

(6)

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
21

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

(5)

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
17

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

(7)

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
83

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

  4. >>> (-2.0).is_integer()
    True
    >>> (3.2).is_integer()
    False
    
    19 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
    
    015 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
    
    021).

  5. >>> (-2.0).is_integer()
    True
    >>> (3.2).is_integer()
    False
    
    17 là bí danh cho
    >>> (-2.0).is_integer()
    True
    >>> (3.2).is_integer()
    False
    
    21 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à ký tự

    def from_bytes(bytes, byteorder='big', signed=False):
        if byteorder == 'little':
            little_ordered = list(bytes)
        elif byteorder == 'big':
            little_ordered = list(reversed(bytes))
        else:
            raise ValueError("byteorder must be either 'little' or 'big'")
    
        n = sum(b << i*8 for i, b in enumerate(little_ordered))
        if signed and little_ordered and (little_ordered[-1] & 0x80):
            n -= 1 << 8*len(little_ordered)
    
        return n
    
    83 trong kết quả.

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

    >>> (-2.0).is_integer()
    True
    >>> (3.2).is_integer()
    False
    
    25) được chèn trước chữ số đầu tiên.

  8. Hình thức thay thế gây ra một

    >>> (-2.0).is_integer()
    True
    >>> (3.2).is_integer()
    False
    
    26 hoặc
    >>> (-2.0).is_integer()
    True
    >>> (3.2).is_integer()
    False
    
    27 hàng đầu (tùy thuộc vào định dạng
    >>> (-2.0).is_integer()
    True
    >>> (3.2).is_integer()
    False
    
    08 hay
    >>> (-2.0).is_integer()
    True
    >>> (3.2).is_integer()
    False
    
    09 đã đượ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à

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
30, đầu ra bị cắt giảm thành
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
30 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 034 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
035 không được chấp nhận, nhưng sẽ không bị xóa trong chuỗi 3.x.buffer protocol without copying.

Xem PEP 237.memoryview(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.

Xem thêm

PEP 461 - Thêm % định dạng vào byte và bytearray

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
7

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

Dưới đây là một ví dụ với định dạng không byte:

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
8

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:

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
9

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

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
0

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

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
38 có một số phương pháp:

__eq __ (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ú 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
051.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
051 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
051 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
056,
def bit_length(self):
    s = bin(self)       # binary representation:  bin(-37) --> '-0b100101'
    s = s.lstrip('-0b') # remove leading zeros and minus sign
    return len(s)       # len('100101') --> 6
057 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
058 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
059:

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
1

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
051, 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):

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
2

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

Tobytes (Order = 'C')(order='C')

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'
20 trên MemoryView.

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
3

Đố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
064 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 ____1051.

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

hex ([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.

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
4

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

Đã thay đổi trong phiên bản 3.8: Tương tự như

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
56,
def bit_length(self):
    s = bin(self)       # binary representation:  bin(-37) --> '-0b100101'
    s = s.lstrip('-0b') # remove leading zeros and minus sign
    return len(s)       # len('100101') --> 6
067 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
>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
56,
def bit_length(self):
    s = bin(self)       # binary representation:  bin(-37) --> '-0b100101'
    s = s.lstrip('-0b') # remove leading zeros and minus sign
    return len(s)       # len('100101') --> 6
067 now supports optional sep and bytes_per_sep parameters to insert separators between bytes in the hex output.

liệt kê()¶()

Trả về dữ liệu trong bộ đệm dưới dạng danh sách các yếu tố.

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
5

Đã 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
056 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
051 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
056 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
051 module syntax as well as multi-dimensional representations.

toreadonly () ¶()

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.

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
6

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

phóng thích()¶()

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'
21 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 trên chế độ xem đều tăng

>>> n = 19
>>> bin(n)
'0b10011'
>>> n.bit_count()
3
>>> (-n).bit_count()
3
74 (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
072 có thể được gọi là nhiều lần):

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
7

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

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
8

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

cast (đị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
074, 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 ____1051. 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:

>>> (-2.0).is_integer()
True
>>> (3.2).is_integer()
False
9

Đúc 1D/byte không dấu thành 1D/char:

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

Đúc 1D/byte đến 3D/INTS đến 1D/đã ký char:

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

Diễn viên 1D/không dấu dài đến 2D/không dấu dài:

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

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:

obj¶

Đối tượng bên dưới của bộ nhớview:

[sign] ['0x'] integer ['.' fraction] ['p' exponent]
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:

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

Mảng đa chiều:

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

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

chỉ đọc¶

Một bool chỉ ra liệu bộ nhớ chỉ được đọc.

định dạng¶

Một chuỗi chứa định dạng (theo kiểu mô -đun ____1051) 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
056) 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
080 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
081.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
080 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
081.

kích thước mục¶

Kích thước tính bằng byte của mỗi phần tử của bộ nhớView:

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

ndim¶

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.

hình dạng¶

Một bộ số số nguyên theo 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
082 cho hình dạng của bộ nhớ dưới dạng một mảng chiều n.

Đã thay đổi trong phiên bản 3.3: Một bộ tuple trống thay 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
28 khi ndim = 0.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
28 when ndim = 0.

sải bước

Một bộ số số nguyên có độ 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
082 cho kích thước bằng byte để truy cập từng phần tử cho mỗi chiều của mảng.

Đã thay đổi trong phiên bản 3.3: Một bộ tuple trống thay 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
28 khi ndim = 0.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
28 when ndim = 0.

sải bước

Một bộ số số nguyên có độ 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
082 cho kích thước bằng byte để truy cập từng phần tử cho mỗi chiều của mảng.

Suboffsets¶

Được sử dụng nội bộ cho các mảng theo phong cách pil. Giá trị chỉ là thông tin.contiguous.

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

c_contiguid¶

Một bool chỉ ra liệu bộ nhớ có phải là C-Contigent hay không.contiguous.

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

f_contigout¶

Một bool chỉ ra liệu bộ nhớ có phải là Fortran tiếp giáp hay không.contiguous.

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

tiếp giáp

Một bool chỉ ra liệu bộ nhớ có tiếp giáp hay không.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'
65,
def bit_count(self):
    return bin(self).count("1")
90, and
def bit_count(self):
    return bin(self).count("1")
91 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
091 module.)

Đặt 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'
66, ________ 467¶

Một đối tượng được thiết lập là một bộ sưu tập không có thứ tự của các đối tượng băm khác biệt. Sử dụng phổ biến bao gồm kiểm tra thành viên, loại bỏ các bản sao khỏi một chuỗi và các hoạt động toán học như giao lộ, liên kết, khác biệt và sự khác biệt đối xứng. .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.

Giống như các bộ sưu tập khác, bộ 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
092,
def bit_length(self):
    s = bin(self)       # binary representation:  bin(-37) --> '-0b100101'
    s = s.lstrip('-0b') # remove leading zeros and minus sign
    return len(s)       # len('100101') --> 6
093 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
094. Là một bộ sưu tập không có thứ tự, các bộ không ghi lại vị trí phần tử hoặc thứ tự chèn. Theo đó, các bộ không hỗ trợ lập chỉ mục, cắt hoặc hành vi giống như trình tự khác.

Hiện tại có hai loại bộ tích 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'
66 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'
67. 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'
66 có thể thay đổi - Nội dung có thể được thay đổi bằng các phương thứ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
098 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'
98. Vì nó có thể thay đổi, nó không có giá trị băm và không thể được sử dụng làm khóa từ điển hoặc là một phần tử của tập khá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'
67 là bất biến và có thể băm - nội dung của nó không thể được thay đổi sau khi nó được tạo ra; Do đó, nó có thể được sử dụng như một khóa từ điển hoặc là một yếu tố của một bộ khác.

Các bộ không trống (không phải đông lạnh) có thể được tạo bằng cách đặt một danh sách các phần tử được phân tách bằng dấu phẩy trong niềng ră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
101, ngoài hàm xây 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'
66.
set([iterable])class frozenset([iterable])

Các hàm tạo cho cả hai lớp hoạt động giống nhau: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'
67 objects. If iterable is not specified, a new empty set is returned.

classset ([itable]) ¶ classFrozenset ([itable]) ¶

  • Trả về một bộ mới hoặc đối tượng Frozenset có các yếu tố được lấy từ Itable. Các yếu tố của một bộ phải được băm. Để thể hiện các bộ của các bộ, các bộ bên trong phải là 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'
    
    67. Nếu có thể được chỉ định, một bộ trống mới được trả về.

  • Các bộ có thể được tạo bằng một số phương tiện:

  • Sử dụng danh sách các yếu tố đượ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
    
    101

Sử dụng sự hiểu biết thiết 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
105

len(s)

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

Các phiên bả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'
66 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'
67 cung cấp các hoạt động sau:
in s

Trả về số lượng các phần tử trong Set S (Cardinality of S).

XINS not in s

Kiểm tra x cho tư cách thành viên trong s.

Xnotins(other)

Kiểm tra x cho không thành viên trong s.

isdisjoint (khác) ¶(other)set <= other

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
53 nếu bộ không có phần tử chung với khác. Các bộ là rời rạc khi và chỉ khi giao điểm của chúng là tập trống.

ISSUBSET (Khác) ¶ Bộ < other

Kiểm tra xem mọi yếu tố trong tập hợp là trong khác.

bộ(other)set >= other

Kiểm tra xem tập hợp có phải là một tập hợp con thích hợp của khác hay không, 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
112.

set>other > other

phát hành (khác) ¶ set> = khác

Kiểm tra xem mọi yếu tố trong khác đều có trong tập hợp.(*others)set | other | ...

Kiểm tra xem tập hợp có phải là một siêu cấp phù hợp của khác hay không, 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
113.

Liên minh (*những người khác) ¶ Set | khác | ...(*others)set & other & ...

Trả về một bộ mới với các phần tử từ bộ và tất cả các phần tử khác.

Giao lộ (*những người khác) ¶ Set & khác & ...(*others)set - other - ...

Trả về một bộ mới với các yếu tố phổ biến cho tập hợp và tất cả các yếu tố khác.

sự khác biệt (*người khác) ¶ Set-other -...(other)set ^ other

Trả về một bộ mới với các phần tử trong tập hợp không có trong các phần khác.

symmetric_diferference (khác) ¶ set^khác()

Trả về một bộ mới với các phần tử trong tập hợp hoặc khác nhưng không phải cả hai.

sao chép () ¶

Cả hỗ 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'
66 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'
67 được thiết lập để đặt so sánh. Hai bộ bằng nhau khi và chỉ khi mỗi phần tử của mỗi bộ được chứa trong phần kia (mỗi bộ là một tập hợp con của phần kia). Một tập hợp nhỏ hơn một tập khác khi và chỉ khi tập đầu tiên là một tập hợp con thích hợp của bộ thứ hai (là một tập hợp con, nhưng không bằng nhau). Một tập hợp lớn hơn một tập khác khi và chỉ khi tập đầu tiên là một superset thích hợp của bộ thứ hai (là một superset, nhưng không bằng nhau).

Các trường hợp 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'
66 được so sánh với 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'
67 dựa trên các thành viên của họ. 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
126 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
53 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
128 cũng vậy.

Các tập hợp con và so sánh bình đẳng không khái quát cho một hàm đơn đặt hàng. Ví dụ: bất kỳ hai bộ phân tách không trống nào không bằng nhau và không phải là tập hợp của nhau, vì vậy tất cả các 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
35:
def bit_length(self):
    s = bin(self)       # binary representation:  bin(-37) --> '-0b100101'
    s = s.lstrip('-0b') # remove leading zeros and minus sign
    return len(s)       # len('100101') --> 6
130,
def bit_length(self):
    s = bin(self)       # binary representation:  bin(-37) --> '-0b100101'
    s = s.lstrip('-0b') # remove leading zeros and minus sign
    return len(s)       # len('100101') --> 6
131 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
132.

Vì các bộ chỉ xác định thứ tự một phần (mối quan hệ tập hợp con), đầu ra của 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
133 không được xác định cho danh sách các bộ.

Đặt các yếu tố, như các khóa từ điển, phải có thể băm.hashable.

Các hoạt động nhị phân kết hợp 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'
66 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'
67 Trả về loại của toán hạng đầu tiên. 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
136 Trả về một thể hiệ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'
67.

Bảng sau đây liệt kê các hoạt động có sẵn 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'
66 không áp dụng cho các trường hợp bất biế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'
67:

Cập nhật (*những người khác) ¶ Set | = Khác | ...(*others)set |= other | ...

Cập nhật bộ, thêm các yếu tố từ tất cả các yếu tố khác.

Intersection_Update (*người khác) ¶ Set & = khác & ...(*others)set &= other & ...

Cập nhật bộ, chỉ giữ các yếu tố được tìm thấy trong đó và tất cả các yếu tố khác.

arction_update (*người khác) ¶ set- = other | ...(*others)set -= other | ...

Cập nhật bộ, loại bỏ các yếu tố được tìm thấy trong các yếu tố khác.

symmetric_difference_update (khác) ¶ set^= other(other)set ^= other

Cập nhật tập hợp, chỉ giữ các yếu tố được tìm thấy trong một trong hai tập hợp, nhưng không phải trong cả hai.

Thêm (elem) ¶(elem)

Thêm phần tử ELEM vào tập hợp.

loại bỏ (elem) ¶(elem)

Loại bỏ phần tử ELEM khỏi tập hợp. 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
140 nếu ELEM không có trong tập hợp.

loại bỏ (elem) ¶(elem)

Loại bỏ phần tử ELEM khỏi tập hợp nếu nó có mặt.

nhạc pop()¶()

Xóa và trả về một phần tử tùy ý khỏi tập hợp. 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
140 nếu bộ trống.

xa lạ()¶()

Loại bỏ tất cả các phần tử khỏi tập hợp.

Lưu ý, các phiên bản không vận hành của 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
142,
def bit_length(self):
    s = bin(self)       # binary representation:  bin(-37) --> '-0b100101'
    s = s.lstrip('-0b') # remove leading zeros and minus sign
    return len(s)       # len('100101') --> 6
143,
def bit_length(self):
    s = bin(self)       # binary representation:  bin(-37) --> '-0b100101'
    s = s.lstrip('-0b') # remove leading zeros and minus sign
    return len(s)       # len('100101') --> 6
144 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
145 sẽ chấp nhận bất kỳ sự khác biệt nào như một đối số.

Lưu ý, đối số ELEM cho 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
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'
98 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
148 có thể là một tập hợp. Để hỗ trợ tìm kiếm một chiếc Frozenset tương đương, một cái tạm thời được tạo ra từ ELEM.

Các loại ánh xạ - ________ 465¶

Một bản đồ đối tượng ánh xạ các giá trị băm vào các đối tượng tùy ý. Ánh xạ là các đối tượng có thể thay đổi. Hiện tại chỉ có một loại ánh xạ tiêu chuẩn, từ điển. .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

def bit_count(self):
    return bin(self).count("1")
90,
>>> (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'
66, and
def bit_count(self):
    return bin(self).count("1")
91 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
091 module.)

Một phím từ điển từ các giá trị gần như tùy ý. Các giá trị không thể băm, nghĩa là các giá trị chứa danh sách, từ điển hoặc các loại có thể thay đổi khác (được so sánh theo giá trị thay vì nhận dạng đối tượng) không được sử dụng làm khóa. Các giá trị so sánh bằng nhau (chẳng hạ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
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
155 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
53) có thể được sử dụng thay thế cho nhau để lập chỉ mục cùng một mục từ điển.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. Values that 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
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
155, 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
53) can be used interchangeably to index the same dictionary entry.

ClassDict (** kwargs) classdict (ánh xạ, ** kwargs) classdict (itable, ** kwargs) dict(**kwargs)classdict(mapping, **kwargs) classdict(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
    
    157 đượ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
    
    158 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
    
    159

  • 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
    
    47,
    def bit_length(self):
        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

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

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

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

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ợ):

list(d)

Trả về một danh sách tất cả các khóa được sử dụng trong từ điển d.

len(d)

Trả lại số lượng các mục trong từ điển d.

d[key]

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
140 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
167 và khóa không 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
168 sẽ gọi phương thức đó với khóa chính là đố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
168 sau đó trả lại 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
170. 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
167. 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
167 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
140 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
167 phải là một phương pháp; Nó không thể là một biến thể hiện:

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

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
175. 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
176 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
177.

d[key]=value = value

Đặ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
168 thành Giá trị.

deld[key] d[key]

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
168 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
140 nếu khóa không có trong bản đồ.

chính in d

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

Keynotind not in d

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

iter(d)

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

xa lạ()¶()

Hủy bỏ tất cả các mục khỏi từ điển.

sao chép () ¶()

Trả lại một bản sao nông của từ điển.

classMethodFromKeys (itable [, value]) ¶fromkeys(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
185 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
28. 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.

Nhận (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
28, 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
140.

mặt hàng()¶()

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
189 cặp). Xem tài liệu của các đối tượng xem.documentation of view objects.

chìa khóa () ¶()

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.

pop (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
140 sẽ được nâng lên.

Popitem ()()

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
189 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
192 rất hữu ích để lặp lại một cách phá hủy trong 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
192 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
140.

Đã 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
192 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
192 would return an arbitrary key/value pair.

reversed(d)

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

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

setDefault (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
28.

Cập nhật ([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
28.

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

giá trị ()()

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
201 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
35. Đ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
201 với chính nó:

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

D | Khác | other

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.

D | = Khác |= other

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.

D | = Khác

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.

def bit_length(self):
    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

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
204 (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
85.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.

def bit_length(self):
    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

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.

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

len(dictview)

Trả lại số lượng mục trong từ điển.

iter(dictview)

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
189) 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
210 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
211:
def bit_length(self):
    s = bin(self)       # binary representation:  bin(-37) --> '-0b100101'
    s = s.lstrip('-0b') # remove leading zeros and minus sign
    return len(s)       # len('100101') --> 6
212. 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
213.

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
214 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.Dictionary order is guaranteed to be insertion order.

XindictView in dictview

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

reversed(dictview)

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.Dictionary views are now reversible.

dictview.mapping

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
217 kết thúc từ điển ban đầu mà chế độ xem đề cập.

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

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

def bit_length(self):
    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

Trình quản lý bối cảnh Loại

Tuyên bố Python từ ____1073 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:

bối cảnh người quản lý .__ Nhập __ () ¶__enter__()

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
224 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
073 bằng cách sử dụng trình quản lý ngữ cảnh này.

Một ví dụ về trình quản lý bối cảnh tự trả về là một đối tượng tệp. Các đối tượng tệp tự trả lại từ __NENTER __ () để cho 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
226 được sử dụng làm biểu thức ngữ cảnh trong 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
073.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
226 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
073 statement.

Một ví dụ về trình quản lý bối cảnh trả về một đối tượng liên quan là một đố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
228. Các nhà quản lý này đặt bối cảnh thập phân chủ động thành một bản sao của bối cảnh thập phân gốc và sau đó trả lại bản sao. Điều này cho phép các thay đổi được thực hiện theo bối cảnh thập phân hiện tại trong phần thân của tuyên 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
073 mà không ảnh hưởng đến mã bên ngoài 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
073.

Bối cảnh bối cảnh .__ Thoát __ (exc_type, exc_val, exc_tb) ¶__exit__(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
073, 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
28.

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

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ý ngữ 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
236 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
237 để 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
238 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
238, 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
240 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
236 cần thiết, thay vì trình lặp được tạo ra bởi hàm tạo máy phát không được trang bị.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
238 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
238 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
240 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
236 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
242 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ư
def bit_count(self):
    return bin(self).count("1")
90 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'
65. 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
245 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
242 được tạo bằng cách đăng ký lớp
def bit_count(self):
    return bin(self).count("1")
90 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
98. 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
242 đượ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
def bit_count(self):
    return bin(self).count("1")
90 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'
65. 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
245 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
242 object created by subscripting the
def bit_count(self):
    return bin(self).count("1")
90 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
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
242 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
250.

Đố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
242 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
252 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'
66 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'
20.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
252 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'
66 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'
20.

Đố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
250 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
256 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'
19 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'
20:

  • 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
    
    259,
    >>> n = 19
    >>> bin(n)
    '0b10011'
    >>> n.bit_count()
    3
    >>> (-n).bit_count()
    3
    
    79 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
    
    261 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
    
    262 sẽ 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'
    
    19. 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
    
    242
    def bit_length(self):
        s = bin(self)       # binary representation:  bin(-37) --> '-0b100101'
        s = s.lstrip('-0b') # remove leading zeros and minus sign
        return len(s)       # len('100101') --> 6
    
    265.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
    
    261 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
    
    262 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'
    
    19. 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
    
    242
    def bit_length(self):
        s = bin(self)       # binary representation:  bin(-37) --> '-0b100101'
        s = s.lstrip('-0b') # remove leading zeros and minus sign
        return len(s)       # len('100101') --> 6
    
    265.

  • 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
    
    266, (lưu ý
    >>> (-2.0).is_integer()
    True
    >>> (3.2).is_integer()
    False
    
    43 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'
    
    20),
    def bit_count(self):
        return bin(self).count("1")
    
    28 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
    
    270, 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
    
    271 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
    
    272 sẽ là 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'
    
    20. 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
    
    274.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
    
    274.

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
242 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
276, 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
242.

T[X,Y,...] Y, ...]

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
242 đạ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
279 đượ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
279 được sử dụng. Ví dụ: một hàm mong đợi một
def bit_count(self):
    return bin(self).count("1")
90 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
99:

def bit_length(self):
    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

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'
65, đâ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'
65 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'
19 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
98: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'
65, 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'
65 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'
19 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
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
04

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
287 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
288 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
242 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
05

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
242, 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
242, 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
06

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
07

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
29 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
30 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
08

Phương pháp

def from_bytes(bytes, byteorder='big', signed=False):
    if byteorder == 'little':
        little_ordered = list(bytes)
    elif byteorder == 'big':
        little_ordered = list(reversed(bytes))
    else:
        raise ValueError("byteorder must be either 'little' or 'big'")

    n = sum(b << i*8 for i, b in enumerate(little_ordered))
    if signed and little_ordered and (little_ordered[-1] & 0x80):
        n -= 1 << 8*len(little_ordered)

    return n
65 của các thùng chứa chung sẽ gây ra 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
294:

def bit_length(self):
    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

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

def bit_length(self):
    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

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.

  • def bit_count(self):
        return bin(self).count("1")
    
    91

  • def bit_count(self):
        return bin(self).count("1")
    
    90

  • >>> (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'
    
    65

  • >>> (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'
    
    66

  • >>> (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'
    
    67

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

  • def bit_length(self):
        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

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

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

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

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

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

  • def bit_length(self):
        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

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

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

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

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

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

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

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

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

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

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

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

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

  • def bit_length(self):
        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

  • def bit_length(self):
        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

  • def bit_count(self):
        return bin(self).count("1")
    
    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'
    
    69

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

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

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

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

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

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

  • def bit_length(self):
        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

  • def bit_length(self):
        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

  • def bit_length(self):
        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

  • def bit_length(self):
        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

  • def bit_length(self):
        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

  • def bit_length(self):
        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

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

  • def bit_length(self):
        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

  • def bit_length(self):
        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

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

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

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

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

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

  • def bit_length(self):
        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

  • def bit_length(self):
        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

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
242 Đố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.

Genericalias .__ Origin__¶__origin__

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
11

Genericalias .__ Args__¶__args__

Thuộc tính này là

def bit_count(self):
    return bin(self).count("1")
91 (có thể là độ dài 1) của các loại chung được truyề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
250 ban đầu 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
12

Genericalias .__ Tham số __¶__parameters__

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

def bit_length(self):
    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

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
242 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
354 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
355 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
354 được dự định chủ yếu để kiểm tra loại tĩnh.

Genericalias .__ Giải nén__¶__unpacked__

Boolean là đúng nếu bí danh đã được giải nén bằng toán tử

def bit_count(self):
    return bin(self).count("1")
97 (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
358).

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

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

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

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
361 (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
362.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
362.

X | y | ... | Y | ...

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
363 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
364. Ví dụ: chức năng 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
98 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
99:

def bit_length(self):
    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

union_object==other == other

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
    
    15

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

  • 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
    
    362:

    def bit_length(self):
        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

  • 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
    
    28:

    def bit_length(self):
        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

isinstance(obj,union_object)issubclass(obj,union_object) union_object) issubclass(obj, union_object)

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
287 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
288 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
19

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
20

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
371 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
287. 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
21

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
242 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
354 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
355 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
354 được dự định chủ yếu để 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
22

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

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 360

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 361 (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 362.

X | y | ...Objects, values and types and Class definitions for these.

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 363 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 364. Ví dụ: chức năng 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 98 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 99:

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:

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
390 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
391 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
392 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
393 là hàm 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
394 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
395.

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
396), 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
397 đượ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
23

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
398 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
399 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
400.

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
399 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
402 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
403 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
404.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
402 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
403 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
404.

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

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
398 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
399 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
400.

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
399 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
402 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
403 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
404.

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

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
407. 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
408 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 409.

Đố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
413 (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
414 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
413 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
28 (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
411 tạo ra 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 28.

Đố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
418 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
419 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
413 (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
414 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
413.

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

Đối tượng được thực hiệnTruth Value Testing above).

Đối tượng này được trả về từ các so sánh và hoạt động nhị phân khi chúng được yêu cầu hoạt động theo các loại mà họ không hỗ trợ. Xem so sánh để biết thêm thông tin. Có chính xác 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
418.
def bit_length(self):
    s = bin(self)       # binary representation:  bin(-37) --> '-0b100101'
    s = s.lstrip('-0b') # remove leading zeros and minus sign
    return len(s)       # len('100101') --> 6
419 Sản xuất ví dụ 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 418.

Giá trị BooleanThe standard type hierarchy for this information. It describes stack frame objects, traceback objects, and slice objects.

Các giá trị Boolean là hai đối tượng không đổ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 35 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 53. Chúng được sử dụng để thể hiện các giá trị sự thật (mặc dù các giá trị khác cũng có thể được coi là sai hoặc đúng). Trong bối cảnh số (ví dụ khi được sử dụng làm đối số cho toán tử số học), chúng hoạt động như số nguyên 0 và 1, tương ứng. 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 423 có thể được sử dụng để chuyển đổi bất kỳ giá trị nào thành boolean, nếu giá trị có thể được hiểu là giá trị sự thật (xem phần Kiểm tra giá trị sự thật ở trên).

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

Đối tượng bên trong__dict__

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.

Thuộc tính đặc biệt__class__

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

Đối tượng .__ Dict__¶__bases__

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

ví dụ .__ lớp __¶__name__

Lớp mà một thể hiện lớp thuộc về.

Lớp .__ Base__¶__qualname__

Tuple của các lớp cơ sở của một đối tượng lớp.qualified name of the class, function, method, descriptor, or generator instance.

Định nghĩa .__ Name__¶

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

Định nghĩa .__ Qualname__¶

class.mro () ¶mro()

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

lớp .__ Các lớp con __ ()__subclasses__()

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
24

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
98 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'
19 để 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
98 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
431 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
74 đượ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
25

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

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
26

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

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
98 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'
19 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'
20:

  • def bit_length(self):
        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 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
    
    439 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
    
    440.

  • def bit_length(self):
        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

  • 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
    
    442,
    def bit_length(self):
        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 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
    
    444.

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
    
    439 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
    
    446 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
    
    447.

  • def bit_length(self):
        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,
    def bit_length(self):
        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,
    def bit_length(self):
        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.

  • Đặ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'
    
    19 đế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
    
    99.

  • >>> (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 đến
    >>> n = 19
    >>> bin(n)
    '0b10011'
    >>> n.bit_count()
    3
    >>> (-n).bit_count()
    3
    
    05.

Đị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
    
    455, 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
    
    456 để đặ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
    
    457 để 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
    
    458, 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
    
    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 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
    
    455 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
    
    458. 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
    
    463 đượ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
    
    463 đượ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
    
    433 đã đượ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
    
    455 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
    
    458. 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
    
    463 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
    
    463 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
    
    433 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
466 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
    
    467 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
    
    468 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
469:

  • def bit_length(self):
        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 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
    
    434 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.11.

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
98 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'
19 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'
20:

def bit_length(self):
    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 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 439 cho tất cả các cơ sở không phải là sức mạnh của 2.

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
442,
def bit_length(self):
    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 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
444.

Example:

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:

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

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
478 đượ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
479 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.