# class A: # pass # class B(A): # pass # a = A() # print(isinstance(a,A)) # print(issubclass(B,A)) # 反射:是用字符串类型的名字去操作变量 # eval('print(">>>")') # 安全隐患很大 # 反射 就没有安全问题 # hasattr getattr setattr ddelattr # 反射对象中的属性和方法 # class A: # price = 10 # @classmethod # def func(cls): # print('>>>>>func') # a = A() # a.name = 'Mr.Feng' # # 反射对象的属性: # shuxing = input('>>>') # ret = getattr(a,shuxing) # print(ret) # 反射对象的方法 # b = input('>>>>>') # if hasattr(a,b): # getattr(a,b)() # 反射类的属性 # A.price # print(getattr(A,'price')) # 反射类的方法:classmethod staticmethod # if hasattr(A,'func'): # getattr(A,'func')() # else: # print('参数错误') # 模块 # import my_mode # 反射模块的属性 # print(my_mode.day) # # getattr(my_mode,'day') # 反射模块的属性 # getattr(my_mode,'wahaha')() # 内置模块也可以使用 # import sys # year = 2019 # def huoying(): # print('huoying') # 反射自己模块中的变量 # getattr(sys.modules[__name__],'year') # 反射自己模块中的模块 # getattr(sys.modules[__name__],'huoying')() # ret = input('>>>') # print(getattr(sys.modules[__name__],ret)) # import time # a = input('>>>') # # print(getattr(time,a)()) # print(time.strftime('%Y-%m-%d')) # print(getattr(time,a)('%Y-%m_%d')) # 反射一个模块中的类 import my_mode # print(getattr(my_mode,'A')()) # setattr 设置/修改变量 # class A: # pass # a = A() # setattr(A,'name','feng') # print(A.name) # setattr(a,'name','zhen') # print(a.name) # # delattr 删除变量 # delattr(a,'name') # print(a.name) # delattr(A,'name') # print(a.name)
反射——重要指数************************************(满天星)
猜你喜欢
转载自www.cnblogs.com/Mr-Feng/p/10855621.html
今日推荐
周排行