内置函数的补充 反射

# l=list([])
# print(type(l)is list)
# print(isinstance(l,list))
'''
True
'''
# class Foo:
# pass
# class Bar(Foo):
# pass
#
# print(issubclass(Bar,Foo))
'''
True
'''
# class Foo:
# def __init__(self,name):
# self.name=name
# def f1(self):
# print('f1')
# obj=Foo('rambo')
'''
hasattr
setattr
getattr
delattr
'''
# print(hasattr(obj,'name'))
'''
True
'''
# print(hasattr(obj,'f1'))
'''
True
'''
# if hasattr(obj,'f1'):
# f=getattr(obj,'f1')
# print(f)
'''
<bound method Foo.f1 of <__main__.Foo object at 0x000001BB5B6A1288>>
'''
# if hasattr(obj,'f1'):
# f=getattr(obj,'f1')
# f()
'''
f1
'''
# getattr(obj,'xxx',None)
# setattr(obj,'x',1)
# print(obj.__dict__)
'''
{'name': 'rambo', 'x': 1}
'''
# delattr(obj,'name')
# print(obj.__dict__)
'''
{}
'''
# class FtpClient:
# def __init__(self,host,port):
# self.host=host
# self.port=port
# self.conn='xxx'
#
# def interactie(self):
# while True:
# cmd=input('>>:').strip()
# if not cmd:
# continue
# cmd_l=cmd.strip()
# print(cmd_l)
# if hasattr(self,cmd_l[0]):
# func=getattr(self,cmd_l[0])
# func(cmd_l)
#
# def get(self,cmd_l):
# print('geting...',cmd_l)
#
# def put(self,cmd_l):
# print('putting...',cmd_l)

猜你喜欢

转载自www.cnblogs.com/0B0S/p/12098685.html
今日推荐