cyc*_*e87 4 python dictionary subclass
嗨,当我尝试使用另一个类的实例将新对象转换为字典时__dict__,我正在尝试使用组合来创建一个新类,它向我展示了<__main__.myobjec object at 0x00000000029CA908>,__dict__虽然我听说它与新类有关,但我不确定我是否使用错误,非常感谢任何帮助。
class User:
def __init__(self, name, job=None):
self.name = name
self.job = job
class Customer(User):
_ID = 100
def __init__(self, name, job=None):
self.name = name
self.job = job
class Account:
_ID = 0
def __init__(self, name, job=None):
self.customer = Customer(name , "Customer")
def __getattr__(self, attr):
return getattr(self.customer, attr)
Run Code Online (Sandbox Code Playgroud)
>>> A = Account("Abdi")
>>> A.__dict__
{'customer': <__main__.Customer object at 0x109fdbfc8>}
>>>
Run Code Online (Sandbox Code Playgroud)
您需要实现该__repr__方法来表示Customer该类的所有实例。
def __repr__(self): return repr(self.__dict__) # the dictionary of attributes in __repr__
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
793 次 |
| 最近记录: |