Multiple inheritance problem in Python?

You only have a single object here; the some_name property is shared between methods from all inherited classes. You call A. __init which sets it to A then B.

__init which changes it to B Also note that you're calling base methods incorrectly; use super : class A(object): def __init__(self): self. Some_name = 'A' super(A, self). __init__() def print_a(self): print self.

Some_name class B(object): def __init__(self): self. Some_name = 'B' super(B, self). __init__() def print_b(self): print self.

Some_name class C(A, B): def __init__(self): super(C, self). __init__() if __name__ == '__main__': c = C() c. Print_a().

You only have a single object here; the some_name property is shared between methods from all inherited classes. You call A. __init__, which sets it to A, then B.

__init__, which changes it to B. Also note that you're calling base methods incorrectly; use super: class A(object): def __init__(self): self. Some_name = 'A' super(A, self).

__init__() def print_a(self): print self. Some_name class B(object): def __init__(self): self. Some_name = 'B' super(B, self).

__init__() def print_b(self): print self. Some_name class C(A, B): def __init__(self): super(C, self). __init__() if __name__ == '__main__': c = C() c.

Print_a().

– zkz Jan 13 at 7:46 I'm not sure what you're asking, but you can't have different properties on the same object with the same name. They're the same object, stored in the same place: c. __dict__, which is simply a regular Python dictionary.

– Glenn Maynard Jan 13 at 8:13 2 Note that you can say self. __some_name, which will create a semi-"private" attribute to that class; it will actually create attributes named eg. _A__some_name and _B__some_name. This is very rarely used, though, and I don't recommend it.

– Glenn Maynard Jan 13 at 8:45.

There's only one self, and you're overwriting its some_name in B. __init__. Maybe you're used to C++, where there would be two separate fields, A.

Some_name and B. Some_name. This concept doesn't apply to Python, where attributes are created dynamically on assignment.

That's the expected python behavior. – kriss Jan 13 at 7:48 @zkz, you need to choose unique names for each attribute, and prevent the classes from stepping on each other. – Matthew Flaschen Jan 13 at 7:48.

You can get this type of behavior using C++ inheritance model, but python model is very different. Only one object with one set of fields. If you want the C++ behavior, the simplest way is probably to declare subobjects (and it looks like a common abuse of inheritance over composition).

Looks like you are trying to do something like below: class Printable(object): def __init__(self, name): self. Name = name def myprint(self): print self.Name class C(object): def __init__(self): self. A = Printable('A') self.

B = Printable('B') def print_a(self): self.a.myprint() def print_b(self): self.a.myprint() if __name__ == '__main__': c = C() c. Print_a().

The example above is just a abstract of my problem. It's more complex in my case, where I can not satisfy my need from your way. I need a way like C++.

– zkz Jan 13 at 8:10 @zkz: you should explain your actual problem, because what you are saying is very unlikely. All C++ like inheritance models can be managed using composition. If your problem is with finding method name from subobjects it is very easy to do using python introspection tools (getattr).

Or is the problem with some common base class? Actually, inheritance is never mandatory, just a facility for mapping concepts to code and in many, many cases I saw it is used to do things that should definitely not be done using inheritance. – kriss Jan 13 at 10:10.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions