Mã nguồn: lib/typort.py Lib/types.py
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ư int
hoặc str
.
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 động
________ 4 ________ 5 [Tên, Base = [], kwds = none, exec_body = none] ¶[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ư metaclass
].
Đố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 lambda ns: None
.
Mới trong phiên bản 3.3.
________ 4 ________ 9 [Tên, Base = [], KWDS = Không] ¶[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ư metaclass
].
Giá trị trả về là 3-Tuple:
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 NotImplemented1
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
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 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.
Mới trong phiên bản 3.3.
________ 4 ________ 9 [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 NotImplemented3 element of the returned tuple has changed. Now an insertion-order-preserving mapping is used when the metaclass does not have 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 NotImplemented4 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:
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 NotImplemented1Metaclass 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
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 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 3000
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 NotImplemented3 của bộ được trả về đã thay đổi. Bây giờ một ánh xạ bảo tồn theo thứ tự chèn được sử dụng khi Metaclass không có phương pháp
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 NotImplemented4.Xem thêm[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ư metaclass
]. - Core support for typing module and generic types
Giá trị trả về là 3-Tuple: 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
1
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
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 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.
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 NotImplemented3 của bộ được trả về đã thay đổi. Bây giờ một ánh xạ bảo tồn theo thứ tự chèn được sử dụng khi Metaclass không có phương pháp
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 NotImplemented4.
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àyPEP 3115 - Metaclasses trong Python 3000
Giới thiệu móc không gian tên 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
4
Giải quyết các mục MRO động theo quy định của PEP 560.
Chức năng 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 NotImplemented8 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 NotImplemented9 được thay thế bằng kết quả không đó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 NotImplemented8, 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 NotImplemented9, thì nó được bao gồm trong phần trả lại không thay đổi.auditing event
str
3 with
argument str
4.Mới trong phiên bản 3.7.
PEP 560 - Hỗ trợ cốt lõi để gõ mô -đun và loại chungCá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ạiint
2.Việc sử dụng điển hình của các tên này là để kiểm tra int
3 hoặc int
4.coroutine objects, created by str
9 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:________ 4 ________ 26¶asynchronous generator-iterator objects, created by asynchronous generator functions.
Loại int
7.
________ 4 ________ 29¶ ________ 4 ________ 31¶
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 str
2.auditing event types.
5 with arguments str
4, types.
7, types.
8, types.
9, new_class
0, new_class
1, new_class
2, new_class
3, new_class
4.
Tăng một sự kiện kiểm toán str
3 với đối số str
4.
________ 4 ________ 36¶
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.
________ 4 ________ 38¶Loại đối tượng Coroutine, được tạo bởi các hàm str
9.
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.
________ 4 ________ 38¶Loại đối tượng Coroutine, được tạo bởi các hàm str
9.
________ 4 ________ 41¶
________ 4 ________ 67¶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ư metaclass
8 hoặc metaclass
9.
Mới trong phiên bản 3.7.
________ 4 ________ 71¶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 lambda ns: None
2.
Mới trong phiên bản 3.7.
________ 4 ________ 71¶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 lambda ns: None
2.
________ 4 ________ 74¶
Loạilambda ns: None
5.Mới trong phiên bản 3.10.
Mới trong phiên bản 3.7.
________ 4 ________ 71¶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 lambda ns: None
2.
Mới trong phiên bản 3.7.
________ 4 ________ 71¶[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 lambda ns: None
2.modules. The constructor takes the name of the module to be created and optionally its
docstring.
Loại lambda ns: None
5.docstring of the module. Defaults to int
7.
________ 4 ________ 77¶loader which loaded the module. Defaults to int
7.
Loại phương pháp của một số loại dữ liệu tích hợp như lambda ns: None
8.
________ 4 ________ 80¶
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ư types.
1.
Lớp ________ 4 ________ 83 [Tên, Doc = none] ¶Defaults to int
7. Previously the attribute was optional.
________ 84¶
Các tài liệu của mô -đun. Mặc định làint
7. ________ 86¶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 prepare_class
6, else it should be set to the name of the package [which can be
prepare_class
7 if the module is a package itself]. Defaults to int
7.
Bộ tải đã tải mô -đun. Mặc định là int
7.
________ 4 ________ 80¶
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ư types.
1.
Lớp ________ 4 ________ 83 [Tên, Doc = none] ¶Defaults to int
7. Previously the attribute was optional.
________ 84¶
Các tài liệu của mô -đun. Mặc định là int
7.
Bộ tải đã tải mô -đun. Mặc định là int
7.
________ 4 ________ 74¶
Loạilambda ns: None
5.[t_origin, t_args]¶Mới trong phiên bản 3.10.parameterized generics such as
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 NotImplemented11.
________ 4 ________ 77¶
>>> 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ư lambda ns: None
8.
________ 4 ________ 80¶This 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ưtypes.
1.Lớp ________ 4 ________ 83 [Tên, Doc = none] ¶union type expressions.
________ 4 ________ 74¶
Loạilambda ns: None
5.[tb_next, tb_frame, tb_lasti, tb_lineno]¶Mới trong phiên bản 3.10.
________ 4 ________ 77¶the 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ưlambda ns: None
8.________ 4 ________ 80¶
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ư types.
1.the language reference for details of the available attributes and operations.
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ó.
________ 84¶Các tài liệu của mô -đun. Mặc định là int
7.
________ 86¶ 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 NotImplemented39.
int
7.[mapping]¶Thuộc tính này phù hợp với types.
8 như được lưu trữ trong đối tượng types.
9.
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 types.
9 thay thế hoặc sử dụng prepare_class
1 nếu bạn cần sử dụng thuộc tính này một cách rõ ràng.Updated to support the new union [
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 NotImplemented42] operator from PEP 584, which simply delegates to the underlying mapping.Đã thay đổi trong phiên bản 3.4: Mặc định thành
int
7. Trước đây thuộc tính là tùy chọn.________ 93¶
Tên của mô -đun. Dự kiến sẽ phù hợp vớiprepare_class
4.________ 95¶
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ànhprepare_class
6, thì nó sẽ được đặt thành tên của gói [có thể là prepare_class
7 nếu chính mô-đun là chính gói] . Mặc định là int
7.Thuộc tính này phù hợp với prepare_class
9 như được lưu trữ trong đối tượng types.
9.
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 NotImplemented50
Trả lại số lượng các mục trong ánh xạ cơ bản.
________ 151 [][]¶Trả lại một bản sao nông của ánh xạ cơ bản.
________ 152 [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à int
7, do đó phương thức này không bao giờ tăng
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 NotImplemented47. ________ 155 [][]¶
Trả về một cái nhìn mới về các mục ánh xạ cơ bản [
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 NotImplemented56 cặp]. ________ 157 [][]¶
Trả về một cái nhìn mới về các phím ánh xạ cơ bản.
________ 158 [][]¶Trả về một cái nhìn mới về các giá trị ánh xạ cơ bản.
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 NotImplemented59
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ổ sung
Lớp ________ 4 ________ 161¶Một lớp con
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 NotImplemented62 đơn giản cung cấp quyền truy cập thuộc tính cho không gian tên của nó, cũng như một repred có ý nghĩa.
Không giống 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 NotImplemented62, vớ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 NotImplemented64 bạn có thể thêm và xóa các thuộc tính. Nếu một đối tượng
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 NotImplemented64 được khởi tạo bằng các đối số từ khóa, chúng được thêm trực tiếp vào không gian tên bên dưới.
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
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 NotImplemented64 có thể hữu ích để thay thế cho
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 NotImplemented67. Tuy nhiên, đối với một loại hồ sơ có cấu trúc sử dụng
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 NotImplemented68 thay thế.
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ư 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
15].Attribute order in the repr changed from alphabetical to insertion [like
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 NotImplemented15]. ________ 4 ________ 171 [fget = none, fset = none, fdel = none, doc = none] ¶[fget=None, fset=None, fdel=None, doc=None]¶
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
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 NotImplemented72 để biết ví dụ].
Mới trong phiên bản 3.4.
Chức năng tiện ích Coroutine
________ 4 ________ 174 [Gen_func] ¶[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
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 NotImplemented75.generator function into a coroutine function which returns a generator-based coroutine. The generator-based coroutine is still a generator iterator, but is also considered to be a coroutine object and is awaitable. However, it may not necessarily implement 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 NotImplemented75 method.
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à
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 NotImplemented76, trường hợp sẽ được bọc trong một đối tượng proxy có thể chờ đợi. Tất cả các loại đối tượng khác sẽ được trả lại như là.
Mới trong phiên bản 3.5.