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') --> 628. 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') --> 629 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') --> 630 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') --> 631. Kiểm tra giá trị sự thậtBấ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') --> 632 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') --> 633 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') --> 634 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') --> 635 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') --> 636 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 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') --> 639 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') --> 635 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') --> 652 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') --> 653 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:
Notes:
So sánh lorCó 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') --> 668 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') --> 669, 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') --> 670 được tìm thấy là sai). Bảng này tóm tắt các hoạt động so sánh:
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') --> 668 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') --> 669, 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') --> 670 đượ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') --> 697 method. lớn hơn lớn hơnlớ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() 301. 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() 302 and >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 303. (The standard library includes the additional numeric types >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 304, for rationals, and >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 305, 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() 306 hoặc >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 307 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() 308, >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 309 và >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 310 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):
Notes:
Tất cả các loại >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 353 ( def bit_length(self): s = bin(self) # binary representation: bin(-37) --> '-0b100101' s = s.lstrip('-0b') # remove leading zeros and minus sign return len(s) # len('100101') --> 698 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') --> 699) cũng bao gồm các hoạt động sau:
sản phẩm của x và y chỉ số của x và ychỉ 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() 314 x phủ định
Notes:
x chuyển đổi thành số nguyênLoạ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() 378. 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() 379 là khác nhau, thì >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 380 là số nguyên dương duy nhất >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 381 sao cho >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 382. Tương đương, khi >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 320 đủ nhỏ để có logarit tròn chính xác, sau đó >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 384. Nếu >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 379 bằng không, thì >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 380 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') --> 639. 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() 388 đượ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() 396 and >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 397. 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() 388 đượ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() 397. 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() 388 đượ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() 389. Nếu byteorder là >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 389, 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() 391, 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') --> 635 và số nguyên âm được đưa ra, một >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 388 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') --> 635. 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() 388: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() 396 và >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 397. 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() 389. Nếu byteorder là >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 389, 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() 391, 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() 397. [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') --> 60 Á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') --> 61 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() 379 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') --> 698, def bit_length(self): s = bin(self) # binary representation: bin(-37) --> '-0b100101' s = s.lstrip('-0b') # remove leading zeros and minus sign return len(s) # len('100101') --> 699, >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 305 và >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 304) 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() 304, 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') --> 699 và >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 305. 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 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:
Để 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') --> 699 hoặc >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 300: def bit_length(self): s = bin(self) # binary representation: bin(-37) --> '-0b100101' s = s.lstrip('-0b') # remove leading zeros and minus sign return len(s) # len('100101') --> 62 Các loại IteratorPython 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') --> 695. 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') --> 695 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átCá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ếnCá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') --> 695 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') --> 696 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() 366 (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
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:
Các loại trình tự bất biến JorHoạ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') --> 685. Các loại trình tự có thể thay đổiCá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).
Notes:
Danh sáchDanh 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:
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') --> 646. 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') --> 671 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') --> 628 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') --> 653, 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() 374 if it can detect that the list has been mutated during a sort. Bài hátCá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:
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') --> 645. 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') --> 698 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') --> 652. 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') --> 639. Nếu bước bằng không, >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 374 đượ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() 388. 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') --> 65 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') --> 639 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') --> 652 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') --> 66 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') --> 675 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') --> 676 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') --> 698 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 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
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:
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') 1671168000. 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') 1671168003 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') --> 630 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') --> 630 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') 1671168006 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') 1671168007, đó 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') 1671168008, 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') --> 630 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') 1671168010. 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') 1671168015 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') 1671168016. 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') 1671168017. 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') 1671168015 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') 1671168016. 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') 1671168017. 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') --> 630 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') 1671168020 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') --> 67 Để 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ỗiCá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') 1671168022, 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') 1671168023. Để 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') 1671168023 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') 1671168024).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') 1671168024 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') 1671168025 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') 1671168026. 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') 1671168027 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') 1671168025; >>> 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') 1671168029 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') 1671168026. 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') 1671168032. 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') 1671168033, 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') 1671168034. 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') 1671168035, >>> 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') 1671168036, >>> 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') 1671168037, >>> 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') 1671168038 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') 1671168039, 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') --> 653 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') --> 635. 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') 1671168042), 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') --> 68 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') 1671168045. Đố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() 338 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') 1671168047 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') --> 695: def bit_length(self): s = bin(self) # binary representation: bin(-37) --> '-0b100101' s = s.lstrip('-0b') # remove leading zeros and minus sign return len(s) # len('100101') --> 69 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') --> 647. 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() 30 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') --> 698, def bit_length(self): s = bin(self) # binary representation: bin(-37) --> '-0b100101' s = s.lstrip('-0b') # remove leading zeros and minus sign return len(s) # len('100101') --> 699, >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 300, >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 305 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') 1671168055) 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') 1671168057 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') 1671168056. 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 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') 1671168056 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') 1671168057 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') 1671168066, 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') 1671168067 đượ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') 1671168067 là một lớp con Dict: >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 31 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') 1671168047, nhưng tăng >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 374 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') --> 653 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') --> 635 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') 1671168074 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') --> 653: >>> 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') 1671168076, >>> 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') 1671168077, >>> 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') 1671168078 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') 1671168079. 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') --> 653 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') --> 635 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') --> 653 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') --> 635 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') --> 653 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') --> 635 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') --> 653 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') --> 635 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') --> 653 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') 1671168089 để 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') 1671168090 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') 1671168091 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') 1671168092. Example: >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 32 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') --> 653 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') --> 635 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') --> 653 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') --> 635 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') --> 653 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') --> 635 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') --> 629 đượ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') --> 653 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') --> 635 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 n04), 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 n05 (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 n06, 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 n07 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 n08. 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') --> 653 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') --> 635 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') --> 653 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') --> 635 nếu không.
>>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 33 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') --> 685 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') --> 628, đố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() 34 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 n17 để 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() 35 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 n18. 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') --> 628. 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') --> 628 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 n21. 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() 36 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 n22. 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() 37 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 n22. 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') 1671168045. Đố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() 338 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 n25 nhưng tăng >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 374 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') --> 628, đố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() 38 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 n32 để 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() 39 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 n33). Nếu MAXSplit không được chỉ định hoặc >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 338, 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 n35 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 n36). Đố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 n37 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 n38). 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 n39. 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') --> 628, 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') --> 628 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') --> 646. 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') --> 628, 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') --> 628 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') --> 646.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â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 n46 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 n48 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') --> 628, 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') --> 628 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') --> 646. 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 n46 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 n47 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 n48 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 n49 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') --> 628, 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') --> 628 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') --> 646. 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') --> 628, 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 n67 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 n68 để 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 n69 để 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 n70 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') --> 635 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') 1671168090 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 n73 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áchGhi 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') 1671168022 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') 1671168022 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 n79 (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 n80 (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 n79 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 n82 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:
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 n83. 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à:
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 n98, 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 n99 hoặc >>> (-2.0).is_integer() True >>> (3.2).is_integer() False00) 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() False01 giống hệt với >>> (-2.0).is_integer() True >>> (3.2).is_integer() False02. Các loại chuyển đổi là:
Notes:
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() False32 không cho rằng >>> (-2.0).is_integer() True >>> (3.2).is_integer() False33 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() False34 conversions for numbers whose absolute value is over 1e50 are no longer replaced by >>> (-2.0).is_integer() True >>> (3.2).is_integer() False35 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() False38 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() False42 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() False43 được thêm vào:
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() False49 (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() 374). Đ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:
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() False55 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() False56 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() False57 sẽ là một số nguyên, trong khi >>> (-2.0).is_integer() True >>> (3.2).is_integer() False58 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() False59) vì nó thường hữu ích hơn ví dụ: >>> (-2.0).is_integer() True >>> (3.2).is_integer() False60. 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() False61. Đối tượng bytearrayCá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:
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() False69 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() False56, >>> (-2.0).is_integer() True >>> (3.2).is_integer() False71 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() False57 sẽ là một số nguyên, trong khi >>> (-2.0).is_integer() True >>> (3.2).is_integer() False58 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() False59) vì nó thường hữu ích hơn ví dụ: >>> (-2.0).is_integer() True >>> (3.2).is_integer() False60. 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() False61. Đối tượng bytearrayCá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() False64 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() False64 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() False65count(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() False77. 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() False78. 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() False78. 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() False78. 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') 1671168032. 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') 1671168033, 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') 1671168034. 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') 1671168035, >>> 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') 1671168036 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') 1671168039, 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() False77. 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() False78. 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') 1671168045. Đố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() 338 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 n25 nhưng tăng >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 374 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 n25 nhưng tăng >>> n = 19 >>> bin(n) '0b10011' >>> n.bit_count() 3 >>> (-n).bit_count() 374 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') --> 653 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') --> 635. 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 Đặ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') --> 628 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') 1671168045. Đố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() 338 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') 1671168045. Đố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() 338 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') --> 628, 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 n29 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 n30 đượ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') --> 628, 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 n29 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 n30 đượ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') --> 628, 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 n29 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 n30 đượ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') --> 628, đố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') 167116800 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') --> 653 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') --> 635 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') 167116801 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') --> 653 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') --> 635 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') 167116802 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') --> 653 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') --> 635 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') --> 653 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') --> 635 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') 167116803 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') --> 653 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') --> 635 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') 167116804 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') --> 653 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') --> 635 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') --> 653 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') --> 635 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') 167116805 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') --> 653 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') --> 635 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') 167116806 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') --> 653 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') --> 635 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') 167116807 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') --> 653 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') --> 635 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') 167116808 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') 167116809 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') --> 653 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') --> 635 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 n0 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 n1 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 n2 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 n3 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 n4 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 n5 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.
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 n6 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à:
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 n98, 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 n99 hoặc >>> (-2.0).is_integer() True >>> (3.2).is_integer() False00) 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() False01 giống hệt với >>> (-2.0).is_integer() True >>> (3.2).is_integer() False02. Các loại chuyển đổi là:
Notes:
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à def bit_length(self): 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') --> 6035 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 n7 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 n8 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 n9 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') --> 6052: >>> (-2.0).is_integer() True >>> (3.2).is_integer() False0 Đã 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() False38 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') --> 6051.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') --> 6051 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') --> 6051 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') --> 6056, def bit_length(self): s = bin(self) # binary representation: bin(-37) --> '-0b100101' s = s.lstrip('-0b') # remove leading zeros and minus sign return len(s) # len('100101') --> 6057 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') --> 6058 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') --> 6059: >>> (-2.0).is_integer() True >>> (3.2).is_integer() False1 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') --> 6051, 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() False2 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') --> 6061 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') --> 6062 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() False3 Đố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') --> 6064 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() False4 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() False56, def bit_length(self): s = bin(self) # binary representation: bin(-37) --> '-0b100101' s = s.lstrip('-0b') # remove leading zeros and minus sign return len(s) # len('100101') --> 6067 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() False5 Đã 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') --> 6056 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') --> 6051 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() False6 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() 374 (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') --> 6072 có thể được gọi là nhiều lần): >>> (-2.0).is_integer() True >>> (3.2).is_integer() False7 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') --> 6073: >>> (-2.0).is_integer() True >>> (3.2).is_integer() False8 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') --> 6074, 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() False9 Đú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') --> 6056) 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') --> 6080 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') --> 6081. 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') --> 6082 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') --> 628 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') --> 6082 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') --> 628 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') --> 6082 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ápMộ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') --> 6091 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') --> 6092, def bit_length(self): s = bin(self) # binary representation: bin(-37) --> '-0b100101' s = s.lstrip('-0b') # remove leading zeros and minus sign return len(s) # len('100101') --> 6093 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') --> 6094. 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') --> 6098 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') --> 6101, 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]) ¶
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') --> 6105 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') --> 648, def bit_length(self): s = bin(self) # binary representation: bin(-37) --> '-0b100101' s = s.lstrip('-0b') # remove leading zeros and minus sign return len(s) # len('100101') --> 6107, def bit_length(self): s = bin(self) # binary representation: bin(-37) --> '-0b100101' s = s.lstrip('-0b') # remove leading zeros and minus sign return len(s) # len('100101') --> 6108 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 sKiể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 <= otherTrả 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') --> 653 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 >= otherKiể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') --> 6112. 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') --> 6113. 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 ^ otherTrả 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') --> 6126 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') --> 653 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') --> 6128 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') --> 635: def bit_length(self): s = bin(self) # binary representation: bin(-37) --> '-0b100101' s = s.lstrip('-0b') # remove leading zeros and minus sign return len(s) # len('100101') --> 6130, def bit_length(self): s = bin(self) # binary representation: bin(-37) --> '-0b100101' s = s.lstrip('-0b') # remove leading zeros and minus sign return len(s) # len('100101') --> 6131 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') --> 6132. 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') --> 6133 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') --> 6136 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 ^= otherCậ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') --> 6140 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') --> 6140 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') --> 6142, def bit_length(self): s = bin(self) # binary representation: bin(-37) --> '-0b100101' s = s.lstrip('-0b') # remove leading zeros and minus sign return len(s) # len('100101') --> 6143, def bit_length(self): s = bin(self) # binary representation: bin(-37) --> '-0b100101' s = s.lstrip('-0b') # remove leading zeros and minus sign return len(s) # len('100101') --> 6144 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') --> 6145 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') --> 697, >>> (1024).to_bytes(2, byteorder='big') b'\x04\x00' >>> (1024).to_bytes(10, byteorder='big') b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00' >>> (-1024).to_bytes(10, byteorder='big', signed=True) b'\xff\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') --> 6148 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') --> 6091 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') --> 652, def bit_length(self): s = bin(self) # binary representation: bin(-37) --> '-0b100101' s = s.lstrip('-0b') # remove leading zeros and minus sign return len(s) # len('100101') --> 6155 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') --> 653) 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') --> 652, def bit_length(self): s = bin(self) # binary representation: bin(-37) --> '-0b100101' s = s.lstrip('-0b') # remove leading zeros and minus sign return len(s) # len('100101') --> 6155, 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') --> 653) 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:
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') --> 6165: [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') --> 6140 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') --> 6167 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') --> 6168 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') --> 6168 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') --> 6170. 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') --> 6167. 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') --> 6167 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') --> 6140 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') --> 6167 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') --> 6175. 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') --> 6176 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') --> 6177. 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') --> 6168 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') --> 6168 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') --> 6140 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') --> 653 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') --> 635. 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') --> 6183. 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') --> 6184. 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') --> 6185 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') --> 628. 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') --> 628, 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') --> 6140. 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') --> 6189 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') --> 6140 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') --> 6189 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') --> 6192 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') --> 6192 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') --> 6140. Đã 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') --> 6192 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') --> 6196. 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') --> 628. 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') --> 628. def bit_length(self): s = bin(self) # binary representation: bin(-37) --> '-0b100101' s = s.lstrip('-0b') # remove leading zeros and minus sign return len(s) # len('100101') --> 6142 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') --> 6200. 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') --> 6201 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') --> 635. Đ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') --> 6201 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 |= otherCậ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') --> 600 Từ điển so sánh bằng nhau khi và chỉ khi chúng có cùng cặp 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') --> 601 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') --> 6206, def bit_length(self): s = bin(self) # binary representation: bin(-37) --> '-0b100101' s = s.lstrip('-0b') # remove leading zeros and minus sign return len(s) # len('100101') --> 6201 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') --> 6208 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') --> 6189) 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') --> 6210 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') --> 6211: def bit_length(self): s = bin(self) # binary representation: bin(-37) --> '-0b100101' s = s.lstrip('-0b') # remove leading zeros and minus sign return len(s) # len('100101') --> 6212. 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') --> 6213. 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') --> 6214 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 dictviewTrả 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') --> 653 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') --> 6189). 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.mappingTrả 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') --> 6217 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') --> 6189 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') --> 602 Trình quản lý bối cảnh LoạiTuyê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') --> 6224 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') --> 6073 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') --> 6226 đượ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') --> 6073.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') --> 6226 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') --> 6073 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') --> 6228. 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') --> 6073 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') --> 6073. 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') --> 6073, 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') --> 628. 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') --> 6073 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') --> 6073. 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') --> 6073. 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') --> 6236 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') --> 6237 để 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') --> 6238 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') --> 6238, 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') --> 6240 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') --> 6236 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') --> 6238 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') --> 6238 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') --> 6240 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') --> 6236 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ạiCá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') --> 6242 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') --> 6245 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') --> 6242 đượ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') --> 698. 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') --> 6242 đượ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') --> 6245 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') --> 6242 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') --> 698. def bit_length(self): s = bin(self) # binary representation: bin(-37) --> '-0b100101' s = s.lstrip('-0b') # remove leading zeros and minus sign return len(s) # len('100101') --> 6242 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') --> 6250. Đố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') --> 6242 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') --> 6252 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') --> 6252 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') --> 6250 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') --> 6256 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:
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') --> 6242 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') --> 6276, 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') --> 6242. 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') --> 6242 đạ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') --> 6279 đượ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') --> 6279 đượ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') --> 699: def bit_length(self): s = bin(self) # binary representation: bin(-37) --> '-0b100101' s = s.lstrip('-0b') # remove leading zeros and minus sign return len(s) # len('100101') --> 603 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') --> 698: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') --> 698: def bit_length(self): s = bin(self) # binary representation: bin(-37) --> '-0b100101' s = s.lstrip('-0b') # remove leading zeros and minus sign return len(s) # len('100101') --> 604 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') --> 6287 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') --> 6288 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') --> 6242 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') --> 605 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') --> 6242, 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') --> 6242, 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') --> 606 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') --> 607 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') --> 629 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') --> 630 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') --> 608 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 n65 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') --> 6294: def bit_length(self): s = bin(self) # binary representation: bin(-37) --> '-0b100101' s = s.lstrip('-0b') # remove leading zeros and minus sign return len(s) # len('100101') --> 609 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') --> 6242 def bit_length(self): s = bin(self) # binary representation: bin(-37) --> '-0b100101' s = s.lstrip('-0b') # remove leading zeros and minus sign return len(s) # len('100101') --> 6296.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') --> 6242 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') --> 6296. def bit_length(self): s = bin(self) # binary representation: bin(-37) --> '-0b100101' s = s.lstrip('-0b') # remove leading zeros and minus sign return len(s) # len('100101') --> 610 Các lớp học chung tiêu chuẩnCá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.
Các thuộc tính đặc biệt của |