Mã nguồn: lib/typort.py Lib/types.py Show Mô -đun này xác định các chức năng tiện ích để hỗ trợ tạo động các loại mới. Nó cũng xác định tên cho một số loại đối tượng được sử dụng bởi trình thông dịch Python tiêu chuẩn, nhưng không được phơi bày như các bản tích hợp như Cuối cùng, nó cung cấp một số lớp và chức năng tiện ích liên quan đến loại bổ sung không đủ cơ bản để được xây dựng. Sáng tạo loại độngTYPE.New_Class (Tên, BASES = (), KWDS = Không, EXEC_BODY = Không) ¶new_class(name, bases=(), kwds=None, exec_body=None)¶Tạo một đối tượng lớp động bằng cách sử dụng metaclass thích hợp. Ba đối số đầu tiên là các thành phần tạo nên tiêu đề định nghĩa lớp: tên lớp, các lớp cơ sở (theo thứ tự), các đối số từ khóa (như Đối số exec_body là một cuộc gọi lại được sử dụng để điền vào không gian tên lớp mới được tạo. Nó sẽ chấp nhận không gian tên lớp là đối số duy nhất của nó và cập nhật không gian tên trực tiếp với nội dung lớp. Nếu không có cuộc gọi lại được cung cấp, nó có tác dụng tương tự như vượt qua trong Mới trong phiên bản 3.3. TYPE.Prepare_Class (Tên, Base = (), KWDS = Không) ¶prepare_class(name, bases=(), kwds=None)¶Tính toán metaclass thích hợp và tạo không gian tên lớp. Các đối số là các thành phần tạo nên tiêu đề định nghĩa lớp: tên lớp, các lớp cơ sở (theo thứ tự) và các đối số từ khóa (như Giá trị trả về là 3-Tuple: Metaclass là metaclass thích hợp, không gian tên là không gian tên lớp được chuẩn bị và KWDS là bản sao cập nhật của đối số được thông qua trong đối số KWDS với bất kỳ mục nhập Mới trong phiên bản 3.3. TYPE.Prepare_Class (Tên, Base = (), KWDS = Không) ¶The default value for the class SimpleNamespace: def __init__(self, /, **kwargs): self.__dict__.update(kwargs) def __repr__(self): items = (f"{k}={v!r}" for k, v in self.__dict__.items()) return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): if isinstance(self, SimpleNamespace) and isinstance(other, SimpleNamespace): return self.__dict__ == other.__dict__ return NotImplemented0 method. Tính toán metaclass thích hợp và tạo không gian tên lớp. Các đối số là các thành phần tạo nên tiêu đề định nghĩa lớp: tên lớp, các lớp cơ sở (theo thứ tự) và các đối số từ khóa (nhưmetaclass ).Giá trị trả về là 3-Tuple: 'metaclass' nào bị xóa. Nếu không có đối số KWDS nào được thông qua, đây sẽ là một lệnh trống rỗng. - Metaclasses in Python 3000Thay đổi trong phiên bản 3.6: Giá trị mặc định cho phần tử class SimpleNamespace: def __init__(self, /, **kwargs): self.__dict__.update(kwargs) def __repr__(self): items = (f"{k}={v!r}" for k, v in self.__dict__.items()) return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): if isinstance(self, SimpleNamespace) and isinstance(other, SimpleNamespace): return self.__dict__ == other.__dict__ return NotImplemented0. Xem thêmresolve_bases(bases)¶ MetaclassesPEP 560. Chi tiết đầy đủ về quy trình tạo lớp được hỗ trợ bởi các chức năng này PEP 3115 - Metaclasses trong Python 3000 Tính toán metaclass thích hợp và tạo không gian tên lớp. Các đối số là các thành phần tạo nên tiêu đề định nghĩa lớp: tên lớp, các lớp cơ sở (theo thứ tự) và các đối số từ khóa (như Giá trị trả về là 3-Tuple: metaclass, namespace, kwdsMetaclass là metaclass thích hợp, không gian tên là không gian tên lớp được chuẩn bị và KWDS là bản sao cập nhật của đối số được thông qua trong đối số KWDS với bất kỳ mục nhập Thay đổi trong phiên bản 3.6: Giá trị mặc định cho phần tử class SimpleNamespace: def __init__(self, /, **kwargs): self.__dict__.update(kwargs) def __repr__(self): items = (f"{k}={v!r}" for k, v in self.__dict__.items()) return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): if isinstance(self, SimpleNamespace) and isinstance(other, SimpleNamespace): return self.__dict__ == other.__dict__ return NotImplemented0. Xem thêm Metaclasses Chi tiết đầy đủ về quy trình tạo lớp được hỗ trợ bởi các chức năng nàyNoneType¶PEP 3115 - Metaclasses trong Python 3000 Giới thiệu móc không gian tên ____1010 loại.resolve_base (cơ sở) ¶FunctionType¶ types.LambdaType¶Giải quyết các mục MRO động theo quy định của PEP 560. Hàm này tìm kiếm các mục trong các cơ sở không phải là trường hợp của class SimpleNamespace: def __init__(self, /, **kwargs): self.__dict__.update(kwargs) def __repr__(self): items = (f"{k}={v!r}" for k, v in self.__dict__.items()) return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): if isinstance(self, SimpleNamespace) and isinstance(other, SimpleNamespace): return self.__dict__ == other.__dict__ return NotImplemented2 và trả về một tuple trong đó mỗi đối tượng có phương thức class SimpleNamespace: def __init__(self, /, **kwargs): self.__dict__.update(kwargs) def __repr__(self): items = (f"{k}={v!r}" for k, v in self.__dict__.items()) return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): if isinstance(self, SimpleNamespace) and isinstance(other, SimpleNamespace): return self.__dict__ == other.__dict__ return NotImplemented3 được thay thế bằng kết quả chưa đóng gói khi gọi phương thức này. Nếu một mục cơ sở là một ví dụ là class SimpleNamespace: def __init__(self, /, **kwargs): self.__dict__.update(kwargs) def __repr__(self): items = (f"{k}={v!r}" for k, v in self.__dict__.items()) return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): if isinstance(self, SimpleNamespace) and isinstance(other, SimpleNamespace): return self.__dict__ == other.__dict__ return NotImplemented2 hoặc nó không có phương thức class SimpleNamespace: def __init__(self, /, **kwargs): self.__dict__.update(kwargs) def __repr__(self): items = (f"{k}={v!r}" for k, v in self.__dict__.items()) return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): if isinstance(self, SimpleNamespace) and isinstance(other, SimpleNamespace): return self.__dict__ == other.__dict__ return NotImplemented3, thì nó được bao gồm trong bộ tple trả về không thay đổi.auditing event int 1 with argument int 2.Mới trong phiên bản 3.7. PEP 560 - Hỗ trợ cốt lõi để gõ mô -đun và loại chungGeneratorType¶Các loại phiên dịch tiêu chuẩngenerator-iterator objects, created by generator functions. Mô -đun này cung cấp tên cho nhiều loại được yêu cầu để thực hiện trình thông dịch Python. Nó cố tình tránh bao gồm một số loại chỉ phát sinh tình cờ trong quá trình xử lý như loạiclass SimpleNamespace: def __init__(self, /, **kwargs): self.__dict__.update(kwargs) def __repr__(self): items = (f"{k}={v!r}" for k, v in self.__dict__.items()) return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): if isinstance(self, SimpleNamespace) and isinstance(other, SimpleNamespace): return self.__dict__ == other.__dict__ return NotImplemented6.CoroutineType¶ Việc sử dụng điển hình của các tên này là để kiểm tra class SimpleNamespace: def __init__(self, /, **kwargs): self.__dict__.update(kwargs) def __repr__(self): items = (f"{k}={v!r}" for k, v in self.__dict__.items()) return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): if isinstance(self, SimpleNamespace) and isinstance(other, SimpleNamespace): return self.__dict__ == other.__dict__ return NotImplemented7 hoặc class SimpleNamespace: def __init__(self, /, **kwargs): self.__dict__.update(kwargs) def __repr__(self): items = (f"{k}={v!r}" for k, v in self.__dict__.items()) return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): if isinstance(self, SimpleNamespace) and isinstance(other, SimpleNamespace): return self.__dict__ == other.__dict__ return NotImplemented8.coroutine objects, created by int 3 functions.Nếu bạn khởi tạo bất kỳ loại nào trong số này, lưu ý rằng chữ ký có thể thay đổi giữa các phiên bản Python. Tên tiêu chuẩn được xác định cho các loại sau:AsyncGeneratorType¶loại.Nonetype¶asynchronous generator-iterator objects, created by asynchronous generator functions. Loại TYPE. Loại chức năng và chức năng do người dùng xác định được tạo bởi các biểu thức Tăng một sự kiện kiểm toán loại.generatortype¶ Loại đối tượng máy phát điện, được tạo bởi các hàm của máy phát. loại.coroutInetype¶CellType¶Loại đối tượng Coroutine, được tạo bởi các hàm Loại đối tượng máy phát điện, được tạo bởi các hàm của máy phát. loại.coroutInetype¶MethodType¶Loại đối tượng Coroutine, được tạo bởi các hàm loại.asyncgeneratortype¶ typ.wrapperdescriptortype¶WrapperDescriptorType¶Loại phương pháp của một số loại dữ liệu tích hợp và các lớp cơ sở như Mới trong phiên bản 3.7. loại.methodwrapperType¶MethodWrapperType¶Loại phương thức ràng buộc của một số loại dữ liệu tích hợp và các lớp cơ sở. Ví dụ, nó là loại Mới trong phiên bản 3.7. loại.methodwrapperType¶NotImplementedType¶Loại phương thức ràng buộc của một số loại dữ liệu tích hợp và các lớp cơ sở. Ví dụ, nó là loại loại. Loạimetaclass 0.MethodDescriptorType¶Mới trong phiên bản 3.10. Mới trong phiên bản 3.7. loại.methodwrapperType¶ClassMethodDescriptorType¶Loại phương thức ràng buộc của một số loại dữ liệu tích hợp và các lớp cơ sở. Ví dụ, nó là loại Mới trong phiên bản 3.7. loại.methodwrapperType¶types.ModuleType(name, doc=None)¶Loại phương thức ràng buộc của một số loại dữ liệu tích hợp và các lớp cơ sở. Ví dụ, nó là loại Loại class SimpleNamespace: def __init__(self, /, **kwargs): self.__dict__.update(kwargs) def __repr__(self): items = (f"{k}={v!r}" for k, v in self.__dict__.items()) return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): if isinstance(self, SimpleNamespace) and isinstance(other, SimpleNamespace): return self.__dict__ == other.__dict__ return NotImplemented9. Mới trong phiên bản 3.10.¶ typloader which loaded the module. Defaults to class SimpleNamespace: def __init__(self, /, **kwargs): self.__dict__.update(kwargs) def __repr__(self): items = (f"{k}={v!r}" for k, v in self.__dict__.items()) return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): if isinstance(self, SimpleNamespace) and isinstance(other, SimpleNamespace): return self.__dict__ == other.__dict__ return NotImplemented9. Loại phương pháp của một số loại dữ liệu tích hợp như TYP Loại phương thức lớp không liên kết của một số loại dữ liệu tích hợp như classtypes.moduletype (tên, doc = none) ¶Defaults to class SimpleNamespace: def __init__(self, /, **kwargs): self.__dict__.update(kwargs) def __repr__(self): items = (f"{k}={v!r}" for k, v in self.__dict__.items()) return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): if isinstance(self, SimpleNamespace) and isinstance(other, SimpleNamespace): return self.__dict__ == other.__dict__ return NotImplemented9. Previously the attribute was optional. Loại mô -đun. Trình xây dựng lấy tên của mô -đun sẽ được tạo và tùy chọn DocString của nó.¶ __doc__¶ Các tài liệu của mô -đun. Mặc định làclass SimpleNamespace: def __init__(self, /, **kwargs): self.__dict__.update(kwargs) def __repr__(self): items = (f"{k}={v!r}" for k, v in self.__dict__.items()) return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): if isinstance(self, SimpleNamespace) and isinstance(other, SimpleNamespace): return self.__dict__ == other.__dict__ return NotImplemented9.¶ __loader__¶package a module belongs to. If the module is top-level (i.e. not a part of any specific package) then the attribute should be set to class SimpleNamespace: def __init__(self, /, **kwargs): self.__dict__.update(kwargs) def __repr__(self): items = (f"{k}={v!r}" for k, v in self.__dict__.items()) return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): if isinstance(self, SimpleNamespace) and isinstance(other, SimpleNamespace): return self.__dict__ == other.__dict__ return NotImplemented9. Bộ tải đã tải mô -đun. Mặc định là class SimpleNamespace: def __init__(self, /, **kwargs): self.__dict__.update(kwargs) def __repr__(self): items = (f"{k}={v!r}" for k, v in self.__dict__.items()) return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): if isinstance(self, SimpleNamespace) and isinstance(other, SimpleNamespace): return self.__dict__ == other.__dict__ return NotImplemented9. TYP Loại phương thức lớp không liên kết của một số loại dữ liệu tích hợp như classtypes.moduletype (tên, doc = none) ¶Defaults to class SimpleNamespace: def __init__(self, /, **kwargs): self.__dict__.update(kwargs) def __repr__(self): items = (f"{k}={v!r}" for k, v in self.__dict__.items()) return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): if isinstance(self, SimpleNamespace) and isinstance(other, SimpleNamespace): return self.__dict__ == other.__dict__ return NotImplemented9. Previously the attribute was optional. Loại mô -đun. Trình xây dựng lấy tên của mô -đun sẽ được tạo và tùy chọn DocString của nó.¶ __doc__¶ Các tài liệu của mô -đun. Mặc định là Bộ tải đã tải mô -đun. Mặc định là class SimpleNamespace: def __init__(self, /, **kwargs): self.__dict__.update(kwargs) def __repr__(self): items = (f"{k}={v!r}" for k, v in self.__dict__.items()) return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): if isinstance(self, SimpleNamespace) and isinstance(other, SimpleNamespace): return self.__dict__ == other.__dict__ return NotImplemented9. loại. Loạimetaclass 0.types.GenericAlias(t_origin,
t_args)¶Mới trong phiên bản 3.10.parameterized generics such as typ >>> from types import GenericAlias >>> list[int] == GenericAlias(list, (int,)) True >>> dict[str, int] == GenericAlias(dict, (str, int)) True Loại phương pháp của một số loại dữ liệu tích hợp như TYPThis type can now be subclassed. Loại phương thức lớp không liên kết của một số loại dữ liệu tích hợp nhưmetaclass 2.
types.UnionType¶classtypes.moduletype (tên, doc = none) ¶union type expressions. loại. Loạimetaclass 0.
types.TracebackType(tb_next, tb_frame, tb_lasti, tb_lineno)¶Mới trong phiên bản 3.10. typthe language reference for details of the available attributes and operations, and guidance on creating tracebacks dynamically. Loại phương pháp của một số loại dữ liệu tích hợp nhưmetaclass 1.FrameType¶TYP Loại phương thức lớp không liên kết của một số loại dữ liệu tích hợp như Loại mô -đun. Trình xây dựng lấy tên của mô -đun sẽ được tạo và tùy chọn DocString của nó. __doc__¶MemberDescriptorType¶Các tài liệu của mô -đun. Mặc định là class SimpleNamespace: def __init__(self, /, **kwargs): self.__dict__.update(kwargs) def __repr__(self): items = (f"{k}={v!r}" for k, v in self.__dict__.items()) return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): if isinstance(self, SimpleNamespace) and isinstance(other, SimpleNamespace): return self.__dict__ == other.__dict__ return NotImplemented9. __loader__¶ In other implementations of Python, this type may be identical to class SimpleNamespace: def __init__(self, /, **kwargs): self.__dict__.update(kwargs) def __repr__(self): items = (f"{k}={v!r}" for k, v in self.__dict__.items()) return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): if isinstance(self, SimpleNamespace) and isinstance(other, SimpleNamespace): return self.__dict__ == other.__dict__ return NotImplemented9.types.MappingProxyType(mapping)¶ Thuộc tính này phù hợp với Ghi chú Một phiên bản tương lai của Python có thể ngừng đặt thuộc tính này theo mặc định. Để bảo vệ chống lại sự thay đổi tiềm năng này, tốt nhất là đọc từ thuộc tính class SimpleNamespace: def __init__(self, /, **kwargs): self.__dict__.update(kwargs) def __repr__(self): items = (f"{k}={v!r}" for k, v in self.__dict__.items()) return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): if isinstance(self, SimpleNamespace) and isinstance(other, SimpleNamespace): return self.__dict__ == other.__dict__ return NotImplemented9. Trước đây thuộc tính là tùy chọn. in proxy __Tên__¶ proxy[key]Tên của mô -đun. Dự kiến sẽ phù hợp với __bưu kiện__¶ len(proxy)Gói nào một mô -đun thuộc về. Nếu mô-đun là cấp cao nhất (nghĩa là không phải là một phần của bất kỳ gói cụ thể nào) thì thuộc tính phải được đặt thành class SimpleNamespace: def __init__(self, /, **kwargs): self.__dict__.update(kwargs) def __repr__(self): items = (f"{k}={v!r}" for k, v in self.__dict__.items()) return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): if isinstance(self, SimpleNamespace) and isinstance(other, SimpleNamespace): return self.__dict__ == other.__dict__ return NotImplemented9. sao chép () ¶()¶ Trả lại một bản sao nông của ánh xạ cơ bản. Nhận (khóa [, mặc định]) ¶(key[, default])¶Trả về giá trị cho khóa nếu có trong ánh xạ cơ bản, mặc định khác. Nếu mặc định không được đưa ra, nó mặc định là class SimpleNamespace: def __init__(self, /, **kwargs): self.__dict__.update(kwargs) def __repr__(self): items = (f"{k}={v!r}" for k, v in self.__dict__.items()) return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): if isinstance(self, SimpleNamespace) and isinstance(other, SimpleNamespace): return self.__dict__ == other.__dict__ return NotImplemented9, do đó phương thức này không bao giờ tăng 'metaclass' 3. mặt hàng()¶()¶Trả về một cái nhìn mới về các mục ánh xạ cơ bản (các cặp Trả về một cái nhìn mới về các phím ánh xạ cơ bản. giá trị ()()¶Trả về một cái nhìn mới về các giá trị ánh xạ cơ bản. reversed(proxy)Trả về một trình lặp ngược qua các khóa của ánh xạ cơ bản. Mới trong phiên bản 3.9. Các lớp và chức năng tiện ích bổ sungclasstypes.simplenamespace¶ types.SimpleNamespace¶Một lớp con Không giống như Loại gần tương đương với mã sau: class SimpleNamespace: def __init__(self, /, **kwargs): self.__dict__.update(kwargs) def __repr__(self): items = (f"{k}={v!r}" for k, v in self.__dict__.items()) return "{}({})".format(type(self).__name__, ", ".join(items)) def __eq__(self, other): if isinstance(self, SimpleNamespace) and isinstance(other, SimpleNamespace): return self.__dict__ == other.__dict__ return NotImplemented
Mới trong phiên bản 3.3. Đã thay đổi trong phiên bản 3.9: thứ tự thuộc tính trong repr đã thay đổi từ bảng chữ cái sang chèn (như Truy cập thuộc tính tuyến trên một lớp đến __getAttr__. Đây là một mô tả, được sử dụng để xác định các thuộc tính hoạt động khác nhau khi được truy cập thông qua một thể hiện và thông qua một lớp. Truy cập ví dụ vẫn bình thường, nhưng quyền truy cập vào một thuộc tính thông qua một lớp sẽ được chuyển đến phương thức lớp __getAttr__; Điều này được thực hiện bằng cách nâng cao thuộc tính. Điều này cho phép người ta có các thuộc tính hoạt động trên một thể hiện và có các thuộc tính ảo trên lớp có cùng tên (xem Mới trong phiên bản 3.4. Chức năng tiện ích Coroutineloại.coroutine (gen_func) ¶coroutine(gen_func)¶Hàm này biến đổi hàm máy phát thành hàm coroutine trả về coroutine dựa trên máy phát. Coroutine dựa trên máy phát vẫn là một trình lặp máy phát điện, nhưng cũng được coi là một đối tượng coroutine và có thể chờ đợi. Tuy nhiên, nó có thể không nhất thiết phải thực hiện phương pháp Nếu Gen_func là một hàm máy phát, nó sẽ được sửa đổi tại chỗ. Nếu gen_func không phải là hàm máy phát, nó sẽ được bọc. Nếu nó trả về một thể hiện là Mới trong phiên bản 3.5. 4 được xây dựng là gìPython cũng cung cấp một số loại dữ liệu tích hợp, đặc biệt là Dict, Danh sách, Bộ và Frozenset và Tuple.dict , list , set and frozenset , and tuple .
5 loại dữ liệu trong Python là gì?Python có sáu loại dữ liệu tiêu chuẩn: số.Sợi dây.Danh sách.Tuple. |