Selcuk's suggestion is best but sometimes the refactoring is too troublesome.
So instead, here's something that will cause your co-workers to scream like banshees and curse your name.
It works by using a metaclass, hide_meta
, to add overloaded __getattribute__
and __dir__
methods. Its application to the desired class is done by simply setting __metaclass__ = hide_meta
and __excluded__ = ["list", "of", "unwanted", "attributes/methods"]
.
class hide_meta(type):
def __new__(cls, cls_name, cls_bases, cls_dict):
cls_dict.setdefault("__excluded__", [])
out_cls = super(hide_meta, cls).__new__(cls, cls_name, cls_bases, cls_dict)
def __getattribute__(self, name):
if name in cls_dict["__excluded__"]:
raise AttributeError(name)
else:
return super(out_cls, self).__getattribute__(name)
out_cls.__getattribute__ = __getattribute__
def __dir__(self):
return sorted((set(dir(out_cls)) | set(self.__dict__.keys())) - set(cls_dict["__excluded__"]))
out_cls.__dir__ = __dir__
return out_cls
class A(object):
def someMethodToHide(self):
pass
def someMethodToShow(self):
pass
class B(A):
__metaclass__ = hide_meta
__excluded__ = ["someMethodToHide"]
a = A()
print dir(a)
b = B()
print dir(b)
b.someMethodToShow()
b.someMethodToHide()