Multiple inheritance in python3 with micropython

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
316meng
Posts: 4
Joined: Mon Apr 18, 2016 7:33 am

Multiple inheritance in python3 with micropython

Post by 316meng » Mon May 02, 2016 3:51 pm

[code]class A(object):
def __init__(self, a=None, b=None, *args, **kwargs):
super().__init__(*args, **kwargs)
print('Init {} with arguments {}'.format(self.__class__.__name__, (a, b)))
print ('A')

class B(object):
def __init__(self, q=None, *args, **kwargs):
super().__init__(*args, **kwargs)
print('Init {} with arguments {}'.format(self.__class__.__name__, (q)))
print ('B')

class C(A, B):
def __init__(self):
super().__init__()
print ('C')
[/code]
What I got is
[code]Init C with arguments (None, None)
A
C[/code]

So the question is why class B 's fun __init__() is not be called.
How can I correct the code??

316meng
Posts: 4
Joined: Mon Apr 18, 2016 7:33 am

Re: Multiple inheritance in python3 with micropython

Post by 316meng » Tue May 03, 2016 1:20 am

Is it a bug that micropython can't use super()?

Code: Select all

class A:
    def __init__(self):
        print("A.__init__")

class B(A):
    def __init__(self):
        print("B.__init__")
        super().__init__()
    
class C(A):
    def __init__(self):
        print("C.__init__")
        super().__init__()


class D(B,C):
    def __init__(self):
        print("D.__init__")
        super().__init__()

D()
when I use micropython I get

Code: Select all

D.__init__
B.__init__
A.__init__
MicroPython v1.5.1-58-gb4eccfd on 2015-12-03; PYBv1.0 with STM32F405RG
Type "help()" for more information.
But if I run this on my pc I get

Code: Select all

D.__init__
B.__init__
C.__init__
A.__init__
why this happen??

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

Re: Multiple inheritance in python3 with micropython

Post by pythoncoder » Tue May 03, 2016 5:36 am

I think you've hit a bug in Micropython. I'll raise an issue on Github.

Incidentally when posting code to the forum it's worth using the Code button: this preserves indentation making the code easier to read. It also makes it easier to cut and paste when we want to try it ;)
Peter Hinch
Index to my micropython libraries.

Post Reply