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
setattr creating duplicates for existing attributes
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: setattr creating duplicates for existing attributes
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.
Index to my micropython libraries.