#判断obj是不是Foo的对象
class Foo:
pass
obj = Foo()
#print(isinstance(obj, Foo))
#判断Bar是不是Serives的子类
class Serives(object):
pass
class Bar(Serives):
pass
#print(issubclass(Bar, Serives))
class Foo:
def __init__(self, name):
self.name = name
def __getitem__(self, item): #item = ''name''
#print('getitem')
return self.__dict__.get(item)
def __setitem__(self, key, value):
# print('setitem')
# print(key, value)
self.__dict__[key] = value
def __delitem__(self, key):
# print('delitem')
# print(key)
#del self.__dict__[key] 删除法一
self.__dict__.pop(key) #删除法二
p = Foo('egon')
#查看属性
#p.属性名字
print(p['name']) #相当于p.name
#设置属性
p.sex = 'male'
p['sex'] = 'male'
print(p.__dict__)
print(p.sex)
#删除属性
#之前 del p.sex
del p['sex']
print(p.__dict__)
#所有的数据类型就是类
d = dict({'username': 'laowang'})
#print(d.__dict__)
print(d)
print(isinstance(d, dict))
print(dict.__dict__)
class People:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return '{name:%s, age:%s}'%(self.name, self.age)
obj = People('laowang', '18')
print(obj) #res = obj.__str__()
#打印obj对象就想当于执行了 __str__() 结果 只返回str
class Open:
def __init__(self, filename):
print('打开文件。。。。')
self.filename = filename
def __del__(self):
print('回收操作系统资源,self.close')
f = Open('setting.py')
#del f #手动触发del
print('-------main--------')
'''__del__方法会在程序执行完或者手动执行del时会先触发回收对象的机制,然后再进行回收
'''