Các đối tượng là sự trừu tượng hóa dữ liệu của Python. Tất cả dữ liệu trong chương trình Python được biểu diễn bằng đối tượng hoặc bằng mối quan hệ giữa các đối tượng. [Theo một nghĩa nào đó, và phù hợp với mô hình "máy tính chương trình được lưu trữ" của Von Neumann, mã cũng được biểu diễn bằng các đối tượng. ]
Mỗi đối tượng có một danh tính, một loại và một giá trị. Danh tính của một đối tượng không bao giờ thay đổi khi nó đã được tạo; . Toán tử '
import sys from types import ModuleType class VerboseModule[ModuleType]: def __repr__[self]: return f'Verbose {self.__name__}' def __setattr__[self, attr, value]: print[f'Setting {attr}...'] super[].__setattr__[attr, value] sys.modules[__name__].__class__ = VerboseModule7' so sánh danh tính của hai đối tượng;
Chi tiết triển khai CPython. Đối với CPython,
import sys from types import ModuleType class VerboseModule[ModuleType]: def __repr__[self]: return f'Verbose {self.__name__}' def __setattr__[self, attr, value]: print[f'Setting {attr}...'] super[].__setattr__[attr, value] sys.modules[__name__].__class__ = VerboseModule9 là địa chỉ bộ nhớ lưu trữ
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass0
Loại của một đối tượng xác định các hoạt động mà đối tượng hỗ trợ [e. g. , "nó có độ dài không?"] và cũng xác định các giá trị có thể có cho các đối tượng thuộc loại đó. Hàm
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass1 trả về kiểu của một đối tượng [chính nó là một đối tượng]. Giống như danh tính của nó, loại của một đối tượng cũng không thể thay đổi. 1
Giá trị của một số đối tượng có thể thay đổi. Objects whose value can change are said to be mutable; objects whose value is unchangeable once they are created are called immutable. [The value of an immutable container object that contains a reference to a mutable object can change when the latter’s value is changed; however the container is still considered immutable, because the collection of objects it contains cannot be changed. So, immutability is not strictly the same as having an unchangeable value, it is more subtle. ] An object’s mutability is determined by its type; for instance, numbers, strings and tuples are immutable, while dictionaries and lists are mutable
Objects are never explicitly destroyed; however, when they become unreachable they may be garbage-collected. An implementation is allowed to postpone garbage collection or omit it altogether — it is a matter of implementation quality how garbage collection is implemented, as long as no objects are collected that are still reachable
CPython implementation detail. CPython currently uses a reference-counting scheme with [optional] delayed detection of cyclically linked garbage, which collects most objects as soon as they become unreachable, but is not guaranteed to collect garbage containing circular references. See the documentation of the
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass2 module for information on controlling the collection of cyclic garbage. Other implementations act differently and CPython may change. Do not depend on immediate finalization of objects when they become unreachable [so you should always close files explicitly]
Note that the use of the implementation’s tracing or debugging facilities may keep objects alive that would normally be collectable. Also note that catching an exception with a ‘
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass3…
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass4’ statement may keep objects alive
Some objects contain references to “external” resources such as open files or windows. It is understood that these resources are freed when the object is garbage-collected, but since garbage collection is not guaranteed to happen, such objects also provide an explicit way to release the external resource, usually a
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass5 method. Programs are strongly recommended to explicitly close such objects. The ‘
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass3…
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass7’ statement and the ‘
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass8’ statement provide convenient ways to do this
Some objects contain references to other objects; these are called containers. Examples of containers are tuples, lists and dictionaries. The references are part of a container’s value. In most cases, when we talk about the value of a container, we imply the values, not the identities of the contained objects; however, when we talk about the mutability of a container, only the identities of the immediately contained objects are implied. So, if an immutable container [like a tuple] contains a reference to a mutable object, its value changes if that mutable object is changed
Types affect almost all aspects of object behavior. Even the importance of object identity is affected in some sense. for immutable types, operations that compute new values may actually return a reference to any existing object with the same type and value, while for mutable objects this is not allowed. e. g. , after
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass9,
import sys from types import ModuleType class VerboseModule[ModuleType]: def __repr__[self]: return f'Verbose {self.__name__}' def __setattr__[self, attr, value]: print[f'Setting {attr}...'] super[].__setattr__[attr, value] sys.modules[__name__].__class__ = VerboseModule30 and
import sys from types import ModuleType class VerboseModule[ModuleType]: def __repr__[self]: return f'Verbose {self.__name__}' def __setattr__[self, attr, value]: print[f'Setting {attr}...'] super[].__setattr__[attr, value] sys.modules[__name__].__class__ = VerboseModule31 may or may not refer to the same object with the value one, depending on the implementation, but after
import sys from types import ModuleType class VerboseModule[ModuleType]: def __repr__[self]: return f'Verbose {self.__name__}' def __setattr__[self, attr, value]: print[f'Setting {attr}...'] super[].__setattr__[attr, value] sys.modules[__name__].__class__ = VerboseModule32,
import sys from types import ModuleType class VerboseModule[ModuleType]: def __repr__[self]: return f'Verbose {self.__name__}' def __setattr__[self, attr, value]: print[f'Setting {attr}...'] super[].__setattr__[attr, value] sys.modules[__name__].__class__ = VerboseModule33 and
import sys from types import ModuleType class VerboseModule[ModuleType]: def __repr__[self]: return f'Verbose {self.__name__}' def __setattr__[self, attr, value]: print[f'Setting {attr}...'] super[].__setattr__[attr, value] sys.modules[__name__].__class__ = VerboseModule34 are guaranteed to refer to two different, unique, newly created empty lists. [Note that
import sys from types import ModuleType class VerboseModule[ModuleType]: def __repr__[self]: return f'Verbose {self.__name__}' def __setattr__[self, attr, value]: print[f'Setting {attr}...'] super[].__setattr__[attr, value] sys.modules[__name__].__class__ = VerboseModule35 assigns the same object to both
import sys from types import ModuleType class VerboseModule[ModuleType]: def __repr__[self]: return f'Verbose {self.__name__}' def __setattr__[self, attr, value]: print[f'Setting {attr}...'] super[].__setattr__[attr, value] sys.modules[__name__].__class__ = VerboseModule33 and
import sys from types import ModuleType class VerboseModule[ModuleType]: def __repr__[self]: return f'Verbose {self.__name__}' def __setattr__[self, attr, value]: print[f'Setting {attr}...'] super[].__setattr__[attr, value] sys.modules[__name__].__class__ = VerboseModule34. ]
3. 2. The standard type hierarchy¶
Below is a list of the types that are built into Python. Extension modules [written in C, Java, or other languages, depending on the implementation] can define additional types. Future versions of Python may add types to the type hierarchy [e. g. , rational numbers, efficiently stored arrays of integers, etc. ], although such additions will often be provided via the standard library instead
Some of the type descriptions below contain a paragraph listing ‘special attributes. ’ These are attributes that provide access to the implementation and are not intended for general use. Their definition may change in the future
NoneThis type has a single value. There is a single object with this value. This object is accessed through the built-in name
import sys from types import ModuleType class VerboseModule[ModuleType]: def __repr__[self]: return f'Verbose {self.__name__}' def __setattr__[self, attr, value]: print[f'Setting {attr}...'] super[].__setattr__[attr, value] sys.modules[__name__].__class__ = VerboseModule38. It is used to signify the absence of a value in many situations, e. g. , it is returned from functions that don’t explicitly return anything. Its truth value is falseNotImplemented
This type has a single value. There is a single object with this value. This object is accessed through the built-in name
import sys from types import ModuleType class VerboseModule[ModuleType]: def __repr__[self]: return f'Verbose {self.__name__}' def __setattr__[self, attr, value]: print[f'Setting {attr}...'] super[].__setattr__[attr, value] sys.modules[__name__].__class__ = VerboseModule39. Numeric methods and rich comparison methods should return this value if they do not implement the operation for the operands provided. [The interpreter will then try the reflected operation, or some other fallback, depending on the operator. ] It should not be evaluated in a boolean context
See Implementing the arithmetic operations for more details.
Changed in version 3. 9. Evaluating
import sys from types import ModuleType class VerboseModule[ModuleType]: def __repr__[self]: return f'Verbose {self.__name__}' def __setattr__[self, attr, value]: print[f'Setting {attr}...'] super[].__setattr__[attr, value] sys.modules[__name__].__class__ = VerboseModule39 in a boolean context is deprecated. While it currently evaluates as true, it will emit a
import sys from types import ModuleType class VerboseModule[ModuleType]: def __repr__[self]: return f'Verbose {self.__name__}' def __setattr__[self, attr, value]: print[f'Setting {attr}...'] super[].__setattr__[attr, value] sys.modules[__name__].__class__ = VerboseModule41. It will raise a
import sys from types import ModuleType class VerboseModule[ModuleType]: def __repr__[self]: return f'Verbose {self.__name__}' def __setattr__[self, attr, value]: print[f'Setting {attr}...'] super[].__setattr__[attr, value] sys.modules[__name__].__class__ = VerboseModule42 in a future version of Python. Ellipsis
This type has a single value. There is a single object with this value. This object is accessed through the literal
import sys from types import ModuleType class VerboseModule[ModuleType]: def __repr__[self]: return f'Verbose {self.__name__}' def __setattr__[self, attr, value]: print[f'Setting {attr}...'] super[].__setattr__[attr, value] sys.modules[__name__].__class__ = VerboseModule43 or the built-in name
import sys from types import ModuleType class VerboseModule[ModuleType]: def __repr__[self]: return f'Verbose {self.__name__}' def __setattr__[self, attr, value]: print[f'Setting {attr}...'] super[].__setattr__[attr, value] sys.modules[__name__].__class__ = VerboseModule44. Giá trị thật của nó là true
import sys from types import ModuleType class VerboseModule[ModuleType]: def __repr__[self]: return f'Verbose {self.__name__}' def __setattr__[self, attr, value]: print[f'Setting {attr}...'] super[].__setattr__[attr, value] sys.modules[__name__].__class__ = VerboseModule45
Chúng được tạo bởi các chữ số và được trả về dưới dạng kết quả bởi các toán tử số học và các hàm tích hợp số học. Các đối tượng số là bất biến; . Tất nhiên, số Python có liên quan chặt chẽ đến số toán học, nhưng chịu các hạn chế của biểu diễn số trong máy tính
Biểu diễn chuỗi của các lớp số, được tính bởi
import sys from types import ModuleType class VerboseModule[ModuleType]: def __repr__[self]: return f'Verbose {self.__name__}' def __setattr__[self, attr, value]: print[f'Setting {attr}...'] super[].__setattr__[attr, value] sys.modules[__name__].__class__ = VerboseModule46 và
import sys from types import ModuleType class VerboseModule[ModuleType]: def __repr__[self]: return f'Verbose {self.__name__}' def __setattr__[self, attr, value]: print[f'Setting {attr}...'] super[].__setattr__[attr, value] sys.modules[__name__].__class__ = VerboseModule47, có các thuộc tính sau
Chúng là các chữ số hợp lệ, khi được chuyển đến hàm tạo của lớp chúng, sẽ tạo ra một đối tượng có giá trị của số ban đầu
Biểu diễn ở cơ sở 10, khi có thể
Các số 0 ở đầu, có thể ngoại trừ một số 0 trước dấu thập phân, không được hiển thị
Các số 0 ở cuối, có thể ngoại trừ một số 0 sau dấu thập phân, không được hiển thị
Một dấu hiệu chỉ được hiển thị khi số âm
Python phân biệt giữa số nguyên, số dấu phẩy động và số phức
import sys from types import ModuleType class VerboseModule[ModuleType]: def __repr__[self]: return f'Verbose {self.__name__}' def __setattr__[self, attr, value]: print[f'Setting {attr}...'] super[].__setattr__[attr, value] sys.modules[__name__].__class__ = VerboseModule48
Chúng đại diện cho các phần tử từ tập hợp các số nguyên [dương và âm]
Có hai loại số nguyên
Toàn bộ [______249]Chúng đại diện cho các số trong một phạm vi không giới hạn, chỉ tùy thuộc vào bộ nhớ [ảo] có sẵn. Với mục đích của các phép toán dịch chuyển và mặt nạ, một biểu diễn nhị phân được giả định và các số âm được biểu diễn dưới dạng một biến thể của phần bù 2, điều này tạo ảo giác về một chuỗi vô hạn các bit dấu kéo dài sang trái
Boolean [class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass30]
Chúng đại diện cho các giá trị thật Sai và Đúng. Hai đối tượng đại diện cho các giá trị ______331 và
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass32 là các đối tượng Boolean duy nhất. Kiểu Boolean là một kiểu con của kiểu số nguyên và các giá trị Boolean hoạt động giống như các giá trị 0 và 1 tương ứng trong hầu hết các ngữ cảnh, ngoại trừ khi được chuyển đổi thành một chuỗi, các chuỗi
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass33 hoặc
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass34 được trả về , tương ứng
Các quy tắc biểu diễn số nguyên nhằm đưa ra cách giải thích có ý nghĩa nhất về phép dịch chuyển và mặt nạ liên quan đến số nguyên âm
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass35 [
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass36]
Chúng đại diện cho các số dấu phẩy động chính xác kép ở cấp độ máy. Bạn phụ thuộc vào kiến trúc máy bên dưới [và triển khai C hoặc Java] cho phạm vi được chấp nhận và xử lý tràn. Python không hỗ trợ các số dấu phẩy động có độ chính xác đơn;
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass37 [
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass38]
Chúng biểu thị các số phức dưới dạng một cặp số dấu phẩy động chính xác kép ở cấp độ máy. Các cảnh báo tương tự áp dụng cho các số dấu phẩy động. Phần thực và phần ảo của một số phức
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass39 có thể được truy xuất thông qua các thuộc tính chỉ đọc
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass30 và
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass31trình tự
Chúng đại diện cho các tập hợp được sắp xếp hữu hạn được lập chỉ mục bởi các số không âm. Hàm tích hợp
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass32 trả về số lượng phần tử của một chuỗi. Khi độ dài của một dãy là n, bộ chỉ số chứa các số 0, 1, …, n-1. Mục i của dãy a được chọn bởi
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass33
Trình tự cũng hỗ trợ cắt.
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass34 chọn tất cả các mục có chỉ số k sao cho i
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass35 k
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass36 j. Khi được sử dụng như một biểu thức, một lát cắt là một chuỗi cùng loại. Điều này ngụ ý rằng bộ chỉ mục được đánh số lại để nó bắt đầu từ 0
Một số trình tự cũng hỗ trợ "cắt lát mở rộng" với thông số "bước" thứ ba.
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass37 chọn tất cả các mục của a có chỉ số x trong đó
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass38, n
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass39
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass30 và i
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass35 x
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass36 j
Các trình tự được phân biệt theo khả năng biến đổi của chúng
Trình tự bất biếnMột đối tượng thuộc loại chuỗi bất biến không thể thay đổi sau khi được tạo. [Nếu đối tượng chứa các tham chiếu đến các đối tượng khác, các đối tượng khác này có thể thay đổi và có thể thay đổi; tuy nhiên, tập hợp các đối tượng được tham chiếu trực tiếp bởi một đối tượng không thể thay đổi không thể thay đổi. ]
Các loại sau đây là trình tự bất biến
DâyChuỗi là một dãy các giá trị đại diện cho các điểm mã Unicode. Tất cả các điểm mã trong phạm vi
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass33 có thể được biểu diễn trong một chuỗi. Python không có loại char ; . Hàm tích hợp sẵn
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass35 chuyển đổi một điểm mã từ dạng chuỗi của nó thành một số nguyên trong phạm vi
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass36; . Có thể sử dụng
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass30 để chuyển đổi
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass31 thành
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass32 bằng cách sử dụng mã hóa văn bản đã cho và có thể sử dụng
class Philosopher: def __init_subclass__[cls, /, default_name, **kwargs]: super[].__init_subclass__[**kwargs] cls.default_name = default_name class AustralianPhilosopher[Philosopher, default_name="Bruce"]: pass33 để đạt được điều ngược lại. Tuples
Các mục của một tuple là các đối tượng Python tùy ý. Các bộ gồm hai hoặc nhiều mục được tạo thành bởi các danh sách biểu thức được phân tách bằng dấu phẩy. Một bộ của một mục [một 'singleton'] có thể được tạo bằng cách thêm dấu phẩy vào một biểu thức [bản thân một biểu thức không tạo ra một bộ, vì dấu ngoặc đơn phải có thể sử dụng được để nhóm các biểu thức]. Một bộ trống có thể được tạo bởi một cặp dấu ngoặc đơn rỗng
BytesMột đối tượng bytes là một mảng bất biến. The items are 8-bit bytes, represented by integers in the range 0