Answer the question
In order to leave comments, you need to log in
Explain the nuance in class inheritance in Python?
There is this code:
<font color="black"><a href="http://s-c.me/9339/s">Copy Source</a> | <a href="http://s-c.me/9339/h">Copy HTML</a><ol> <li><font color="#008000">'''</font><br/> <font color="#008000">Базовый класс</font><br/> <font color="#008000">'''</font></li> <li><font color="#0000ff">class</font> <font color="#cc6633">Component</font>():</li> <li> </li> <li> connects = []</li> <li> </li> <li> <font color="#008000">'''</font><br/> <font color="#008000"> Метод добавляе объект в коллекцию</font><br/> <font color="#008000"> '''</font></li> <li> <font color="#0000ff">def</font> <font color="#cc6633">connect</font>(self, object):</li> <li> <b>self</b>.connects.append(object)</li> <li> </li> <li> </li> <li><font color="#008000">'''</font><br/> <font color="#008000">Подкласс 1</font><br/> <font color="#008000">'''</font></li> <li><font color="#0000ff">class</font> <font color="#cc6633">Component1</font>(<font color="#cc6633">Component</font>):</li> <li> </li> <li> <font color="#0000ff">def</font> <font color="#cc6633">getData</font>(self):</li> <li> <font color="#0000ff">print</font> <font color="#008000">123</font></li> <li> </li> <li> </li> <li><font color="#008000">'''</font><br/> <font color="#008000">Подкласс 2</font><br/> <font color="#008000">'''</font></li> <li><font color="#0000ff">class</font> <font color="#cc6633">Component2</font>(<font color="#cc6633">Component</font>):</li> <li> </li> <li> <font color="#0000ff">def</font> <font color="#cc6633">getData</font>(self):</li> <li> <font color="#0000ff">print</font> <font color="#008000">123</font></li> <li> </li> <li> </li> <li> </li> <li><font color="#008000">'''</font><br/> <font color="#008000">Инстанцируем</font><br/> <font color="#008000">'''</font></li> <li>c1 = <font color="#cc6633">Component1</font>()</li> <li>c2 = <font color="#cc6633">Component2</font>()</li> <li> </li> <li><font color="#008000">'''</font><br/> <font color="#008000">Проверяем кол-во элементов в подклассе 2</font><br/> <font color="#008000">'''</font></li> <li><font color="#0000ff">print</font> <b>len</b>(<font color="#cc6633">Component2</font>.connects) <font color="#696969"># 0</font></li> <li> </li> <li><font color="#008000">'''</font><br/> <font color="#008000">Добавляем первому подклассу в объект коллекцию</font><br/> <font color="#008000">'''</font></li> <li>c1.<font color="#cc6633">connect</font>(c2)</li> <li> </li> <li><font color="#008000">'''</font><br/> <font color="#008000">Проверяем кол-во элементов в подклассе 2</font><br/> <font color="#008000">'''</font></li> <li><font color="#0000ff">print</font> <b>len</b>(<font color="#cc6633">Component2</font>.connects) <font color="#696969"># 1 </font></li> </ol></font>
Answer the question
In order to leave comments, you need to log in
connects is an attribute of the Component class, not an instance. Inheriting classes use THIS memory address (id(c1.connects) == id(c2.connects)) If you
want it to be different, add connects = [] to the description of Component1 and Component2, as you did for the base class.
Not
connects = []
but
def __init__(self)
connects = []
PS Parser ate indents :(
Are you sure that you need to connect to the class, and not to the object?
then it's more correct to do it as @classmethod and not an object method.
and in order not to copy the line, the attribute can be created in the meta-class:
class MetaComp(type):
def __init__(cls,name,bases,kwargs):
super(MetaComp, cls).__init__(name,bases,kwargs)
cls.connects = [] # для каждого класса и наследника будеит свой
class Comp(object):
__metaclass__ = MetaComp
@classmethod
def connect(cls,obj):
cls.connects.append(obj)
class Comp1(Comp):
pass
class Comp2(Comp):
pass
# каждый вызов добавляет к своему connects:
Comp.connect('foo')
Comp1.connect('foo1')
Comp2.connect('foo2')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question