super() seems to work differently on the pyboard than on the unix port of micropython. I have a file, foo.py, which has two classes derived from bytearray. The first ("A") works on Cpython and on the unix port of micropython but not on the pyboard. The second ("B") works on the pyboard. My question is, how is maxchars begin passed to bytearray.__init__? And is this behavior a bug I should report?
Note: the leading periods in the example should be spaces, but apparently BBcode is disabled for me.
foo.py:
-----------------------------------------------------------
__ALL__ = ("A")
class A(bytearray):
...def __init__(self,maxchars):
......super().__init__(maxchars)
class B(bytearray):
...def __init__(self,maxchars=0):
......super().__init__()
w = B()
x = B(10)
-----------------------------------------------------------
Entered at REPL:
>>> import foo
>>> len(foo.w)
0
>>> len(foo.x)
10
>>> q = foo.A(3)
q = foo.A(3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "foo.py", line 5, in __init__
TypeError: function takes 1 positional arguments but 2 were given
Do I just misunderstand super() on the pyboard?
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Do I just misunderstand super() on the pyboard?
See differences implementation differences (3.).
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Do I just misunderstand super() on the pyboard?
But shouldn't the unix micropython still behave the same as the pyboard one?
Re: Do I just misunderstand super() on the pyboard?
If it were me, I would report it. The variance between pyboard and Unix in such a fundamental behaviour as argument-passing in inheritance is surprising and I suspect could either be fixed or well-documented. If the inheritance strategy of a core object is fully invalid in Micropython because optimisations prevent it from behaving as expected, perhaps it should fail. The note in 3. wouldn't suggest to me that this would take place.
Re: Do I just misunderstand super() on the pyboard?
Thanks for the thoughts--I will put together a bug report and submit it.
Re: Do I just misunderstand super() on the pyboard?
Okay, I checked the bug reports (which I should have done first) and find that it was already reported and closed without explanation. The fact that it's different on the pyboard and the unix port suggests to me that it is a bug, but I can work around it now that I know it's there.
The earlier bug report is at https://github.com/micropython/micropython/issues/3554 .
Thanks again for the responses.
The earlier bug report is at https://github.com/micropython/micropython/issues/3554 .
Thanks again for the responses.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Do I just misunderstand super() on the pyboard?
In my opinion if you attempt to use an unsupported feature all bets are off as to behaviour.
However subclassing built-in types is a widely used technique. So this issue regularly crops up. I think attempts to instantiate such a class should throw a runtime exception. Perhaps an issue should be raised to that effect?
However subclassing built-in types is a widely used technique. So this issue regularly crops up. I think attempts to instantiate such a class should throw a runtime exception. Perhaps an issue should be raised to that effect?
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.