Python装饰器函数在编译时调用 [英] Python decorator function called at compile time
问题描述
在我的示例代码中,我在定义logtofile闭包之前在writeit装饰器中包含了一个测试打印语句。如果你运行我提供的整个代码,在writeit中的测试打印语句被调用为在Customer类中定义的每个@writeit装饰器 - 在writeit被使用之前。
为什么在编译时调用logtofile?有人可以解释这种行为吗?
def writeit(func):
print('testing')
def logtofile (customer,* arg,** kwargs):
print('logtofile')
result = func(customer,* arg,** kwargs) ,'w')as myfile:
myfile.write(func .__ name__)
返回结果
返回logtofile
类Customer
def __init __(self,firstname,lastname,address,city,state,zipcode):
self._custinfo = dict(firstname = firstname,lastname = lastname,address = address,city = city,state = state,zipcode = zipcode)
@writeit
def setFirstName(self,firstname):
print('setFirstName')
self._custinfo ['firstname'] = firstname
@writeit
def setLastName(self,lastname):
print('setLastName')
self._custinfo ['lastname'] = lastname
@writeit
def setAddress(self,address):
print('setAddress')
self._custinfo ['address'] = address
def main():
cust1 = Customer('Joe','Shmoe','123 Washington','Washington DC','DC','12345')
cust1.setFirstName Joseph')
cust1.setLastName('Shmoestein')
if(__ name__ =='__main__'):main()
您的代码在导入模块时运行。 Python执行所有顶级语句,包括那时的类定义。
类定义体作为一个函数执行,局部命名空间成为类属性。
当Python在执行时遇到一个修饰的函数时,它会在模块的顶层定义类。 ll定义类,然后执行装饰器函数,传递函数对象并将装饰器的返回值绑定到函数的名称。由于类体在导入期间执行,这意味着你的装饰器在那时被执行。
I hope that someone familiar with Python's compilation / run-time procedures could shed some light on my question relating to how Python compiles decorator functions.
Within my sample code, I've included a testing print statement in the "writeit" decorator just before the logtofile closure is defined. If you run the entire code that I've provided, the "testing" print statement in writeit is called for each @writeit decorator defined in the Customer class-- before writeit is ever used.
Why is logtofile being called at compile time? Could someone please explain this behavior?
def writeit(func):
print('testing')
def logtofile(customer, *arg, **kwargs):
print('logtofile')
result = func(customer, *arg, **kwargs)
with open('dictlog.txt','w') as myfile:
myfile.write(func.__name__)
return result
return logtofile
class Customer(object):
def __init__(self,firstname,lastname,address,city,state,zipcode):
self._custinfo = dict(firstname=firstname,lastname=lastname,address=address,city=city,state=state,zipcode=zipcode)
@writeit
def setFirstName(self,firstname):
print('setFirstName')
self._custinfo['firstname']=firstname
@writeit
def setLastName(self,lastname):
print('setLastName')
self._custinfo['lastname']=lastname
@writeit
def setAddress(self,address):
print('setAddress')
self._custinfo['address']=address
def main():
cust1 = Customer('Joe','Shmoe','123 Washington','Washington DC','DC','12345')
cust1.setFirstName('Joseph')
cust1.setLastName('Shmoestein')
if(__name__ == '__main__'): main()
Your code runs when the module is imported. Python executes all top-level statements, including class definitions at that time.
A class definition body is executed as a function, with the local namespace becoming the class attributes. This means that class bodies are executed on import, provided the class is defined at the top-level of the module.
When Python encounters a decorated function when executing, it'll define the class, then execute the decorator function, passing in the function object and binding the return value of the decorator to the name of the function. Since the class body is executed during import, this means your decorator is executed at that time.
这篇关于Python装饰器函数在编译时调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!