-1

Code:

class Base(object):
    def __init__(self):
        print "Base created"


class ChildB(Base):
    def __init__(self):
        super(ChildB, self).__init__()

Base = ChildB
Base()

When I do Base = ChildB, Base, in the global frame, points to ChildB which extends the previous Base. Since python is a dynamically-typed language, so I assume the hierarchical relationship will change. And MRO that super reference will be ChildB > Base(ChildB) > ChildB > Base(ChildB) ....

But the result tell the truth that ChildB still extends original Base. Why???

MMMMMCCLXXVII
  • 571
  • 1
  • 8
  • 23
  • it is unclear what actually you are trying to do? – xiº Mar 15 '17 at 14:09
  • You may find this article useful: [Facts and myths about Python names and values](http://nedbatchelder.com/text/names.html), which was written by SO veteran Ned Batchelder. Also see [Other languages have "variables", Python has "names"](http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables) for a brief summary with nice diagrams. – PM 2Ring Mar 15 '17 at 14:16
  • @xiº It's my question about the answer from S C to [Understanding Python super() with __init__() methods ](http://stackoverflow.com/questions/576169/understanding-python-super-with-init-methods) – MMMMMCCLXXVII Mar 15 '17 at 14:22

1 Answers1

1

Don't confuse the class whose name is Base with the Python variable Base that originally referred to it.

>>> class Base(object): pass
...
>>> class ChildBase(Base): pass
...
>>> Base = ChildBase
>>> Base.__name__
'ChildBase'

When ChildBase is defined, the name Base is still bound to the class Base, so that is the parent class of ChildBase, regardless of what the name Base is subsequently bound to.

A class statement is like class Base(object): pass is equivalent to a call to type:

Base = type('Base', (object,), {})

The identifier Base is used both for the name of the class and the variable bound to the resulting object.

chepner
  • 497,756
  • 71
  • 530
  • 681