Stay Hungry,Stay Foolish!

python中的反射

cProfile.run('re.compile("foo|bar")', 'restats')

https://docs.python.org/3/library/profile.html

import cProfile
import re
cProfile.run('re.compile("foo|bar")')

 

这段python的闪耀点是环境感知能力

 

exec

查看实现, 其调用的是python的built-in 接口 exec

https://docs.python.org/3/library/functions.html#exec

 

Python's exec(): Execute Dynamically Generated Code

https://realpython.com/python-exec/

 

Java 反射(Reflection)

类似java中的发射机制

https://www.runoob.com/java/java-reflection.html

 

python中也有发射

https://www.cnblogs.com/vipchenwei/p/6991209.html

 

class A: 
def __init__(self): 
self.name = 'zhangjing'
#self.age='24'
def method(self): 
print"method print"
  
Instance = A() 
print getattr(Instance , 'name, 'not find') #如果Instance 对象中有属性name则打印self.name的值,否则打印'not find'
print getattr(Instance , 'age', 'not find') #如果Instance 对象中有属性age则打印self.age的值,否则打印'not find'
print getattr(a, 'method', 'default') #如果有方法method,否则打印其地址,否则打印default 
print getattr(a, 'method', 'default')() #如果有方法method,运行函数并打印None否则打印default 

 

posted @ 2024-12-11 21:28  lightsong  阅读(1)  评论(0编辑  收藏  举报
Life Is Short, We Need Ship To Travel