class Person:
"""
I'm persion doc!
"""
def __init__(self):
self.name = "zhangsan"
self.age = 10
def __str__(self):
return "I'm str"
def __repr__(self):
return "I'm repr"
def __call__(self):
return "I'm call"
p = Person()
method | usage | result | 人话 |
---|
__dict__ | p.__dict__ | {‘name’: ‘zhangsan’, ‘age’: 10} | |
__class__ | p.__class__ | <class ‘main.Person’> | 返回对象的类 |
__doc__ | p.__doc__ | “\n\tI’m persion doc!\n\t” | |
__new__ | | | 单例模式 |
__repr__ | p.__repr__() | <main.Person object at 0x7f256a67c2d0> | p |
__dir__ | p.__dir__() | | dir() |
__del__ | | | del p |
__str__ | | | str( p ) |
__call__ | | | p() |
class Person:
_instance = None
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super().__new__(cls, *args, **kwargs)
return cls._instance
p1 = Person()
p2 = Person()
print(p1)
print(p2)
__new__