f = People('egon',18,'male')
非函数
hasattr # hasattr(f,'name')
getattr # getattr(f,'name')
setattr # setattr(f,'name','EGON')
class Teacher:
school = 'oldboy'
def __init__(self,name,age):
self.name = name
self.age = age
def teach(self):
print(self.name)
# print(hasattr(Teacher,'school'))
# print(getattr(Teacher,'school'))
# print(getattr(Teacher,'asdfadsf','没有这个'))
# setattr(Teacher,'school','欧德博爱')
# print(Teacher.school)
# delattr(Teacher,'school')
# print(Teacher.__dict__)
t = Teacher('egon',18)
# print(hasattr(t,'name'))
# print(getattr(t,'name'))
# setattr(t,'name','EGON')
# print(getattr(t,'name'))
#
# print(t.__dict__)
# delattr(t,'name')
# print(t.__dict__)
# print(getattr(t,'teach'))
# print(getattr(t,'school'))
# print(t.school)
# t.school = 'hahhahahahha'
# print(getattr(Teacher,'school'))
# print(getattr(t,'school'))
# print(t.__dict__)
item系列
__getitem__ # f['name']
__setitem__ # f['name'] = 'EGON'
__delitem__ # del f['name']
__len__ # len(f)
class Foo:
def __init__(self,name,age,sex):
self.name=name
self.age=age
self.sex=sex
def __getitem__(self, item):
# print(self,item,type(item))
# return getattr(self,item)
return self.__dict__[item]
def __setitem__(self, key, value):
# setattr(self,key,value)
self.__dict__[key]=value
def __delitem__(self, key):
# delattr(self,key)
self.__dict__.pop(key)
def __len__(self):
return 10
f=Foo('egon',18,'male')
# print(f.name) #f['name']
# print(f.age) #f['age']
# print(f.sex) #f['sex']
# print(f['name'])
# f['name']='egon_nb'
# print(f.__dict__)
# del f['name']
# print(f.__dict__)
print(len(f))
f = People
__str__ # print(f) # f.__str__()
class Teacher:
def __init__(self,name,age):
self.name=name
self.age=age
self.courses=[]
def teach(self):
print('%s teach' %self.name)
def __str__(self):
return '<name:%s age:%s>' %(self.name,self.age)
class Course:
def __init__(self,name,price,period):
self.name=name
self.price=price
self.period=period
def __str__(self):
return '《name:%s price:%s period:%s》' %(self.name,self.price,self.period)
# egon=Teacher('egon',18)
# print(egon) #egon.__str__()
# print(egon) #egon.__str__()
__del__ # del f # f.__del__()
import time
class Foo:
def __init__(self,x):
self.x=x
print('connect mysql') #conn=abcdef('192.168.1.10',3306)
def __del__(self):
'''做一些与这个对象有关的清理操作'''
# conn.close()
# file.close()
print('====>')
f=Foo(10)
del f #f.__del__()
time.sleep(3)
print('主程序')