14

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.

2
  • 1
    are you looking for callback.__name__?? Honestly, if you are just going to print it, you might as well just print callback... Commented Feb 7, 2020 at 6:15
  • It works, thank you Commented Feb 7, 2020 at 6:32

1 Answer 1

15

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.