setattr creating duplicates for existing attributes

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
k4r1
Posts: 1
Joined: Mon Jan 15, 2018 8:58 pm

setattr creating duplicates for existing attributes

Post by k4r1 » Mon Jan 15, 2018 9:04 pm

Why does setattr behave differently on micropython to python3 ?

File: intropsect.py

#!/usr/bin/python3

class A():
one = "1"
two = "2"
def x():
pass

class B():
two = "ii"
three = "iii"
def y():
pass

a = A()
b = B()

for attr in dir(a):
if not attr.startswith('__') and not callable(getattr(a,attr)):
print("a=",attr,getattr(a,attr))
setattr(b,attr,getattr(a,attr))

for attr in dir(b):
if not attr.startswith('__') and not callable(getattr(b,attr)):
print("b=",attr,getattr(b,attr))


pi@raspberrypi:~/working $ ampy --port /dev/ttyUSB0 run ./temp/introspect.py
a= one 1
a= two 2
b= three iii
b= two 2
b= two 2
b= one 1
pi@raspberrypi:~/working $ ./temp/introspect.py
a= one 1
a= two 2
b= one 1
b= three iii
b= two 2

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

Re: setattr creating duplicates for existing attributes

Post by pythoncoder » Tue Jan 16, 2018 9:47 am

See my response on GitHub. tl;dr I think it's a minor difference in the way dir() reports the case where a class variable has the same name as an instance variable.
Peter Hinch
Index to my micropython libraries.

Post Reply