Python multiple inheritance questions -



Python multiple inheritance questions -

sorry if question has been asked before, not find reply while searching other questions.

i'm new python , i'm having issues multiple inheritance. suppose have 2 classes, b , c, inherit same class a, defined follows:

class b(a): def foo(): ... homecoming def bar(): ... homecoming class c(a): def foo(): ... homecoming def bar(): ... homecoming

i want define class d, inherits both b , c. d should inherit b's implementation of foo, c's implementation of bar. how go doing this?

resisting temptation "avoid situation in first place", 1 (not elegant) solution wrap methods explicitly:

class a: pass class b( ): def foo( self ): print( 'b.foo') def bar( self ): print( 'b.bar') class c( ): def foo( self ): print( 'c.foo') def bar( self ): print( 'c.bar') class d( b, c ): def foo( self ): homecoming b.foo( self ) def bar( self ): homecoming c.bar( self )

alternatively can create method definitions explicit, without wrapping:

class d( b, c ): foo = b.foo bar = c.bar

python inheritance multiple-inheritance

Comments

Popular posts from this blog

c# - ASP.NET MVC Sequence contains no matching element -

java - Parsing XML, skip certain tags -

rest - How to invalidate user session on inactivity in a stateless server? -