python 编译函数_Python编译器函数在编译时调用

我希望熟悉

Python的编译/运行时程序的人可以解释一下我关于Python如何编译装饰器函数的问题.

在我的示例代码中,我在定义logtofile闭包之前在“writeit”装饰器中包含了一个测试print语句.如果你运行我提供的整个代码,那么在使用writeit之前,为Customer类中定义的每个@writeit装饰器调用writeit中的“testing”print语句.

为什么在编译时调用logtofile?有人可以解释一下这种行为吗?

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值