Workaround available to do reflection? Functions don't appe

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
whogben
 
Posts: 13
Joined: Thu Jul 04, 2019 4:28 pm

Workaround available to do reflection? Functions don't appe

Post by whogben »

I have a project I am trying to make work in both circuit python and regular, and it relies quite heavily on reflection. I just went to run it on circuit python and discovered functions don't have __name__s, (I'm also going to need __doc__ and __class__ etc)

Are these attributes stripped out somehow, and is there a way I can get them back in, or another workaround?

I need to be able to inspect the names of functions, their arguments, etc. Thanks for taking a look!

Code to replicate:

Code: Select all

def a_function():
    pass
print(a_function.__name__)
Result on running:

Code: Select all

Traceback (most recent call last):
  File "code.py", line 7, in <module>
AttributeError: 'function' object has no attribute '__name__'

User avatar
whogben
 
Posts: 13
Joined: Thu Jul 04, 2019 4:28 pm

Re: Workaround available to do reflection? Functions don't

Post by whogben »

From Neradoc on the discord:
remember that it's a microcontroller, a very constrained platform. `__name__` is actually available via the `MICROPY_PY_FUNCTION_ATTRS` compilation flag and is enabled on some ports, in a not-very-consistent way: it's currently disabled by default, enabled on nrf, stm, samd except samd21 (it could probably be enabled on rp2040 and ESP without issue)
I’m targeting a Feather S2 so a custom compilation with the function attrs (and presumably other items) seems like it will work.

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”