Skip to content

Instantly share code, notes, and snippets.

@alvesjnr
Created February 1, 2012 15:18
Show Gist options
  • Select an option

  • Save alvesjnr/1717493 to your computer and use it in GitHub Desktop.

Select an option

Save alvesjnr/1717493 to your computer and use it in GitHub Desktop.

First of all I created two files:

File which contains the base class (I called it base.py)

class A(object):
   """do something"""
   def __init__(self):
       print 'initializing base class' 
       print '__module__: ', self.__module__ 
       print '__class__: ', self.__class__ 
       print 'self: ', self 
       print

File which contains the son class (inherits from A). I called it other.py

from base import A

class A(A):
   """do the same thing as A from base"""
   def __init__(self):
       super(A,self).__init__()
       print 'initializing son class'
       print '__module__: ', self.__module__
       print '__class__: ', self.__class__
       print 'self: ', self
       print

Notice that in the second file (other.py) the name of the class is identical to the name of its parent (A for both). For some reason the class created has priority on the namespace and overwrite the old one, which you have imported.

Now, running it:

>>> import other
>>> a = other.A()
initializing base class
__module__:  other
__class__:  <class 'other.A'>
self:  <other.A object at 0x7f6bee790410>

initializing son class
__module__:  other
__class__:  <class 'other.A'>
self:  <other.A object at 0x7f6bee790410>

>>> a
<other.A object at 0x7f6bee790410>
>>> 
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'a', 'other']
>>> 

Notice that your namespace just contains the module 'other', and not the base one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment