-3

I have a question about name clashes in python. If I have something like:

class A: a='a'
class B(A): a='b'
class C(A): a='c'
class D(C,B): pass

D.a will print c, is there any way to retrieve B.a from D or A.a?

user1544128
  • 583
  • 4
  • 20
  • 1
    can you read how mro works ?http://www.python.org/download/releases/2.3/mro/ – James Sapam Dec 22 '13 at 10:49
  • -1 -- your question doesn't shown even a *minimal* research effort. Your are basically asking how multiple-inheritance works, but this is explained quite well in the documentation and in tons of other questions here on SO. I don't buy that you tried to search and couldn't find anything about this. – Bakuriu Dec 22 '13 at 10:54

1 Answers1

2

Yes, you can do exactly what you suggest:

class D(C, B):
    a = A.a
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • then, there is no more meaning for inheritance :) – James Sapam Dec 22 '13 at 10:54
  • Well I assume there are other class and instance attributes we aren't being told about, otherwise this is a high-effort way to get characters! – jonrsharpe Dec 22 '13 at 11:00
  • maybe the question was not precise enough. Once I have compiled the code from the question how can access to `A.a` from a `D` object? If I have `d = D()`, `d.a` will give me `c`. – user1544128 Dec 22 '13 at 11:06
  • 3
    You can use `A.a` *anywhere*. In your example, `a` is a class attribute, the specific instance `d` is more-or-less irrelevant. If you want `d.a == A.a`, then assign it: `d.a = A.a`. – jonrsharpe Dec 22 '13 at 11:08
  • @user1544128: Try not to muddle `D.a` and `D().a` – Eric Dec 22 '13 at 11:41