issubclass()
issubclass()检测一个类是否是另外一个类的子类
格式1:issubclass(被检测类,父类)
返回值:布尔值 格式 1:issubclass(被检测类,(父类1,父类2,父类3...))
返回值:布尔值
注意:只要有一个类是当前被检测的父类,那么最终结果就是True
class A: pass class B(A): pass class C(B): pass print(issubclass(C,A)) print(issubclass(C,A)) # 隔代继承也是子类 # True # True
isinstance(对象,类)
class A: pass class B(A): pass class C(B): pass c = C() b = B() a = A() print(isinstance(c,C)) print(isinstance(c,A)) print(isinstance(a,(B,A))) # True # True # True
反射方法
python 面向对象中的反射,通过字符串的形式操作对象相关的属性 python中的一切事物都是对象(都可以使用反射)
四个反射相关的函数
-hasattr:判断是否有此变量,返回bool值
-getattr:获取属性值或者获取方法变量的地址
-setattr:给类或者对象设置属性或者方法 (用的场合很少,了解即可)
-delattr:删除类或对象的属性或方法(用的场合很少,了解即可)
-反射类的属性和方法-反射对象的属性和方法 - 反射模块的属性和方法
getattr()获取对象/类中的成员值
格式3:getattr(对象,'属性名'[成员不存在时的默认值])
返回值:成员的值
class Teacher: dic = {'学生信息':'show_student','讲师信息':'show_teacher'} def __init__(self,name,age): self.name = name self.age = age @classmethod def func(cls): print("--func--") def show_student(self): print("--show student--") def show_teacher(self): print("--show teacher--") print(getattr(Teacher,'dic')) ret = getattr(Teacher,'func') ret() teacher = Teacher('may',22) #--func-- print(teacher.__dict__) #{'name': 'may', 'age': 22} # print(getattr(Teacher,'name')) print(getattr(Teacher,'__dict__')) print(getattr(Teacher,'show_student')) # {'学生信息': 'show_student', '讲师信息': 'show_teacher'} # # --func-- # # {'name': 'may', 'age': 22} # # {'__module__': '__main__', 'dic': {'学生信息': 'show_student', '讲师信息': 'show_teacher'}, '__init__': <function Teacher.__init__ at 0x0000021243FE3AE8>, 'func': <classmethod object at 0x0000021243FE8E48>, 'show_student': <function Teacher.show_student at 0x0000021243FE3BF8>, 'show_teacher': <function Teacher.show_teacher at 0x0000021243FE3C80>, '__dict__': <attribute '__dict__' of 'Teacher' objects>, '__weakref__': <attribute '__weakref__' of 'Teacher' objects>, '__doc__': None} # # <function Teacher.show_student at 0x0000021243FE3BF8>
hasattr()
检查对象/类是否具有某个成员
格式:hasattr (对象/类,'成员名')
返回值:布尔值
print(hasattr(Teacher,'dic')) print(hasattr(Teacher,'show_teacher'))
#True
#True
# 练习1:判断类中是否有dic静态属性 ret = hasattr(Teacher,'dic') if ret: print(getattr(Teacher,'dic')) else: print(False) # {'学生信息': 'show_student', '讲师信息': 'show_teacher'} # 练习2:根据用户输入的key来调用对应的方法。 key = input("请输入想调用的方法名:") ret1 = hasattr(teacher,teacher.dic.get(key,'')) print(ret1) if ret1: func = getattr(teacher,teacher.dic[key]) func() else: print("方法不存在") # 请输入想调用的方法名:学生信息 # True # --show student--
setattr()设置或添加对象/类中的成员
格式: setattr(对象,'成员名',值)
返回值:None
class A: pass setattr(A,'name','johnson') print(getattr(A,'name')) a = A() setattr(a,'name','john') print(getattr(a,'name')) # johnson # john
delattr() 删除对象/类中的成员
格式:delattr(对象,成员)
返回值:None
class A: pass setattr(A,'name','john') # delattr(A,'name') # print(getattr(A,'name')) a = A() setattr(a,'name','may') print(getattr(a,'name')) delattr(a,'name') print(getattr(a,'name')) # may # john
练习:
# 第一个参数是模块名称,第二个参数是变量名称 # 练习 1:反射 test 模块中的属性、函数和类 import test print(test.day1) print(getattr(test, 'day1')) #反射变量 ret = getattr(test,'func1') #反射函数 ret() A = getattr(test,'A') a = A() a.func2() #反射类中的函数 setattr(a,'name','zs') print(getattr(a, 'name')) # hahah # 周二 # 周二 # 这是func1函数 # 这是A中的func2函数 import random import time ret = getattr(random,'randint') print(ret(1, 10)) t = getattr(time,'time') print(t()) # zs # 6 # 1586960627.4621446
# 借助 sys 模块获取当前模块的名称。 # 练习 2:查看当前的所有模块 import sys print(sys.modules) name = "johnson" print(getattr(sys.modules["__main__"],'name')) # {'builtins': <module 'builtins' (built-in)>, 'sys': <module 'sys' (built-in)>, '_frozen_importlib': <module 'importlib._bootstrap' (frozen)>, '_imp': <module '_imp' (built-in)>, '_warnings': <module '_warnings' (built-in)>, '_thread': <module '_thread' (built-in)>, '_weakref': <module '_weakref' (built-in)>, '_frozen_importlib_external': <module 'importlib._bootstrap_external' (frozen)>, '_io': <module 'io' (built-in)>, 'marshal': <module 'marshal' (built-in)>, 'nt': <module 'nt' (built-in)>, 'winreg': <module 'winreg' (built-in)>, 'zipimport': <module 'zipimport' (built-in)>, 'encodings': <module 'encodings' from 'E:\programfiles\python\lib\encodings\__init__.py'>, 'codecs': <module 'codecs' from 'E:\programfiles\python\lib\codecs.py'>, '_codecs': <module '_codecs' (built-in)>, 'encodings.aliases': <module 'encodings.aliases' from 'E:\programfiles\python\lib\encodings\aliases.py'>, 'encodings.utf_8': <module 'encodings.utf_8' from 'E:\programfiles\python\lib\encodings\utf_8.py'>, '_signal': <module '_signal' (built-in)>, '__main__': <module '__main__' from 'E:/All_Project_Entry/Python/Python全面学习/3.类/22.类的常用函数.py'>, 'encodings.latin_1': <module 'encodings.latin_1' from 'E:\programfiles\python\lib\encodings\latin_1.py'>, 'io': <module 'io' from 'E:\programfiles\python\lib\io.py'>, 'abc': <module 'abc' from 'E:\programfiles\python\lib\abc.py'>, '_weakrefset': <module '_weakrefset' from 'E:\programfiles\python\lib\_weakrefset.py'>, 'site': <module 'site' from 'E:\programfiles\python\lib\site.py'>, 'os': <module 'os' from 'E:\programfiles\python\lib\os.py'>, 'errno': <module 'errno' (built-in)>, 'stat': <module 'stat' from 'E:\programfiles\python\lib\stat.py'>, '_stat': <module '_stat' (built-in)>, 'ntpath': <module 'ntpath' from 'E:\programfiles\python\lib\ntpath.py'>, 'genericpath': <module 'genericpath' from 'E:\programfiles\python\lib\genericpath.py'>, 'os.path': <module 'ntpath' from 'E:\programfiles\python\lib\ntpath.py'>, '_collections_abc': <module '_collections_abc' from 'E:\programfiles\python\lib\_collections_abc.py'>, '_sitebuiltins': <module '_sitebuiltins' from 'E:\programfiles\python\lib\_sitebuiltins.py'>, 'sysconfig': <module 'sysconfig' from 'E:\programfiles\python\lib\sysconfig.py'>, '_bootlocale': <module '_bootlocale' from 'E:\programfiles\python\lib\_bootlocale.py'>, '_locale': <module '_locale' (built-in)>, 'encodings.gbk': <module 'encodings.gbk' from 'E:\programfiles\python\lib\encodings\gbk.py'>, '_codecs_cn': <module '_codecs_cn' (built-in)>, '_multibytecodec': <module '_multibytecodec' (built-in)>, 'types': <module 'types' from 'E:\programfiles\python\lib\types.py'>, 'functools': <module 'functools' from 'E:\programfiles\python\lib\functools.py'>, '_functools': <module '_functools' (built-in)>, 'collections': <module 'collections' from 'E:\programfiles\python\lib\collections\__init__.py'>, 'operator': <module 'operator' from 'E:\programfiles\python\lib\operator.py'>, '_operator': <module '_operator' (built-in)>, 'keyword': <module 'keyword' from 'E:\programfiles\python\lib\keyword.py'>, 'heapq': <module 'heapq' from 'E:\programfiles\python\lib\heapq.py'>, '_heapq': <module '_heapq' (built-in)>, 'itertools': <module 'itertools' (built-in)>, 'reprlib': <module 'reprlib' from 'E:\programfiles\python\lib\reprlib.py'>, '_collections': <module '_collections' (built-in)>, 'weakref': <module 'weakref' from 'E:\programfiles\python\lib\weakref.py'>, 'collections.abc': <module 'collections.abc' from 'E:\programfiles\python\lib\collections\abc.py'>, 'importlib': <module 'importlib' from 'E:\programfiles\python\lib\importlib\__init__.py'>, 'importlib._bootstrap': <module 'importlib._bootstrap' (frozen)>, 'importlib._bootstrap_external': <module 'importlib._bootstrap_external' (frozen)>, 'warnings': <module 'warnings' from 'E:\programfiles\python\lib\warnings.py'>, 'importlib.util': <module 'importlib.util' from 'E:\programfiles\python\lib\importlib\util.py'>, 'importlib.abc': <module 'importlib.abc' from 'E:\programfiles\python\lib\importlib\abc.py'>, 'importlib.machinery': <module 'importlib.machinery' from 'E:\programfiles\python\lib\importlib\machinery.py'>, 'contextlib': <module 'contextlib' from 'E:\programfiles\python\lib\contextlib.py'>, 'backports': <module 'backports' from 'E:\programfiles\python\lib\site-packages\backports\__init__.py'>, 'sitecustomize': <module 'sitecustomize' from 'E:\Program Files\JetBrains\PyCharm 2019.2.1\helpers\pycharm_matplotlib_backend\sitecustomize.py'>, 'test': <module 'test' from 'E:\All_Project_Entry\Python\Python全面学习\3.类\test.py'>, 'random': <module 'random' from 'E:\programfiles\python\lib\random.py'>, 'math': <module 'math' (built-in)>, 'hashlib': <module 'hashlib' from 'E:\programfiles\python\lib\hashlib.py'>, '_hashlib': <module '_hashlib' from 'E:\programfiles\python\DLLs\_hashlib.pyd'>, '_blake2': <module '_blake2' (built-in)>, '_sha3': <module '_sha3' (built-in)>, 'bisect': <module 'bisect' from 'E:\programfiles\python\lib\bisect.py'>, '_bisect': <module '_bisect' (built-in)>, '_random': <module '_random' (built-in)>, 'time': <module 'time' (built-in)>} # johnson