Tôi gặp khó khăn khi tìm danh sách tương đương tốt nhất trong Python và thành ngữ Pythonic tạo danh sách trống rồi thêm từng phần tử một với. nối thêm []
Ví dụ: làm cách nào để thực hiện việc này đối với tập hợp các cặp được sắp xếp theo thứ tự [Int64, Int64]?
julia> v = Vector{[Int64, Int64]}[]
LỖI. loại không thể được xây dựng
Sau đó, tôi muốn làm một cái gì đó như
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 hiển thị dưới dạng nội trang 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 NotImplemented3 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 NotImplemented4 là
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 kiểu bổ sung không đủ cơ bản để trở thành nội trang
Tạo kiểu động¶
các loại. new_class[tên , cơ sở=[], kwds=None, exec_body=None]¶Tạo một đối tượng lớp động bằng cách sử dụng siêu dữ liệu 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 [chẳng hạ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 NotImplemented5]
Đố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ó phải chấp nhận không gian tên lớp làm đố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 nào được cung cấp, nó có tác dụng tương tự như chuyển vào
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 NotImplemented6
Mới trong phiên bản 3. 3
các loại. prepare_class[tên , cơ sở=[], kwds=None]¶Tính toán siêu dữ liệu 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 [chẳng hạ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 NotImplemented5]
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 NotImplemented8
siêu dữ liệu là siêu dữ liệu thích hợp, không gian tên là không gian tên lớp đã chuẩn bị và kwds là bản sao cập nhật của đối số kwds được truyền 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 NotImplemented9 nào đã bị xóa. Nếu không có đối số kwds nào được truyền vào, đây sẽ là một lệnh trống
Mới trong phiên bản 3. 3
Đã 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 của bộ được trả về đã thay đổi. Giờ đây, ánh xạ bảo toàn thứ tự chèn được sử dụng khi siêu dữ liệu 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 NotImplemented1.
Xem thêm
siêu dữ liệuChi 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 - Siêu dữ liệu trong Python 3000Giớ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 NotImplemented1các loại. resolve_bases[base] ¶
Giải quyết các mục nhập MRO một cách linh hoạt như được chỉ định bởi PEP 560
Hàm này tìm kiếm các mục trong các cơ sở không phải là phiên bản 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 NotImplemented3 và trả về một bộ 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 NotImplemented4 được thay thế bằng kết quả được giải nén khi gọi phương thức này. Nếu một mục cơ sở là một thể hiện 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 NotImplemented3 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 NotImplemented4, thì nó được bao gồm trong bộ dữ liệu trả về không thay đổi
Mới trong phiên bản 3. 7
Xem thêm
PEP 560 - Hỗ trợ cốt lõi để nhập mô-đun và các loại chung
Các loại phiên dịch tiêu chuẩn¶
Mô-đun này cung cấp tên cho nhiều loại được yêu cầu để triển khai 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 ngẫu nhiên trong quá trình xử lý, chẳng hạn như 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 NotImplemented7
Việc sử dụng điển hình 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 NotImplemented8 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 NotImplemented9
Nếu bạn khởi tạo bất kỳ loại nào trong số này, hãy lưu ý rằng chữ ký có thể khác nhau 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
các loại. NoneType ¶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 NotImplemented00
Mới trong phiên bản 3. 10
các loại. Loại chức năng ¶ loại. LambdaType ¶Loại hàm do người dùng định nghĩa và hàm được tạo bởi biểu 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 NotImplemented01
Tăng sự kiện 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 NotImplemented02 với đối số
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 NotImplemented03.
Sự kiện kiểm tra chỉ xảy ra để khởi tạo trực tiếp các đối tượng chức năng và không được đưa ra để biên dịch bình thường
các loại. Loại máy phát điện ¶Loại đối tượng trình tạo -iterator, được tạo bởi các hàm trình tạo.
các loại. Loại Coroutine ¶Loại đối tượng coroutine , được tạo bởi hàm
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 NotImplemented04.
Mới trong phiên bản 3. 5
các loại. AsyncGeneratorType ¶Loại đối tượng trình tạo không đồng bộ , được tạo bởi các hàm trình tạo không đồng bộ.
Mới trong phiên bản 3. 6
loại loại. Loại mã[**kwargs]¶Loại dành cho các đối tượng mã, chẳng hạn như được trả về bở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 NotImplemented05
Tăng sự kiện 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 NotImplemented06 với các đối số
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 NotImplemented03,
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 NotImplemented08,
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 NotImplemented09,
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 NotImplemented30,
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 NotImplemented31,
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 NotImplemented32,
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 NotImplemented33,
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 NotImplemented34,
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 NotImplemented35.
Lưu ý rằng các đối số được kiểm tra có thể không khớp với tên hoặc vị trí mà trình khởi tạo yêu cầu. Sự kiện kiểm tra chỉ xảy ra để khởi tạo trực tiếp các đối tượng mã và không được đưa ra để biên dịch bình thường
thay thế[**kwargs]¶Trả về một bản sao của đối tượng mã với các giá trị mới cho các trường đã chỉ định
Mới trong phiên bản 3. 8
các loại. Loại ô ¶Loại cho các đối tượng ô. các đối tượng như vậy được sử dụng làm nơi chứa các biến tự do của hàm
Mới trong phiên bản 3. 8
các loại. Loại phương thức ¶Loại phương thức của các thể hiện lớp do người dùng định nghĩa
các loại. Loại hàm tích hợp ¶ loại. BuiltinMethodType ¶Loại hàm dựng sẵ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 NotImplemented36 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 NotImplemented37 và các phương thức của lớp dựng sẵn. [Ở đây, thuật ngữ “tích hợp sẵn” có nghĩa là “được viết bằng C”. ]các loại. WrapperDescriptorType ¶
Loại phương thức của một số kiểu dữ liệu và lớp cơ sở dựng sẵ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 NotImplemented38 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 NotImplemented39
Mới trong phiên bản 3. 7
các loại. MethodWrapperType ¶Loại phương thức ràng buộc của một số kiểu dữ liệu và lớp cơ sở dựng sẵn. Ví dụ nó là 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 NotImplemented40
Mới trong phiên bản 3. 7
các loại. NotImplementedType ¶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 NotImplemented41
Mới trong phiên bản 3. 10
các loại. MethodDescriptorType ¶Loại phương thức của một số kiểu dữ liệu dựng sẵ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 NotImplemented42
Mới trong phiên bản 3. 7
các loại. ClassMethodDescriptorType ¶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ư
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 NotImplemented43
Mới trong phiên bản 3. 7
loại loại. Loại mô-đun[tên , doc=None]¶Loại mô-đun . Hàm tạo lấy tên của mô-đun sẽ được tạo và tùy chọn chuỗi tài liệu của nó.
Ghi chú
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 NotImplemented44 để tạo mô-đun mới nếu bạn muốn đặt các thuộc tính kiểm soát nhập khác nhau__doc__ ¶
The chuỗi 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 NotImplemented00. __loader__ ¶
Trình tải loader đã 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 NotImplemented00.
Thuộc tính này khớp 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 NotImplemented47 như được lưu trữ trong đố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 NotImplemented48
Ghi chú
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. Để đề phòng thay đổi có thể xảy ra này, tốt nhất bạn nên đọ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 NotImplemented48 hoặ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 NotImplemented50 nếu bạn rõ ràng cần sử dụng thuộc tính này
Đã thay đổi trong phiên bản 3. 4. 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 NotImplemented00. Trước đây thuộc tính là tùy chọn. __name__ ¶
Tên của mô-đun. Dự kiến sẽ khớp 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 NotImplemented52__gói__ ¶
Mô-đun thuộc về gói nào. Nếu mô-đun là cấp cao nhất [i. e. 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 NotImplemented53, nếu không, thuộc tính phải được đặt thành tên của gói [có thể 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 NotImplemented54 nếu bản thân mô-đun là một gói]. 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 NotImplemented00.
Thuộc tính này khớp 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 NotImplemented56 như được lưu trữ trong đố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 NotImplemented48
Ghi chú
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. Để đề phòng thay đổi có thể xảy ra này, tốt nhất bạn nên đọ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 NotImplemented48 hoặ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 NotImplemented59 nếu bạn rõ ràng cần sử dụng thuộc tính này
Đã thay đổi trong phiên bản 3. 4. 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 NotImplemented00. Trước đây thuộc tính là tùy chọn. __spec__ ¶
Bản ghi trạng thái liên quan đến hệ thống nhập của mô-đun. Dự kiến là một phiên bản 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 NotImplemented61
Mới trong phiên bản 3. 4
các loại. Kiểu dấu ba chấm ¶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 NotImplemented62
Mới trong phiên bản 3. 10
loại loại. Bí danh chung[t_origin , t_args]¶Loại thuốc generic được tham số hóa chẳng hạ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 NotImplemented63.
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 phải là một lớp chung không tham số hóa, chẳng hạ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 NotImplemented65,
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 NotImplemented66 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 NotImplemented67.
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 phải là một loại ________ 166 [có thể có độ dài 1] tham số hóa ________ 164
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
Mới trong phiên bản 3. 9
Đã thay đổi trong phiên bản 3. 9. 2. Loại này hiện có thể được phân lớp.
loại loại. UnionType ¶Loại biểu thức loại liên kết .
Mới trong phiên bản 3. 10
loại loại. TracebackType[tb_next , tb_frame, tb_lasti, tb_lineno]¶Loại đối tượng truy nguyên chẳng hạn như được tìm thấy trong
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 NotImplemented51
Xem tham chiếu ngôn ngữ để biết chi tiết về các thuộc tính và hoạt động có sẵn cũng như hướng dẫn tạo truy nguyên động.
các loại. Kiểu khung ¶Loại đối tượng khung chẳng hạn như được tìm thấy trong
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 NotImplemented52 nếu
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 NotImplemented53 là đối tượng truy nguyên
Xem tham chiếu ngôn ngữ để biết chi tiết về các thuộc tính và thao tác khả dụng.
Loại đối tượng được xác định trong các mô-đun mở rộng 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 NotImplemented54, chẳng hạ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 NotImplemented55 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 NotImplemented56. Loại này được sử dụng làm mô tả cho các thuộc tính đối tượng; các loại. MemberDescriptorType ¶
Loại đối tượng được xác định trong mô-đun mở rộng 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 NotImplemented58, chẳng hạ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 NotImplemented59. Loại này được sử dụng làm bộ mô tả cho các thành viên dữ liệu C đơn giản sử dụng các hàm chuyển đổi tiêu chuẩn;
Chi tiết triển khai CPython. Trong các triển khai khác của Python, loại này có thể giống 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 NotImplemented81loại loại. MappingProxyType[ánh xạ] ¶
Proxy chỉ đọc của ánh xạ. Nó cung cấp chế độ xem động trên các mục của ánh xạ, có nghĩa là khi ánh xạ thay đổi, chế độ xem sẽ phản ánh những thay đổi này
Mới trong phiên bản 3. 3
Đã thay đổi trong phiên bản 3. 9. Đã cập nhật để hỗ trợ toán tử hợ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 NotImplemented82] mới từ PEP 584, đơn giản là ủy quyền cho ánh xạ cơ bản. khóa trong proxy
Trả lạ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 NotImplemented83 nếu ánh xạ bên dưới có khóa chính, nếu không thì
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 NotImplemented84ủy nhiệm [key]
Trả lại mục của ánh xạ cơ bản bằng phím key. 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 NotImplemented85 nếu khóa không có trong ánh xạ cơ bảnlặp đi lặp lại [proxy]
Trả về một trình vòng lặp qua các khóa của ánh xạ cơ bản. Đây là lối tắt 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 NotImplemented86len[proxy]
Trả về số mục trong ánh xạ cơ bản
bản sao[] ¶Trả lại một bản sao nông của ánh xạ bên dưới
lấy[khóa[ , default]]¶Trả về giá trị cho khóa nếu khóa nằm trong ánh xạ cơ bản, nếu không thì mặc định. Nếu giá trị mặc định không được cung cấp, nó sẽ 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 NotImplemented00, do đó phương thức này không bao giờ tăng giá trị
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 NotImplemented85items[] ¶
Trả lại chế độ xem mới cho các mục của ánh xạ bên dướ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 NotImplemented89 cặp]phím[] ¶
Trả lại chế độ xem mới cho các khóa của ánh xạ bên dưới
giá trị[] ¶Trả lại chế độ xem mới về các giá trị của ánh xạ cơ bản
đảo ngược [ủy nhiệm]Trả về một trình vòng lặp đảo ngược trên các khóa của ánh xạ bên dưới
Mới trong phiên bản 3. 9
Các lớp và chức năng tiện ích bổ sung¶
loại loại. Không gian tên đơn giản ¶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 NotImplemented90 đơn giản cung cấp quyền truy cập thuộc tính vào không gian tên của nó, cũng như một repr có ý nghĩa
Khác 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 NotImplemented90, 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 NotImplemented92 bạn có thể thêm bớt 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 NotImplemented92 được khởi tạo với các đối số từ khóa, chúng sẽ đượ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 đoạn 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 NotImplemented92 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 NotImplemented95. Tuy nhiên, đối với loại bản ghi có cấu trúc, hãy 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 NotImplemented96 để 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ừ thứ tự bảng chữ cái thành thứ tự 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 NotImplemented67]. các loại. DynamicClassAttribute[fget=Không có, fset=None, fdel=None, doc=None]¶
Định tuyến truy cập thuộc tính trên một lớp tới __getattr__
Đây là một bộ 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. Quyền truy cập phiên bản 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 định tuyến đến phương thức __getattr__ của lớp đó;
Điều này cho phép một người 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 ví dụ về
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 NotImplemented98]
Mới trong phiên bản 3. 4
Hàm tiện ích của Coroutine¶
các loại. coroutine[gen_func] ¶Hàm này chuyển đổi một hàm tạo thành một chức năng coroutine . Tuy nhiên, nó có thể không nhất thiết phải thực hiện 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 NotImplemented99. trình lặp trình tạo , nhưng cũng được coi là 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 NotImplemented99 method.
Nếu gen_func là hàm tạo, nó sẽ được sửa đổi tại chỗ
Nếu gen_func không phải là hàm tạo, nó sẽ được bao bọc. Nếu nó trả về một thể hiện 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 NotImplemented00, thì thể hiện đó sẽ được bao bọc trong một đối tượng proxy có thể chờ được. Tất cả các loại đối tượng khác sẽ được trả về nguyên trạng