AttributeError: 'function' object has no attribute '__name__

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
winneymj13
Posts: 2
Joined: Sat Nov 10, 2018 2:19 am

AttributeError: 'function' object has no attribute '__name__

Post by winneymj13 » Wed Apr 06, 2022 10:03 pm

Hi,
I am running micropython on my ESP32_S2 Saola board and I am trying determine the name of a function passed as a parameter to another function (to be used as a callback).
Produces error: AttributeError: 'function' object has no attribute '__name__'
How do I retrieve the name of the function passed? If I do a 'print(func)' it prints the name of the function to console, so it is known somewhere.
As can be seen way below in full example I am trying to add a new method to a class using a @add_method decorator and it works.....except the I had to hardcode the function name 'foo'.

Code: Select all

setattr(cls, 'foo', wrapper)
What I really want to do is:

Code: Select all

setattr(cls, func.__name__, wrapper)
Full code example:

Code: Select all

def add_method(cls):
    def decorator(func):
        @wraps(func)
        def wrapper(self, *args, **kwargs):
            return func(*args, **kwargs)

        # print(func)
        # print(sys.modules[func.__globals__['__name__']])
        setattr(cls, 'foo', wrapper) # Hardcoded function name 'foo'...Not ideal
        # setattr(cls, func.__name__, wrapper)
        # Note we are not binding func, but wrapper which accepts self but does exactly the same as func
        return func # returning func means func can still be used normally
    return decorator

class A:
    pass

# No trickery. Class A has no methods nor variables.
a = A()
try:
    a.foo()
except AttributeError as ae:
    print(f'Exception caught: {ae}') # 'A' object has no attribute 'foo'

# Decorator can be written to take normal functions and make them methods
@add_method(A)
def foo():
    print('hello world!')

a.foo()

tepalia02
Posts: 99
Joined: Mon Mar 21, 2022 5:13 am

Re: AttributeError: 'function' object has no attribute '__name__

Post by tepalia02 » Sat Apr 09, 2022 10:46 am

Hi, can't exactly say what is the problem here. But I have seen this error usually occurs when a python package is not installed but imported into the code. Kindly check if you have installed all the packages that you're using.

Nicolett
Posts: 7
Joined: Mon Apr 11, 2022 1:16 pm

Re: AttributeError: 'function' object has no attribute '__name__

Post by Nicolett » Thu Apr 14, 2022 7:37 am

Have you tried to ask this question on Stackoverflow?

nekomatic
Posts: 37
Joined: Thu May 08, 2014 9:31 pm

Re: AttributeError: 'function' object has no attribute '__name__

Post by nekomatic » Thu Apr 14, 2022 8:45 am

Haha, mostly what I do when answering MicroPython questions on stackoverflow is suggest people come here.

I'm not familiar with defining decorators (I'm sure it's something I should learn) but is this an example of User-defined attributes for functions are not supported?

Post Reply