Just passing
1 min readOct 4, 2017

--

Sorry, but you’re wrong. Not only it’s possible to remove an attribute from the child, but it’s also possible to do it in a non ugly way. But since the attributes don’t really exist in the child, you can’t find the answer with this question. The right question is, “how to not import them”. And for this, you need :

  1. To prevents their use, and __getattribute__ is here for that. Just raise an AttributeError when they are accessed and it’s done.
  2. To hide their shadowed presence, and yes, __dir_ is here for that. But please not this, ugly and incorrect, false linking between dir and __dict__. Basically speaking, dir return all the attributes, while __dict__ return just the variables. Return a correct __dir__ where you’ve removed the unwanted attributes.

class Son( Father ):

def __getattribute__( self, name ):
if name in [whatever must be excluded ]: raise AttributeError( name )
else: return super( Son, self ).__getattribute__( name )

def __dir__(self):
return sorted( ( set( dir( self.__class__ ) ) | set( self.__dict__.keys() ) ) — set( [ whatever must be excluded ] ) )

And it’s done.

  1. The attributes are not imported into the child.
  2. It’s not an ugly code.
  3. The behavior of the class is 100% correct.
  4. The attributes you’ll add to the class are usable.
  5. The attributes you’ll add to the class will be seen with dir.

--

--