python有字面对象吗????

语法原生

obj = type('', (), {})()   # 造一个空类并立即实例化
obj.x = 1
obj.y = 2
print(obj.x, obj.y)       # 1 2

type(name, bases, dict) -> # 一个新类对象

class name(bases): # 等于这个
    dict 里的内容直接塞进类命名空间

官方库

from types import SimpleNamespace
obj = SimpleNamespace()
obj.x = 1
obj.y = 2
print(obj)                # namespace(x=1, y=2)

自定义

class X: pass
x = X()
x.ooo = lambda: "yes"
print(x.ooo())  # 输出: yes
class DictObj(dict):
    __getattr__ = dict.__getitem__
    __setattr__ = dict.__setitem__

obj = DictObj()
obj.x = 1
obj.y = 2
print(obj.x, obj['y'])    # 1 2