ChuannBlog

类成员

字段

方法

属性

相关方法

isinstance(object,classinfo)和issubclass(class,classinfo)

内置方法

class Singleton:
    def __new__(cls, *args, **kw):
        if not hasattr(cls, '_instance'):
            cls._instance = object.__new__(cls, *args, **kw)
        return cls._instance

one = Singleton()
two = Singleton()
three = Singleton()
print(one,two,three)
class Foo:
    def __init__(self):
        self.name = 'jack'
        self.age = 24
        
    def __getitem__(self, item):
        return self.__dict__[item]

    def __setitem__(self, key, value):
        self.__dict__[key] = value

    def __delitem__(self, key):
        del self.__dict__[key]
f = Foo()
print(f['name'])
f['name'] = 'bob'
del f['name']