Python Inheritance

We just started to study class inheritance and attribute search 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 en will be equal to 1 due to the nature of the attribute search procedure (depth search first). However, how do I access, say, the class cn from class e? I tried ecn but it gives me an error. Can someone tell me what I am doing wrong? Thanks in advance!

+3
source share
2 answers
>>> e.__bases__[1].n
3
+1
source

You cannot get from here. Class attributes are replaced. Use the class reference directly ( c.n).

+1
source

Source: https://habr.com/ru/post/1733137/


All Articles