I downloaded the following code :
from __future__ import print_function
from time import sleep
def callback_a(i, result):
print("Items processed: {}. Running result: {}.".format(i, result))
def square(i):
return i * i
def processor(process, times, report_interval, callback):
print("Entered processor(): times = {}, report_interval = {}, callback = {}".format(
times, report_interval, callback.func_name))
# Can also use callback.__name__ instead of callback.func_name in line above.
result = 0
print("Processing data ...")
for i in range(1, times + 1):
result += process(i)
sleep(1)
if i % report_interval == 0:
# This is the call to the callback function
# that was passed to this function.
callback(i, result)
processor(square, 20, 5, callback_a)
It works fine under python 2, but I get the following error under python3:
Traceback (most recent call last):
File "test/python/cb_demo.py", line 33, in <module>
processor(square, 20, 5, callback_a)
File "test/python/cb_demo.py", line 21, in processor
times, report_interval, callback.func_name))
AttributeError: 'function' object has no attribute 'func_name'
I need to work under python3.
That behaviour in Python 3 is expected as it was changed from Python 2. Per the documentation here:
https://docs.python.org/3/whatsnew/3.0.html#operators-and-special-methods
The function attributes named func_X have been renamed to use the
__X__
form, freeing up these names in the function attribute namespace for user-defined attributes. To wit, func_closure, func_code, func_defaults, func_dict, func_doc, func_globals, func_name were renamed to__closure__
,__code__
,__defaults__
,__dict__
,__doc__
,__globals__
,__name__
, respectively.
You will notice the mention of func_name
as one of the attributes that were renamed. You will need to use __name__
.
Sample code in Python 3:
>>> def foo(a):
... print(a.__name__)
...
>>> def c():
... pass
...
>>>
>>> foo(c)
c
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With