Mel
Mel

Reputation: 1125

Inheritance in Python

We just started learning about class inheritance and attribute lookup in python. I have a question about the following code:

class a : n = 1
class b : n = 2
class c : n = 3
class d (a,b) : pass
class e (d,c) : pass

I know that e.n would equal 1 due to the nature of attribute lookup procedure (depth first search). However, how would I access, say, class c's n from class e? I've tried e.c.n, but that gives me an error. Can someone tell me what I'm doing wrong? Thanks in advance!

Upvotes: 3

Views: 303

Answers (2)

John La Rooy
John La Rooy

Reputation: 304117

>>> e.__bases__[1].n
3

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798436

You can't get there from here. Class attributes are replaced. Use the class reference directly (c.n).

Upvotes: 1

Related Questions