Get Attribute of a Class

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
okay1984
Posts: 3
Joined: Sat May 20, 2017 3:41 pm

Get Attribute of a Class

Post by okay1984 » Sat May 20, 2017 3:53 pm

def add_lookup(cls):
varnames = filter(str.isupper, cls.__dict__.keys())
lookup = dict(map(lambda varname: (cls.__dict__.get(varname, None), varname), varnames))
setattr(cls, 'lookup', lookup)
return cls

Guys,

Please help me to convert this function for MicroPython. Thanks a lot. There is no __dict__ function built in.

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

Re: Get Attribute of a Class

Post by pythoncoder » Sun May 21, 2017 7:37 am

MicroPython lacks some of the "introspection" capabilities of CPython in the interests of keeping it "Micro". See https://github.com/micropython/micropyt ... ifferences.

You might like to experiment with:

Code: Select all

def add_lookup(cls):
    varnames = filter(str.isupper, dir(cls))
    lookup = dict(map(lambda varname: (getattr(cls, varname), varname), varnames))
    setattr(cls, 'lookup', lookup)
    return cls
Peter Hinch
Index to my micropython libraries.

okay1984
Posts: 3
Joined: Sat May 20, 2017 3:41 pm

Re: Get Attribute of a Class

Post by okay1984 » Tue May 23, 2017 3:50 am

Thank you Peter, appreciated.

Post Reply