support for built-in function vars()
Re: support for built-in function vars()
That decorator uses a mechanism called descriptors, which is not implemented in MicroPython for performance reasons. You will have to write your own decorator and make it a little bit less magical.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: support for built-in function vars()
Function attributes are a seldom used feature of Python - a feature not supported by MicroPython. I suggest replacing the function with a class of the same name having a __call__() method as a workround.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
-
- Posts: 12
- Joined: Tue Sep 27, 2016 2:29 pm
Re: support for built-in function vars()
Thanks! we found a way to get around few libraries that were creating issues.
-
- Posts: 12
- Joined: Tue Sep 27, 2016 2:29 pm
Re: support for built-in function vars()
A module example.py looks like:
Now, I want to access the name of the class inside the module. In python 2.7, one can do:
Any idea how it can be done in micropython?
Code: Select all
class ExampleClass:
pass
Code: Select all
>>> import example
>>> name = 'ExampleClass'
>>> example.__dict__.get(name, None)
<class example.ExampleClass at 0x7f2ed0c492c0>
Any idea how it can be done in micropython?
Re: support for built-in function vars()
Use getattr
-
- Posts: 12
- Joined: Tue Sep 27, 2016 2:29 pm
Re: support for built-in function vars()
There is no getattr for a module imported in micropython
Re: support for built-in function vars()
Are you sure? Works for me...
Code: Select all
>>> import machine
>>> getattr(machine, 'Pin')
<class 'Pin'>
-
- Posts: 12
- Joined: Tue Sep 27, 2016 2:29 pm
Re: support for built-in function vars()
well, I meant user-defined module where a class is declared
Re: support for built-in function vars()
That also works:
Code: Select all
>>> import ssd1306
>>> getattr(ssd1306, 'SSD1306')
<class 'SSD1306'>
-
- Posts: 12
- Joined: Tue Sep 27, 2016 2:29 pm