content of an object in Micropython

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
klik
Posts: 5
Joined: Thu Jan 08, 2015 8:37 am

content of an object in Micropython

Post by klik » Wed Jul 01, 2015 7:19 am

Hi everyone,

i have a small question, not a big deal but python2.7 (or python3.4) gives a different result than Micropython.


python3.4
Python 3.4.0 (default, Jun 19 2015, 14:20:21)
[GCC 4.8.2] on linux

Code: Select all

class A:
    def __init__(self):
        self.x = 10
        self.y = 10

a = A()
dir(a)
and the result

Code: Select all

['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'x', 'y']
I see the my class variables, x and y.

in Micropython
same code ... but the result

Code: Select all

['__init__', '__qualname__', '__module__']
x and y are not listed.

Any idea?
thx

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

Re: content of an object in Micropython

Post by pythoncoder » Sat Jul 04, 2015 6:49 am

I think this is a feature of MicroPython being "micro". See https://github.com/micropython/micropyt ... ifferences from which I quote:
MicroPython supports only minimal subset of introspection and reflection features
Peter Hinch
Index to my micropython libraries.

klik
Posts: 5
Joined: Thu Jan 08, 2015 8:37 am

Re: content of an object in Micropython

Post by klik » Thu Jul 09, 2015 9:24 am

So everything is fine. Thank you for the information.
But it's strange that a function in the class is displayed with dir() :roll:

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: content of an object in Micropython

Post by dhylands » Thu Jul 09, 2015 4:00 pm

klik wrote:So everything is fine. Thank you for the information.
But it's strange that a function in the class is displayed with dir() :roll:
Well the function is also shown in CPython. What's missing from MicroPython are a & b.

I suspect that __init__ is being stored in the class object and that a & b are being stored in the instance and that dir isn't looking at the instance (or something like that).

Post Reply