FAQ Database Discussion Community


Python Multiple Inheritance: Argument passing (**kwargs) and super()

python,inheritance,python-3.x,multiple-inheritance
I am trying to understand Python multiple inheritance and I kind of understand MRO, super() and passing arguments in MI, but while I was reading the below example it kind of confused me. class Contact: all_contacts = [] def __init__(self, name=None, email=None, **kwargs): super().__init__(**kwargs) self.name = name self.email = email...

Am I in a specific case justifying multiple inheritance?

c++,inheritance,multiple-inheritance
Currently, I have the following classes managing different kinds of variables : class Variable; class Number : public Variable; class Boolean : public Variable; class RealNumber : public Number; class IntegerNumber : public Number; This is a classical inheritance tree, and it works well. I manage vectors of Number* instances...

Why does this work? (multiple inheritance, slicing)

c++,multiple-inheritance,object-slicing
Consider this example: #include <iostream> using namespace std; class A { public: int x; }; class B { public: int y; B() { y = 0; } B(int var): y(var) {} }; class C : public A, public B { public: void assignB(B x) { *(B *)this = x; //...

Why is repeated inheritance / duplicate inheritance invalid?

c++,inheritance,multiple-inheritance,language-lawyer
This code is invalid: struct Agent {}; struct DoubleAgent : public Agent, public Agent {}; Because: > g++ -c DoubleAgent.cpp DoubleAgent.cpp:2:43: error: duplicate base type ‘Agent’ invalid struct DoubleAgent : public Agent, public Agent {}; ^ Why? I don't think this violates the concept of inheritance; if a class can...

Is that OK to use the MRO in order to override a mixin?

python,python-3.x,multiple-inheritance,mixins
Problem description: I have a class C inheriting from mixins A and B. I want a new class, C_, having all the methods/attributes defined in the class C but with B swapped with B_ (same API) in the inheritance scheme (one possible use of this is easy mocking). All classes...

How to best implement this Ruby code in Python

python,ruby,multiple-inheritance,mixins
Ruby code: module ToFile def filename "object_#{self.object_id}.txt" end def to_f File.open(filename, 'w') { |f| f.write(to_s) } end end class Person include ToFile attr_accessor :name def initialize(name) @name = name end def to_s name end end my Python code class ToFile: def __init__(self): self.filename = "object_#{0}.txt".format(id(self)) def to_f(self): with open(self.filename, 'w')...

memory layout of a multiple-inherited object in C++

c++,compiler-construction,multiple-inheritance
class B1 { virtual void f1(); int int_in_b1; }; class B2 { virtual void f2(); int int_in_b2; }; class D: B1, B2 { int int_in_d; void f1(); void f2(); }; class D1: B1, B2 { int int_in_d; virtual void f1(); virtual void f2(); }; Based on this article, the memory...

Implementing a method of interface is overriding or not in java

java,interface,polymorphism,multiple-inheritance,method-overriding
I know this might be crazy but today one of my friend puzzled by asking when we implement an interface in java is it considered as method overriding. I told him it is not overriding as we are providing working(definition) of method first time when we implement any interface. To...

Multiple inheritance difference between Python 2.7 and 3

python,python-2.7,python-3.x,multiple-inheritance
I've already got that there is a difference between Python 2.7 and 3. implementing multiple inheritance, for example: In Python 3.: class A: def __init__(self, x2='', x3='', **kwargs): print kwargs super().__init__(**kwargs) self.x2 = x2 self.x3 = x3 class B: def __init__(self, x4='', x5='', **kwargs): print kwargs super().__init__(**kwargs) self.x4 = x4...

PHP Sibling Class Inheritance

php,extension-methods,multiple-inheritance
I'm a bit confused on whether or not this is possible. I've checked a couple of posts here on SO and they don't really explain what I'm looking for. I have 3 classes. One main class and two classes extending that main class. (see code below). Is it possible to...

Pythonic Approach to Multiple Parent Method Calls

python,multiple-inheritance
Suppose I have the following class structure: class Mixin1(Base1): def get_data(self): # Gather some data in some way class Mixin2(Base2): def get_data(self): # Gather some data in another way class MyClass(Mixin1, Mixin2): def get_data(self): # Gather some data in yet another way. Then... # Get Mixin2's version of get_data. Then......

Access specific child class functions whose objects are in a template class vector

c++,templates,vector,subclass,multiple-inheritance
I'm new here and I hope you can help me! :) My problem is the following: First, I have this class diagram: diagram Additionally, I have another class Rocket which has a vector that should contain all the rocket-parts as pointers that I generate. My problem is that the Rocket...

When using inheritance is it necessary to “#include” the header file of child's parent class. C++

c++,header-files,multiple-inheritance
I'm working on a multiple inheritance project in C++. I have separate files for class definitions (.h) and implementations (.cpp). Since this is the first time I've used inheritance in C++ (the rest of our assignments have been in C#) somethings are a little unclear. Do I need to #include...

Python ABC Multiple Inheritance

python,python-2.7,multiple-inheritance,metaclass,urwid
I think the code will explain the problem better than I can do with words. Here is the code in my_abc.py: from abc import ABCMeta, abstractmethod class MyABC(objec): __metaclass__ = ABCMeta @abstractmethod def print(self): pass Here is the code in my_class.py from my_abc import MyABC from third_party_package import SomeClass class...

Invalid covariant type with CRTP clonable class

c++,multiple-inheritance,crtp,cloneable,covariant
I'm trying to implement a Clonable class with the CRTP. However, I need to have abstract class that have a pure virtual clone method, overridden by child classes. To make this happen, I need the clone function to return a covariant return type. I made this code below, and the...

Multiple inheritance with no function overriding. Why the ambiguity

c++,multiple-inheritance
Here are my classes. This is just #include<iostream> using namespace std; class Base { public: int var; Base(int val) : var(val) {} void foo() { cout << "Class Base foo()" << endl; } }; class Derived_A : public Base { public: Derived_A(int val) : Base(val) {} }; class Derived_B :...

Inherit two classes - Javascript

javascript,inheritance,multiple-inheritance,easeljs,createjs
I'm creating a new class definition Button that extends an existing Container class in EaselJS. There are no problems with that. However, I'd also like Button to inherit from a super class All, so that Button also has access to its .banana and .eatBanana. How do I go about this?...

Ambiguous inheritance of function when multiple inheritance of classes that themselves have diamond inheritance in their hierarchy

c++,inheritance,multiple-inheritance,virtual-inheritance,msvc12
Word description (code below): I have a library that provides a collection of classes. For each group of class we have two concrete types, (ClassA_Partial, ClassA), (ClassB_Partial, ClassB) etc. Each of these implements (Interface_Partial, Interface) respectively. Additionally, Interface is a Interface_Partial and each Class? is a Class?_Partial - creating a...

__init__ not called when subcalssing dict and something else [duplicate]

python,multiple-inheritance,builtins
This question already has an answer here: Why aren't Python's superclass __init__ methods automatically invoked? 8 answers Consider the following code: class Lockable(object): def __init__(self): self._lock = None def is_locked(self): return self._lock is None class LockableDict(dict, Lockable): pass And now: In [2]: l = example.LockableDict(a=1, b=2, c=3) In [3]:...

C++ covariant return type error with multiple inheritance

c++,multiple-inheritance,c++98,covariant
I have the code which is equivalent to this one: class X {}; class Y {}; template< typename T> class C { public: virtual T * foo() = 0; }; class A : public C< X> { public: X * foo() {}; }; class B : public A {}; class...

Coping with single inheritance in a game

oop,inheritance,design,multiple-inheritance
I had a simple concept for a 2d game. Think of like a Final Fantasy / D&D setting. I was working in C# but it's more of a general single-inheritance OO issue. I'm just assuming there's a scheme where you can implement many interfaces but inherit only one class. There...

Abstract class not mapping directly, any elegant solutions?

c++,oop,interface,multiple-inheritance
The code below fails to instantiate the Display_OpenGL class, because it does not consider the Surface implementation from Surface_OpenGL to map with the Display : Surface class. Uncommenting this line fix the problem but then this is not elegant. //void Draw() { Surface_OpenGL::Draw(); } Is there's a better solution for...

defining your own python objects and data types

python,inheritance,data-structures,data,multiple-inheritance
Apologize if this is a duplicate, tried searching. I understand that everything in python is a data type, but this is what I'm a bit confused about. So everything is an object, we have the collection class, integer, float, and other classes as children of the parent object class thinking...

Multiple inheritance issue with templates and interfaces

c++,templates,inheritance,multiple-inheritance
I am working on a project that uses a tcl interpreter, we bind c++ objects and methods to tcl commands. The bindings are c++ functions that have a ClientData pointer parameter (which is pretty much a void*). We then cast this as the object we are expecting it to be,...

Initializing derived classes through bases

c++,multiple-inheritance
Consider: struct V { V(int i) { std::cout << "initialized v" << '\n'; } }; struct A { A() { std::cout << "initialized a" << '\n'; } }; struct B : virtual V, virtual A { B(int i) :V{i} { std::cout << "initialized b" << '\n'; } }; class C...

Multiple-inheritance and mixins to bind object slots in Python

python,oop,multiple-inheritance,mixins
I am interested in creating a class hierarchy where various mixins create the slots in an object: class A(object, Keyable, Taggable): """A is keyable and taggable.""" def __init__(self): super(A, self).__init__() print "A" class B(BodyText, Valuable): """B is everything a A is, plus Valuable""" def __init__(self): super(B, self).__init__() print "B" class...

Having a template refer to a member from a jointly-inherited class

c++,templates,multiple-inheritance
It's obvious that the following code won't compile, because it gives an "undeclared identifier" error at the line 'n = n_init'. Nevertheless, to a human reader the intent is probably clear enough: I want to declare a template for a class which will never be instantiated by itself, but only...

what does compiler do when new an object and assign the address of the created object to its base class pointer

c++,multiple-inheritance,virtual-functions
Example 3:(page 377) class A {virtual void f();}; class B {virtual void f(); virtual void g();}; class C: A, B {void f();}; A* pa = new C; B* pb = new C; C* pc = new C; pa->f(); pb->f(); pc->f(); pc->g() (1) In Multiple inheritance for C++, Bjarne wrote: On...

Partial class delegation in Kotlin

multiple-inheritance,delegation,kotlin
How do I partial delegate methods/fields in Kotlin? To be specific: here I am trying to inherit class User from interface TraitA and implement field marked: Boolean in the wrapper StateA. That would clean up the User implementation, because marked is just a state field. Note that TraitA can't be...

Inherit from both 'heapq' and 'deque' in python?

python,multiple-inheritance,priority-queue,deque
I'am trying to implement a 'heapq' or a 'deque' dynamically (according to user's input) class MyClass(): def __init__(self, choose = True ): self.Q = [] self.add = self.genAdd(choose) self.get = self.genGet(choose) def genAdd(self, ch): if(ch == True): def f(Q, elem): return Q.append else: def f(Q): return heappush return f and...

c++ Check if most derived class of a base reference has another base [duplicate]

c++,multiple-inheritance,dynamic-cast
This question already has an answer here: C++11: source type is not polymorphic 3 answers Here is some sample code of the behavior I'd like to see: // Example program #include <iostream> #include <string> #include <type_traits> #include <vector> class BaseA { public: BaseA() { }; }; class BaseB {...

Basic Java inheritance exercise

java,inheritance,hierarchy,multiple-inheritance
I'm learning Java and I'm doing this exercise and I have to say what the main method prints. Its goal is to better understand Java inheritance. interface X extends Remote{ A m(B bb) throws RemoteException; } class A implements Serializable{ int a; A m(A aa){ aa.a = 3; System.out.println("A.m"); return...

Multiple level of different kind of inheritance

php,doctrine2,multiple-inheritance,single-table-inheritance
For my project, I'm trying to use the inheritance feature of Doctrine. I need to represent medias (through different tables : one table for uploaded documents, one for linked videos, ... and so on). But, the videos can vary from provider to provider (such as Youtube, Dailymotion, you name it)....

Filling out abstract class members by deriving from concrete class

c++,inheritance,multiple-inheritance,class-design
Let's say I have an interface that inherits from another interface (pure abstract class) class BaseInterface {}; Then another interface builds upon BaseInterface class ExtendedInterface : public BaseInterface {}; Now, I have a concrete class that implements BaseInterface: class Base : public BaseInterface {}; Now, I want to implement ExtendedInterface,...

How to use derived class variable in baseclass method

python,oop,multiple-inheritance
I have a hierarchy of classes that inherit from each other that look something like that: class BaseClass(object): def __init__(self): self.localLog = logging.getLogger(testName) self.var1 = 'a' def printVar(self): print self.var1 class SubClass1(BaseClass): def __init__(self): self.var1 = 'b' super(SubClass1, self).__init__() class SubClass2(SubClass1): def __init__(self): self.var1 = 'c' super(SubClass2, self).__init__() Now I...

Multiple inheritance in scrapy spiders

python,regex,scrapy,multiple-inheritance,scrapy-spider
Is it possible to create a spider which inherits the functionality from two base spiders, namely SitemapSpider and CrawlSpider? I have been trying to scrape data from various sites and realized that not all sites have listing of every page on the website, thus a need to use CrawlSpider. But...

compiler's detail of this pointer, virtual function and multiple-inheritance

c++,pointers,multiple-inheritance,virtual-functions,this-pointer
I'm reading Bjarne's paper: Multiple Inheritance for C++. In section 3, page 370, Bjarne said that "The compiler turns a call of a member function into an "ordinary" function call with an "extra" argument; that "extra" argument is a pointer to the object for which the member function is called."...

understanding of multiple inheritance for c++

c++,this,multiple-inheritance,virtual-functions,vtable
I'm reading multiple inheritance for c++ An Example in the paper:(page 377) class A {virtual void f();}; class B {virtual void f(); virtual void g();}; class C: A, B {void f();}; A* pa = new C; B* pb = new C; C* pc = new C; pa->f(); pb->f(); pc->f(); pc->g()...

How to Model Inheritance

database-design,data-modeling,multiple-inheritance,class-diagram,erd
I am a bit confused in choosing an approach to model the following relationships. Scenario: The system has a User. The User can perform 2 functions : He Can be a normal User who can buy products listed on the website. He Can Subscribe to Sell Products on the website....

C++: Pointer contains different address after being passed

c++,pointers,visual-studio-2013,multiple-inheritance,memory-address
So i have some code like this: #include <iostream> using namespace std; class Base1 {}; class Base2 {}; class A { public: A() {} void foo(Base2* ptr) { cout << "This is A. B is at the address " << ptr << endl; } }; A *global_a; class B :...

how to resolve multiple inheritance

java,android,multiple-inheritance,extends
I am working in android.and when I am creating new project than By default it extends the ActionBarActivity.now I want to extends other activity that is List Activity and it is predefined activity in android,but we know that java doesn't support multiple inheritance. so how can i solve it because...

Does my UoW class need to implement IDisposable if my BaseUow already implements it? [duplicate]

c#,inheritance,multiple-inheritance,idisposable,unit-of-work
This question already has an answer here: Implementing IDisposable on a subclass when the parent also implements IDisposable 5 answers I have created a base class for my Unit of Work called BaseUoW that inherits IDisposable like so: public class BaseUoW : IDisposable { } My question is... If...

Does this pointer adjustment occur for non-polymorphic inheritance?

c++,multiple-inheritance,this-pointer
Does non-polymorphic inheritance require this pointer adjustment? In all the cases I've seen this pointer adjustment discussed the examples used involved polymorphic inheritance via keyword virtual. It's not clear to me if non-polymorphic inheritance would require this pointer adjustment. An extremely simple example would be: struct Base1 { void b1()...

Multiple inheritance via templates [closed]

c++,templates,inheritance,multiple-inheritance,diamond-problem
Is it good idea to replace virtual multiple inheritance (diamon) with teplates inheritence (linear)? For example I have this class diagram : IBase / \ / \ IExtendedBase BaseImpl \ / ExtendedImpl I know that I can implement it with virtual inheritance. But can I use templates in order to...