Python SimpleNamespace thêm thuộc tính
Sorry if there was any confusion. I didn't mean to suggest we get rid of argparse.Namespace (in favor of SimpleNamespace). Rather, the former would subclass the latter. > * types.SimpleNamespace() sorts attributes, so this would get in the way of issue #39058. hence issue 39075 > * argparse.Namespace() supports a __contains__() method that isn't offered by types.SimpleNamespace(): As I suggested originally, there isn't any problem here if argparse.Namespace subclasses SimpleNamespace. > * Argparse is sensitive to start-up time so we mostly want to avoid adding new dependencies. I agree on avoiding extra imports. The simple solution right now is to use "type(sys.implementation)", though that isn't the clearest code. FWIW, this would also be solved if we added SimpleNamespace to the builtins, but that is a separate discussion. :) > * The __repr__ for SimpleNamespace() doesn't round-trip and isn't what we have now with Namespace. We could certainly fix the repr, but that's kind of irrelevant here if argparse.Namespace is a subclass. FWIW, this is also resolved if we add SimpleNamespace to the builtins (as "namespace"). > * Ironically, the class name "Namespace" is simpler than "SimpleNamespace" ;-) Agreed. :) We only used that long name because putting it in the "types" module mean it needed to have an extra clear name. That said, it is irrelevant here if argparse.Namespace is a subclass > * Much of the code in argparse.Namespace() inherits from _AttributeHolder, so switching to types.SimpleNamespace() doesn't really save us much code. Honestly, _AttributeHolder isn't needed. The only thing it provides is a nice repr (like SimpleNamespace does), with some extra functionality for dynamically sorting/filtering down the attrs shown in the repr. There are 3 subclasses and Namespace doesn't even use the attr filtering. The implementation doesn't seem to warrant a dedicated base class and it would be simpler for those 2 subclasses to each to have a dedicated __repr__ implementation (and for Namespace to subclass SimpleNamespce), rather than to subclass _AttributeHolder. Subclassing from _AttributeHolder gives the illusion that there is something more going on there than there actually is. Aside from that, there's the weaker argument about consistency and avoiding duplication (i.e. SimpleNamespace is the canonical "simple" namespace implementation in Python). Also, if SimpleNamespace had existed when argparse was written then I expect it would have been used instead. > Are there any upsides to switching? Attribute lookup is almost equally fast using either approach, so there is no speed benefit: Yeah, I didn't expect there to much difference in performance. SimpleNamespace() thực sự giỏi trong việc cấp quyền truy cập kiểu thuộc tính. Tôi muốn cung cấp chức năng đó cho mô-đun JSON (hoặc bất kỳ thứ gì khác chấp nhận lệnh tùy chỉnh) bằng cách thêm các phương thức ma thuật cho ánh xạ để chức năng này hoạt động Show danh mục = json. load(f, object_hook=SimpleNamespace) print(catalog['clothing']['mens']['shoes']['extra_wide']['quantity']) # hiện có thể với dict() print(catalog. quần áo. đàn ông. đôi giày. rộng thêm. số lượng]) # được đề xuất với SimpleNamespace() print(catalog. quần áo. bé trai['3t']. ngọn. số lượng # cũng sẽ được hỗ trợ Tôi đã thấy một cái gì đó như thế này trong quá trình sản xuất; . Đây là một điều ngớ ngẩn vì các lớp con tùy chỉnh rất khó viết, không chuẩn và thường hơi chậm. Tôi muốn xem phiên bản chất lượng cao này được cung cấp rộng rãi hơn Ý tưởng cốt lõi là giữ quyền truy cập thuộc tính đơn giản nhưng giúp tải dữ liệu theo chương trình dễ dàng hơn >>> ns = SimpleNamespace(roses='red', violets='blue') >>> thing = input() sugar >>> quality = input() sweet >>> setattr(ns, thing, quality) # current Nếu phương thức PEP 584 __ior__ được hỗ trợ, việc cập nhật SimpleNamespace sẽ sạch hơn nhiều ns. = some_dict Tôi đã đăng một vấn đề trên trình theo dõi. https. // lỗi. con trăn. tổ chức/vấn đề40284. Có một đề xuất để tạo một loại khác cho việc này, nhưng tôi không thấy điểm nào trong việc sao chép đáng kể mọi thứ mà SimpleNamespace đã làm chỉ để chúng tôi có thể thêm một số phương thức hỗ trợ. Vui lòng thêm bình luận để chúng tôi có thể tìm ra cách tốt nhất để cung cấp chức năng mạnh mẽ này 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ư hoặc 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 độngcá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ư Đố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 NotImplemented0 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ư 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 NotImplemented2 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 NotImplemented3 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 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 - 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ơ sở không phải là phiên bản của 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 , 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ộ giá trị 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ẩnMô-đ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ủa những tên này là để kiểm tra hoặc 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. NoneTypeloại Mới trong phiên bản 3. 10 các loại. Loại chức năngloại. Loại LambdaLoại hàm do người dùng định nghĩa và hàm được tạo bởi biểu thức Tăng mộ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 NotImplemented42 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 NotImplemented43 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ệnLoại đối tượng -iterator, được tạo bởi các hàm tạo các loại. Loại CoroutineLoại đối tượng được tạo bởi hàm Mới trong phiên bản 3. 5 các loại. AsyncGeneratorTypeLoại đối tượng -iterator, được tạo bởi các hàm 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 cho các đối tượng mã chẳng hạn như được trả về bởi Tăng mộ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 NotImplemented46 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 NotImplemented43, 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, 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 NotImplemented49, 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, 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, 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, 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 NotImplemented04, 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 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ứcLoạ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 chức năng tích hợpcác loại. BuiltinMethodTypeLoại hàm dựng sẵn như or và 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. WrapperDescriptorTypeLoại phương thức của một số kiểu dữ liệu và lớp cơ sở tích hợp như hoặc Mới trong phiên bản 3. 7 các loại. MethodWrapperTypeLoạ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 NotImplemented60 Mới trong phiên bản 3. 7 các loại. NotImplementedTypeloại Mới trong phiên bản 3. 10 các loại. MethodDescriptorTypeCác kiểu phương thức của một số kiểu dữ liệu dựng sẵn như Mới trong phiên bản 3. 7 các loại. ClassMethodDescriptorTypeLoạ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, 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 Mới trong phiên bản 3. 7 loại loại. Loại mô-đun(tên , doc=None)loại. Hàm tạo lấy tên của mô-đun sẽ được tạo và tùy chọn tên của mô-đun đó. Ghi chú Sử dụng để tạo một 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__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 NotImplemented40__bộ nạp__ Cá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 NotImplemented40 Thuộc tính này là để phù hợp như được lưu trữ trong đối tượng 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 hoặc sử dụng Đã 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 NotImplemented40. Trước đây thuộc tính là tùy chọn. __Tên__ Tên của mô-đun. Dự kiến sẽ phù hợp __bưu kiện__Mô-đun nào thuộc về. 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 NotImplemented40 Thuộc tính này là để phù hợp như được lưu trữ trong đối tượng 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 hoặc sử dụng Đã 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 NotImplemented40. 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 ví dụ của Mới trong phiên bản 3. 4 các loại. Loại dấu chấm lửngloại Mới trong phiên bản 3. 10 loại loại. Bí danh chung(t_origin , t_args)Loại 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 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 NotImplemented04 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 NotImplemented05, 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 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 NotImplemented07. 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 phải là một (có thể có độ dài 1) loại tham số hó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 NotImplemented04 >>> from types import GenericAlias >>> list[int] == GenericAlias(list, (int,)) True >>> dict[str, int] == GenericAlias(dict, (str, int)) True 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. UnionTypeloại 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 Xem để 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 khungLoại đối tượng khung chẳng hạn như được tìm thấy trong Xem để biết chi tiết về các thuộc tính và hoạt động có sẵn các loại. GetSetDescriptorTypeLoại đối tượng được xác định trong mô-đun mở rộng với Loại đối tượng được xác định trong mô-đun mở rộng với 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 NotImplemented21loạ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 NotImplemented22) 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 NotImplemented23 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 NotImplemented24ủy nhiệm [key] Trả lại mục của ánh xạ cơ bản bằng phím key. Tăng một phím if không có trong ánh xạ cơ bản lặ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 NotImplemented26len(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 đưa ra, 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 NotImplemented40, do đó phương thức này không bao giờ tăngitem() Trả lại chế độ xem mới cho các mục của ánh xạ bên dưới (____129 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ổ sungloại loại. Không gian tên đơn giảnMột lớp con đơ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 đại diện có ý nghĩa Không giống như , 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 NotImplemented32 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 NotImplemented32 đượ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 NotImplemented32 có thể hữu ích khi 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 NotImplemented35. Tuy nhiên, đối với loại bản ghi có cấu trúc, hãy sử dụng 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 phần lặp lại đã thay đổi từ thứ tự bảng chữ cái sang phần 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 NotImplemented07). 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 cá thể và có các thuộc tính ảo trên lớp có cùng tên (xem ví dụ) Mới trong phiên bản 3. 4 Chức năng tiện ích Coroutinecác loại. coroutine(gen_func)Hàm này chuyển đổi một hàm thành một hàm trả về một coroutine dựa trên trình tạo. Coroutine dựa trên trình tạo vẫn là một , nhưng cũng được coi là một đối tượng và là. 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 NotImplemented39 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 , 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 |