Traceback (most recent call last): File "C:/Users/Sure/PyProject/day13/exercise.py", line 100, in <module> f.__closure__ AttributeError: 'NoneType'object has no attribute '__closure__'
错误代码:
1 2 3 4 5 6
deffunc(): a = 10 deffoo(): print(a) f = func() f.__closure__
错误原因:
测试闭包的时候,没有在外层函数中将内层函数返回就强行调用,所以报错。
解决方法:
把测试闭包的方法放到函数里面,不要忘记在外面调用函数,否则函数中的代码是不会被运行的。
1 2 3 4 5 6
deffunc(): a = 10 deffoo(): print(a) print(foo.__closure__) func()
补充:
外层函数中不将内层函数返回也可以构成闭包,但是这个闭包无法被使用。
module ‘bake.api’ has no attribute ‘policy’
错误2.1
报错信息:
1 2 3 4
Traceback (most recent call last): File "C:/Users/Sure/PyProject/bake/test.py", line 21, in <module> print(api.policy.name) AttributeError: module 'bake.api' has no attribute 'policy'
Traceback (most recent call last): File "C:/Users/Sure/PyProject/week06/day22/exercise.py", line 7, in <module> print(p1.__name) AttributeError: 'Person'object has no attribute '__name'
错误代码:
1 2 3 4 5
classPerson: def__init__(self, name): self.__name = name p1 = Person('迪丽热巴') print(p1.__name)
Traceback (most recent call last): File "C:/Users/Sure/PyProject/week08/day32/exercise.py", line 46, in <module> with A('大爷') as a: AttributeError: __enter__
错误代码:
1 2 3 4 5
classA: def__init__(self, text): self.text = text with A('大爷') as a: print(a)