Hướng dẫn what is simplenamespace in python? - không gian tên đơn giản trong python là gì?

Sau đây dường như hoạt động một trong hai cách. Lợi thế (trừ repr tốt đẹp) của việc sử dụng types.SimpleNamespace là gì? Hay đó là điều tương tự?

>>> import types
>>> class Cls():
...     pass
... 
>>> foo = types.SimpleNamespace() # or foo = Cls()
>>> foo.bar = 42
>>> foo.bar
42
>>> del foo.bar
>>> foo.bar
AttributeError: 'types.SimpleNamespace' object has no attribute 'bar'

Khi được hỏi ngày 11 tháng 5 năm 2016 lúc 11:28May 11, 2016 at 11:28

Hướng dẫn what is simplenamespace in python? - không gian tên đơn giản trong python là gì?

BaruchbaruchBaruch

Huy hiệu vàng 19.9K2424 gold badges123 silver badges196 bronze badges

0

Điều này được giải thích khá tốt trong các loại mô tả mô -đun. Nó cho bạn thấy rằng types.SimpleNamespace gần tương đương với điều này:

class SimpleNamespace:
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

    def __repr__(self):
        keys = sorted(self.__dict__)
        items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys)
        return "{}({})".format(type(self).__name__, ", ".join(items))

    def __eq__(self, other):
        return self.__dict__ == other.__dict__

Điều này cung cấp các lợi thế sau so với một lớp trống:

  1. Nó cho phép bạn khởi tạo các thuộc tính trong khi xây dựng đối tượng:
    class SimpleNamespace:
        def __init__(self, **kwargs):
            self.__dict__.update(kwargs)
    
        def __repr__(self):
            keys = sorted(self.__dict__)
            items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys)
            return "{}({})".format(type(self).__name__, ", ".join(items))
    
        def __eq__(self, other):
            return self.__dict__ == other.__dict__
    
    0
  2. Nó cung cấp một
    class SimpleNamespace:
        def __init__(self, **kwargs):
            self.__dict__.update(kwargs)
    
        def __repr__(self):
            keys = sorted(self.__dict__)
            items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys)
            return "{}({})".format(type(self).__name__, ", ".join(items))
    
        def __eq__(self, other):
            return self.__dict__ == other.__dict__
    
    1:
    class SimpleNamespace:
        def __init__(self, **kwargs):
            self.__dict__.update(kwargs)
    
        def __repr__(self):
            keys = sorted(self.__dict__)
            items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys)
            return "{}({})".format(type(self).__name__, ", ".join(items))
    
        def __eq__(self, other):
            return self.__dict__ == other.__dict__
    
    2 có thể đọc được
  3. Nó ghi đè so sánh mặc định. Thay vì so sánh với
    class SimpleNamespace:
        def __init__(self, **kwargs):
            self.__dict__.update(kwargs)
    
        def __repr__(self):
            keys = sorted(self.__dict__)
            items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys)
            return "{}({})".format(type(self).__name__, ", ".join(items))
    
        def __eq__(self, other):
            return self.__dict__ == other.__dict__
    
    3, nó so sánh các giá trị thuộc tính thay thế.

Hướng dẫn what is simplenamespace in python? - không gian tên đơn giản trong python là gì?

Martineau

Huy hiệu vàng 116K2525 gold badges161 silver badges286 bronze badges

Đã trả lời ngày 11 tháng 5 năm 2016 lúc 11:33May 11, 2016 at 11:33

Hướng dẫn what is simplenamespace in python? - không gian tên đơn giản trong python là gì?

9

Một

class SimpleNamespace:
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

    def __repr__(self):
        keys = sorted(self.__dict__)
        items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys)
        return "{}({})".format(type(self).__name__, ", ".join(items))

    def __eq__(self, other):
        return self.__dict__ == other.__dict__
4 cung cấp một cơ chế để khởi tạo một đối tượng có thể giữ các thuộc tính và không có gì khác. Trong thực tế, nó là một lớp trống với fancier
class SimpleNamespace:
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

    def __repr__(self):
        keys = sorted(self.__dict__)
        items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys)
        return "{}({})".format(type(self).__name__, ", ".join(items))

    def __eq__(self, other):
        return self.__dict__ == other.__dict__
5 và một
class SimpleNamespace:
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

    def __repr__(self):
        keys = sorted(self.__dict__)
        items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys)
        return "{}({})".format(type(self).__name__, ", ".join(items))

    def __eq__(self, other):
        return self.__dict__ == other.__dict__
6 hữu ích:

>>> from types import SimpleNamespace
>>> sn = SimpleNamespace(x = 1, y = 2)
>>> sn
namespace(x=1, y=2)
>>> sn.z = 'foo'
>>> del(sn.x)
>>> sn
namespace(y=2, z='foo')

hoặc

from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)

output:

namespace(x=1, y=2)
namespace(y=2, z='foo')

Câu trả lời này cũng có thể hữu ích.

Đã trả lời ngày 5 tháng 2 năm 2021 lúc 14:12Feb 5, 2021 at 14:12

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 dưới dạng tích hợp như

class SimpleNamespace:
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

    def __repr__(self):
        keys = sorted(self.__dict__)
        items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys)
        return "{}({})".format(type(self).__name__, ", ".join(items))

    def __eq__(self, other):
        return self.__dict__ == other.__dict__
7 hoặc
class SimpleNamespace:
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

    def __repr__(self):
        keys = sorted(self.__dict__)
        items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys)
        return "{}({})".format(type(self).__name__, ", ".join(items))

    def __eq__(self, other):
        return self.__dict__ == other.__dict__
8.

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

TYPE.New_Class (Tên, BASES = (), KWDS = Không, EXEC_BODY = Không) ¶new_class(name, bases=(), kwds=None, exec_body=None)

Tạo một đối tượng lớp động bằng cách sử dụng metaclass thích hợp.

Ba đối số đầu tiên là các thành phần tạo nên tiêu đề định nghĩa lớp: tên lớp, các lớp cơ sở (theo thứ tự), các đối số từ khóa (như

class SimpleNamespace:
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

    def __repr__(self):
        keys = sorted(self.__dict__)
        items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys)
        return "{}({})".format(type(self).__name__, ", ".join(items))

    def __eq__(self, other):
        return self.__dict__ == other.__dict__
9).

Đố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

>>> from types import SimpleNamespace
>>> sn = SimpleNamespace(x = 1, y = 2)
>>> sn
namespace(x=1, y=2)
>>> sn.z = 'foo'
>>> del(sn.x)
>>> sn
namespace(y=2, z='foo')
0.

Mới trong phiên bản 3.3.

TYPE.Prepare_Class (Tên, Base = (), KWDS = Không) ¶prepare_class(name, bases=(), kwds=None)

Tính toán metaclass thích hợp và tạo không gian tên lớp.

Các đối số là các thành phần tạo nên tiêu đề định nghĩa lớp: tên lớp, các lớp cơ sở (theo thứ tự) và các đối số từ khóa (như

class SimpleNamespace:
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

    def __repr__(self):
        keys = sorted(self.__dict__)
        items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys)
        return "{}({})".format(type(self).__name__, ", ".join(items))

    def __eq__(self, other):
        return self.__dict__ == other.__dict__
9).

Giá trị trả về là 3-Tuple:

>>> from types import SimpleNamespace
>>> sn = SimpleNamespace(x = 1, y = 2)
>>> sn
namespace(x=1, y=2)
>>> sn.z = 'foo'
>>> del(sn.x)
>>> sn
namespace(y=2, z='foo')
2

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

>>> from types import SimpleNamespace
>>> sn = SimpleNamespace(x = 1, y = 2)
>>> sn
namespace(x=1, y=2)
>>> sn.z = 'foo'
>>> del(sn.x)
>>> sn
namespace(y=2, z='foo')
3 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.

Thay đổi trong phiên bản 3.6: Giá trị mặc định cho phần tử

>>> from types import SimpleNamespace
>>> sn = SimpleNamespace(x = 1, y = 2)
>>> sn
namespace(x=1, y=2)
>>> sn.z = 'foo'
>>> del(sn.x)
>>> sn
namespace(y=2, z='foo')
4 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
>>> from types import SimpleNamespace
>>> sn = SimpleNamespace(x = 1, y = 2)
>>> sn
namespace(x=1, y=2)
>>> sn.z = 'foo'
>>> del(sn.x)
>>> sn
namespace(y=2, z='foo')
5.The default value for the
>>> from types import SimpleNamespace
>>> sn = SimpleNamespace(x = 1, y = 2)
>>> sn
namespace(x=1, y=2)
>>> sn.z = 'foo'
>>> del(sn.x)
>>> sn
namespace(y=2, z='foo')
4 element of the returned tuple has changed. Now an insertion-order-preserving mapping is used when the metaclass does not have a
>>> from types import SimpleNamespace
>>> sn = SimpleNamespace(x = 1, y = 2)
>>> sn
namespace(x=1, y=2)
>>> sn.z = 'foo'
>>> del(sn.x)
>>> sn
namespace(y=2, z='foo')
5 method.

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ày

PEP 3115 - Metaclasses trong Python 3000 - Metaclasses in Python 3000

Giới thiệu móc không gian tên

>>> from types import SimpleNamespace
>>> sn = SimpleNamespace(x = 1, y = 2)
>>> sn
namespace(x=1, y=2)
>>> sn.z = 'foo'
>>> del(sn.x)
>>> sn
namespace(y=2, z='foo')
5

loại.resolve_base (cơ sở) ¶resolve_bases(bases)

Giải quyết các mục MRO động theo quy định của PEP 560.PEP 560.

Hàm này tìm kiếm các mục trong các cơ sở không phải là trường hợp của

>>> from types import SimpleNamespace
>>> sn = SimpleNamespace(x = 1, y = 2)
>>> sn
namespace(x=1, y=2)
>>> sn.z = 'foo'
>>> del(sn.x)
>>> sn
namespace(y=2, z='foo')
7 và trả về một tuple trong đó mỗi đối tượng có phương thức
>>> from types import SimpleNamespace
>>> sn = SimpleNamespace(x = 1, y = 2)
>>> sn
namespace(x=1, y=2)
>>> sn.z = 'foo'
>>> del(sn.x)
>>> sn
namespace(y=2, z='foo')
8 được thay thế bằng kết quả chưa đóng gói khi gọi phương thức này. Nếu một mục cơ sở là một ví dụ là
>>> from types import SimpleNamespace
>>> sn = SimpleNamespace(x = 1, y = 2)
>>> sn
namespace(x=1, y=2)
>>> sn.z = 'foo'
>>> del(sn.x)
>>> sn
namespace(y=2, z='foo')
7, hoặc nó không có phương thức
>>> from types import SimpleNamespace
>>> sn = SimpleNamespace(x = 1, y = 2)
>>> sn
namespace(x=1, y=2)
>>> sn.z = 'foo'
>>> del(sn.x)
>>> sn
namespace(y=2, z='foo')
8, thì nó được bao gồm trong phần trả lại không thay đổi.

Mới trong phiên bản 3.7.

Xem thêm

PEP 560 - Hỗ trợ cốt lõi để gõ mô -đun và loại chung - Core support for typing module and generic types

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 để 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ại

from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)
1.

Việc sử dụng điển hình của các tên này là để kiểm tra

from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)
2 hoặc
from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)
3.

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:

loại.Nonetype¶NoneType

Loại

from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)
4.

Mới trong phiên bản 3.10.

TYPE.FunctionTypetypes.LambdaType

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

from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)
5.

Tăng một sự kiện kiểm toán

from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)
6 với đối số
from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)
7.auditing event
from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)
6 with argument
from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)
7.

Sự kiện kiểm toán 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 nêu ra để biên dịch bình thường.

loại.generatortype¶GeneratorType

Loại đối tượng máy phát điện, được tạo bởi các hàm của máy phát.generator-iterator objects, created by generator functions.

loại.coroutInetype¶CoroutineType

Loại đối tượng Coroutine, được tạo bởi các hàm

from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)
8.coroutine objects, created by
from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)
8 functions.

Mới trong phiên bản 3.5.

loại.asyncgeneratortype¶AsyncGeneratorType

Loại đối tượng máy phát điện không đồng bộ, được tạo bởi các hàm tạo không đồng bộ.asynchronous generator-iterator objects, created by asynchronous generator functions.

Mới trong phiên bản 3.6.

classtypes.codetype (** kwargs) ¶types.CodeType(**kwargs)

Loại cho các đối tượng mã như được trả về bởi

from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)
9.

Tăng một sự kiện kiểm toán

namespace(x=1, y=2)
namespace(y=2, z='foo')
0 với các đối số
from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)
7,
namespace(x=1, y=2)
namespace(y=2, z='foo')
2,
namespace(x=1, y=2)
namespace(y=2, z='foo')
3,
namespace(x=1, y=2)
namespace(y=2, z='foo')
4,
namespace(x=1, y=2)
namespace(y=2, z='foo')
5,
namespace(x=1, y=2)
namespace(y=2, z='foo')
6,
namespace(x=1, y=2)
namespace(y=2, z='foo')
7,
namespace(x=1, y=2)
namespace(y=2, z='foo')
8,
namespace(x=1, y=2)
namespace(y=2, z='foo')
9.auditing event
namespace(x=1, y=2)
namespace(y=2, z='foo')
0 with arguments
from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)
7,
namespace(x=1, y=2)
namespace(y=2, z='foo')
2,
namespace(x=1, y=2)
namespace(y=2, z='foo')
3,
namespace(x=1, y=2)
namespace(y=2, z='foo')
4,
namespace(x=1, y=2)
namespace(y=2, z='foo')
5,
namespace(x=1, y=2)
namespace(y=2, z='foo')
6,
namespace(x=1, y=2)
namespace(y=2, z='foo')
7,
namespace(x=1, y=2)
namespace(y=2, z='foo')
8,
namespace(x=1, y=2)
namespace(y=2, z='foo')
9.

Lưu ý rằng các đối số được kiểm toán có thể không khớp với tên hoặc vị trí theo yêu cầu của trình khởi tạo. Sự kiện kiểm toán chỉ xảy ra để khởi tạo trực tiếp các đối tượng mã và không được nêu ra để biên dịch bình thường.

thay thế (** kwargs) ¶(**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 được chỉ định.

Mới trong phiên bản 3.8.

loại.cellType¶CellType

Loại cho các đối tượng ô: Các đối tượng như vậy được sử dụng làm thùng chứa cho các biến miễn phí của hàm.

Mới trong phiên bản 3.8.

loại.cellType¶MethodType

Loại cho các đối tượng ô: Các đối tượng như vậy được sử dụng làm thùng chứa cho các biến miễn phí của hàm.

loại.methodtype¶BuiltinFunctionTypetypes.BuiltinMethodType

Loại phương pháp của các trường hợp lớp do người dùng xác định.

các loại.builtinfonthype¶ Type.builtinmethodtype¶WrapperDescriptorType

Loại chức năng tích hợp như

>>> from types import GenericAlias

>>> list[int] == GenericAlias(list, (int,))
True
>>> dict[str, int] == GenericAlias(dict, (str, int))
True
0 hoặc
>>> from types import GenericAlias

>>> list[int] == GenericAlias(list, (int,))
True
>>> dict[str, int] == GenericAlias(dict, (str, int))
True
1 và các phương pháp của các lớp tích hợp. .

Mới trong phiên bản 3.7.

Xem thêmMethodWrapperType

PEP 560 - Hỗ trợ cốt lõi để gõ mô -đun và loại chung

Mới trong phiên bản 3.7.

Xem thêmNotImplementedType

PEP 560 - Hỗ trợ cốt lõi để gõ mô -đun và loại chung

Mới trong phiên bản 3.10.

TYPE.MethodDescriptorType

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

from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)
5.

Mới trong phiên bản 3.7.

Tăng một sự kiện kiểm toán
from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)
6 với đối số
from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)
7.
ClassMethodDescriptorType

Sự kiện kiểm toán 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 nêu ra để biên dịch bình thường.

Mới trong phiên bản 3.7.

loại.generatortype¶types.ModuleType(name, doc=None)

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.modules. The constructor takes the name of the module to be created and optionally its docstring.

loại.coroutInetype¶

Loại đối tượng Coroutine, được tạo bởi các hàm

from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)
8.docstring of the module. Defaults to
from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)
4.

Mới trong phiên bản 3.5.

loại.asyncgeneratortype¶loader which loaded the module. Defaults to

from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)
4.

Loại đối tượng máy phát điện không đồng bộ, được tạo bởi các hàm tạo không đồng bộ.

Mới trong phiên bản 3.6.

classtypes.codetype (** kwargs) ¶

Loại cho các đối tượng mã như được trả về bởi

from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)
9.Defaults to
from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)
4. Previously the attribute was optional.

Tăng một sự kiện kiểm toán
namespace(x=1, y=2)
namespace(y=2, z='foo')
0 với các đối số
from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)
7,
namespace(x=1, y=2)
namespace(y=2, z='foo')
2,
namespace(x=1, y=2)
namespace(y=2, z='foo')
3,
namespace(x=1, y=2)
namespace(y=2, z='foo')
4,
namespace(x=1, y=2)
namespace(y=2, z='foo')
5,
namespace(x=1, y=2)
namespace(y=2, z='foo')
6,
namespace(x=1, y=2)
namespace(y=2, z='foo')
7,
namespace(x=1, y=2)
namespace(y=2, z='foo')
8,
namespace(x=1, y=2)
namespace(y=2, z='foo')
9.

Lưu ý rằng các đối số được kiểm toán có thể không khớp với tên hoặc vị trí theo yêu cầu của trình khởi tạo. Sự kiện kiểm toán chỉ xảy ra để khởi tạo trực tiếp các đối tượng mã và không được nêu 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 được chỉ định.package a module belongs to. If the module is top-level (i.e. not a part of any specific package) then the attribute should be set to

class SimpleNamespace:
    def __init__(self, /, **kwargs):
        self.__dict__.update(kwargs)

    def __repr__(self):
        items = (f"{k}={v!r}" for k, v in self.__dict__.items())
        return "{}({})".format(type(self).__name__, ", ".join(items))

    def __eq__(self, other):
        if isinstance(self, SimpleNamespace) and isinstance(other, SimpleNamespace):
           return self.__dict__ == other.__dict__
        return NotImplemented
6, else it should be set to the name of the package (which can be
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
7 if the module is a package itself). Defaults to
from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)
4.

Mới trong phiên bản 3.8.

Mới trong phiên bản 3.6.

classtypes.codetype (** kwargs) ¶

Đã thay đổi trong phiên bản 3.4: Mặc định thành

from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)
4. Trước đây thuộc tính là tùy chọn.Defaults to
from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)
4. Previously the attribute was optional.

__Spec__¶

Một bản ghi của trạng thái liên quan đến hệ thống nhập khẩu của mô-đun. Dự kiến ​​sẽ là một ví dụ của repr4.

Mới trong phiên bản 3.4.

glem.ellipsistype¶EllipsisType

Loại repr5.

Mới trong phiên bản 3.10.

classtypes.genericias (t_origin, t_args) ¶types.GenericAlias(t_origin, t_args)

Loại thuốc generic được tham số hóa như repr6.parameterized generics such as repr6.

repr7 phải là một lớp chung không tham số, chẳng hạn như repr8, repr9 hoặc types.SimpleNamespace0. types.SimpleNamespace1 phải là repr9 (có thể là độ dài 1) của các loại tham số hóa repr7:

>>> 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 nhóm.This type can now be subclassed.

classtypes.uniontype¶ types.UnionType

Loại biểu thức loại liên minh.union type expressions.

Mới trong phiên bản 3.10.

classtypes.genericias (t_origin, t_args) ¶ types.TracebackType(tb_next, tb_frame, tb_lasti, tb_lineno)

Loại thuốc generic được tham số hóa như repr6.

repr7 phải là một lớp chung không tham số, chẳng hạn như repr8, repr9 hoặc types.SimpleNamespace0. types.SimpleNamespace1 phải là repr9 (có thể là độ dài 1) của các loại tham số hóa repr7:the language reference for details of the available attributes and operations, and guidance on creating tracebacks dynamically.

Mới trong phiên bản 3.9.FrameType

Đã thay đổi trong phiên bản 3.9.2: Loại này hiện có thể được phân nhóm.

classtypes.uniontype¶the language reference for details of the available attributes and operations.

Loại biểu thức loại liên minh.GetSetDescriptorType

classtypes.tracebacktype (tb_next, tb_frame, tb_lasti, tb_lineno) ¶

Loại đối tượng Traceback như được tìm thấy trong types.SimpleNamespace4.MemberDescriptorType

Xem tài liệu tham khảo ngôn ngữ để biết chi tiết về các thuộc tính và hoạt động có sẵn và hướng dẫn về việc tạo Tracebacks một cách linh hoạt.

loại.frametype¶ In other implementations of Python, this type may be identical to types.SimpleNamespace4.

Loại đối tượng khung như được tìm thấy trong types.SimpleNamespace5 nếu types.SimpleNamespace6 là một đối tượng TraceBack.types.MappingProxyType(mapping)

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.

typ

Loại đối tượng được xác định trong các mô -đun mở rộng với types.SimpleNamespace7, chẳng hạn như types.SimpleNamespace8 hoặc types.SimpleNamespace9. Loại này được sử dụng làm mô tả cho các thuộc tính đối tượng; Nó có cùng mục đích với loại types.SimpleNamespace0, nhưng đối với các lớp được xác định trong các mô -đun mở rộng.Updated to support the new union (types.SimpleNamespace5) operator from PEP 584, which simply delegates to the underlying mapping.

loại.MemberDescriptortype¶ in proxy

Loại đối tượng được xác định trong các mô -đun mở rộng với types.SimpleNamespace1, chẳng hạn như types.SimpleNamespace2. Loại này được sử dụng làm mô tả cho các thành viên dữ liệu C đơn giản sử dụng các chức năng chuyển đổi tiêu chuẩn; Nó có cùng mục đích với loại types.SimpleNamespace0, nhưng đối với các lớp được xác định trong các mô -đun mở rộng.

proxy[key]

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 hệt với types.SimpleNamespace4.

iter(proxy)

classtypes.mappingproxytype (lập bản đồ) ¶

len(proxy)

Proxy chỉ đọc của một bản đồ. Nó cung cấp một chế độ xem động trên các mục nhập ánh xạ, điều đó có nghĩa là khi ánh xạ thay đổi, chế độ xem 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 cập nhật để hỗ trợ nhà điều hành Liên minh mới (types.SimpleNamespace5) từ PEP 584, chỉ cần ủy thác cho bản đồ cơ bản.

KeyinProxy(key[, default])

Trả về types.SimpleNamespace6 Nếu ánh xạ cơ bản có khóa khóa, nếu không types.SimpleNamespace7.

Trả về mục của ánh xạ cơ bản với khóa khóa. Tăng types.SimpleNamespace8 nếu khóa không nằm trong ánh xạ cơ bản.()

Trả lại một trình lặp qua các khóa của ánh xạ cơ bản. Đây là một lối tắt cho types.SimpleNamespace9.

Trả lại số lượng các mục trong ánh xạ cơ bản.()

sao chép () ¶

Trả lại một bản sao nông của ánh xạ cơ bản.()

Nhận (khóa [, mặc định]) ¶

reversed(proxy)

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à

from types import SimpleNamespace

sn = SimpleNamespace(x = 1, y = 2)
print(sn)

sn.z = 'foo'
del(sn.x)
print(sn)
4, do đó phương thức này không bao giờ tăng types.SimpleNamespace8.

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 nhóm.

classtypes.uniontype¶ types.SimpleNamespace

Loại biểu thức loại liên minh.

classtypes.tracebacktype (tb_next, tb_frame, tb_lasti, tb_lineno) ¶

Loại đối tượng Traceback như được tìm thấy trong types.SimpleNamespace4.

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

Xem tài liệu tham khảo ngôn ngữ để biết chi tiết về các thuộc tính và hoạt động có sẵn và hướng dẫn về việc tạo Tracebacks một cách linh hoạt.

typ

Loại đối tượng được xác định trong các mô -đun mở rộng với types.SimpleNamespace7, chẳng hạn như types.SimpleNamespace8 hoặc types.SimpleNamespace9. Loại này được sử dụng làm mô tả cho các thuộc tính đối tượng; Nó có cùng mục đích với loại types.SimpleNamespace0, nhưng đối với các lớp được xác định trong các mô -đun mở rộng.Attribute order in the repr changed from alphabetical to insertion (like types.SimpleNamespace0).

loại.MemberDescriptortype¶DynamicClassAttribute(fget=None, fset=None, fdel=None, doc=None)

Loại đối tượng được xác định trong các mô -đun mở rộng với types.SimpleNamespace1, chẳng hạn như types.SimpleNamespace2. Loại này được sử dụng làm mô tả cho các thành viên dữ liệu C đơn giản sử dụng các chức năng chuyển đổi tiêu chuẩn; Nó có cùng mục đích với loại types.SimpleNamespace0, nhưng đối với các lớp được xác định trong các mô -đun mở rộng.

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 hệt với types.SimpleNamespace4.

classtypes.mappingproxytype (lập bản đồ) ¶

Mới trong phiên bản 3.4.

Proxy chỉ đọc của một bản đồ. Nó cung cấp một chế độ xem động trên các mục nhập ánh xạ, điều đó có nghĩa là khi ánh xạ thay đổi, chế độ xem phản ánh những thay đổi này.

Mới trong phiên bản 3.3.coroutine(gen_func)

Đã thay đổi trong phiên bản 3.9: Được cập nhật để hỗ trợ nhà điều hành Liên minh mới (types.SimpleNamespace5) từ PEP 584, chỉ cần ủy thác cho bản đồ cơ bản.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):
        keys = sorted(self.__dict__)
        items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys)
        return "{}({})".format(type(self).__name__, ", ".join(items))

    def __eq__(self, other):
        return self.__dict__ == other.__dict__
12 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):
        keys = sorted(self.__dict__)
        items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys)
        return "{}({})".format(type(self).__name__, ", ".join(items))

    def __eq__(self, other):
        return self.__dict__ == other.__dict__
13, 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.

Bản thân __ dict __ python là gì?

__dict__ là một từ điển hoặc đối tượng ánh xạ khác được sử dụng để lưu trữ các thuộc tính (có thể ghi) của một đối tượng. Hoặc nói bằng các từ đơn giản Mỗi đối tượng trong Python có một thuộc tính được ký hiệu là __dict__. Và đối tượng này chứa tất cả các thuộc tính được xác định cho đối tượng.A dictionary or other mapping object used to store an object's (writable) attributes. Or speaking in simple words every object in python has an attribute which is denoted by __dict__. And this object contains all attributes defined for the object.

Thư viện loại trong Python là gì?

Mô-đun loại chứa các đối tượng loại cho tất cả các loại đối tượng được xác định bởi trình thông dịch tiêu chuẩn, như ví dụ 1-86 thể hiện.Tất cả các đối tượng cùng loại chia sẻ một đối tượng loại duy nhất.Bạn có thể sử dụng là để kiểm tra nếu một đối tượng có một loại nhất định.contains type objects for all object types defined by the standard interpreter, as Example 1-86 demonstrates. All objects of the same type share a single type object. You can use is to test if an object has a given type.

Loại đối tượng Python là gì?

Tất cả mọi thứ trong Python là một đối tượng bao gồm các lớp.Tất cả các lớp là các trường hợp của một lớp gọi là "loại".Đối tượng loại cũng là một thể hiện của loại loại.Bạn có thể kiểm tra hệ thống phân cấp kế thừa của lớp bằng cách kiểm tra thuộc tính __base__ của một đối tượng lớp.an instance of type class. You can inspect the inheritance hierarchy of class by examining the __bases__ attribute of a class object.

Loại loại trong Python là gì?

Loại là một metaclass, trong đó các lớp là trường hợp.Giống như một đối tượng thông thường là một ví dụ của một lớp, bất kỳ lớp kiểu mới nào trong Python, và do đó, bất kỳ lớp nào trong Python 3, là một ví dụ của loại metaclass.Trong trường hợp trên: x là một ví dụ của lớp foo.Foo là một ví dụ của loại metaclass.a metaclass, of which classes are instances. Just as an ordinary object is an instance of a class, any new-style class in Python, and thus any class in Python 3, is an instance of the type metaclass. In the above case: x is an instance of class Foo . Foo is an instance of the type metaclass.