Python 如何保留或识别呼叫者';什么是堆栈帧?
今天我的大脑感觉迟钝 我正在使用修饰符用Python编写前置/后置/不变量。目前,我需要每次调用为上下文指定局部变量和全局变量,这感觉很糟糕。有没有一种方法可以从decorator应用程序级别获取局部变量和全局变量,即使它是任意深度的 也就是说,我正在尝试制作这个丑陋的代码: 从dectools导入不变量、前置、后置、调用Python 如何保留或识别呼叫者';什么是堆栈帧?,python,stack,decorator,Python,Stack,Decorator,今天我的大脑感觉迟钝 我正在使用修饰符用Python编写前置/后置/不变量。目前,我需要每次调用为上下文指定局部变量和全局变量,这感觉很糟糕。有没有一种方法可以从decorator应用程序级别获取局部变量和全局变量,即使它是任意深度的 也就是说,我正在尝试制作这个丑陋的代码: 从dectools导入不变量、前置、后置、调用 @invariant("self.price >= 0 and self.inventory >= 0 and Item.tax_rate >= 0") c
@invariant("self.price >= 0 and self.inventory >= 0 and Item.tax_rate >= 0")
class Item(object):
tax_rate = 0.10 # California. No property taxes on old property.
@post("ItemDB.fetch(self) = (name, price)", locals(), globals())
def __init__(self, name, price):
self.name = name
self.price = price
self.total_sold = 0
self.inventory = 0
@call_if(check_level, "manager")
@post("self.total_sold > 0", locals(), globals())
@pre("discount > 0 and discount <= self.price * 0.50", locals(), globals())
def adjust_price(self, adjustment):
....
@不变量(“self.price>=0,self.inventory>=0,Item.tax\u rate>=0”)
类别项目(对象):
税率=0.10#加利福尼亚州。对旧房产不征收房产税。
@post(“ItemDB.fetch(self)=(名称,价格)”,locals(),globals()
定义初始(自身、名称、价格):
self.name=名称
self.price=价格
self.total_selled=0
self.inventory=0
@如果需要,请致电(检查“经理”级别)
@post(“self.total_selled>0”、locals()、globals())
@pre(“折扣>0和折扣如果你可以访问self.total\u salled
,你可以访问self.tax\u rate
(这与项目.tax\u rate
相同,除非你踩它——所以你只是不踩它,保持税率作为一个原始的类变量,并通过self.
!-)。这比在堆栈中捣乱要可靠得多,尤其是在图片中使用嵌套的装饰器时,它或多或少地保证了脆弱的、特定的版本相关代码(堆栈内省本质上是用于调试目的,而不是用于生产目的)