support for built-in function vars()

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: support for built-in function vars()

Post by deshipu » Fri Sep 30, 2016 9:25 am

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.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: support for built-in function vars()

Post by pythoncoder » Fri Sep 30, 2016 4:35 pm

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.

amar123deep
Posts: 12
Joined: Tue Sep 27, 2016 2:29 pm

Re: support for built-in function vars()

Post by amar123deep » Mon Oct 03, 2016 1:39 pm

Thanks! we found a way to get around few libraries that were creating issues.

amar123deep
Posts: 12
Joined: Tue Sep 27, 2016 2:29 pm

Re: support for built-in function vars()

Post by amar123deep » Wed Oct 05, 2016 7:50 am

A module example.py looks like:

Code: Select all

class ExampleClass:
	pass
Now, I want to access the name of the class inside the module. In python 2.7, one can do:

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?

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: support for built-in function vars()

Post by deshipu » Wed Oct 05, 2016 10:36 am

Use getattr

amar123deep
Posts: 12
Joined: Tue Sep 27, 2016 2:29 pm

Re: support for built-in function vars()

Post by amar123deep » Wed Oct 05, 2016 11:32 am

There is no getattr for a module imported in micropython

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: support for built-in function vars()

Post by deshipu » Wed Oct 05, 2016 12:28 pm

Are you sure? Works for me...

Code: Select all

>>> import machine
>>> getattr(machine, 'Pin')
<class 'Pin'>

amar123deep
Posts: 12
Joined: Tue Sep 27, 2016 2:29 pm

Re: support for built-in function vars()

Post by amar123deep » Wed Oct 05, 2016 12:47 pm

well, I meant user-defined module where a class is declared

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: support for built-in function vars()

Post by deshipu » Wed Oct 05, 2016 1:44 pm

That also works:

Code: Select all

>>> import ssd1306
>>> getattr(ssd1306, 'SSD1306')
<class 'SSD1306'>

amar123deep
Posts: 12
Joined: Tue Sep 27, 2016 2:29 pm

Re: support for built-in function vars()

Post by amar123deep » Wed Oct 05, 2016 2:13 pm

Thanks :)

Post Reply