python - How to "hide" superclass methods in a subclass -


i want create subclass "hides" methods on superclass don't show in dir() or hasattr() call , users can't call them (at least not through of normal channels). least amount of "magic" possible. thanks.

overriding __dir__ , __getattribute__ method respectively should trick. pretty canonical way kind of stuff in python. although whether should doing entirely different matter.

see python docs on customizing attribute access

use __dir__ list available attributes (this won't affect actual attribute access)

class a(object):     def __dir__(self):         return []  >>> print dir(a()) [] 

use __getattribute__ control actual attribute access

class a(object):     def __getattribute__(self, attr):         """prevent 'private' attribute access"""         if attr.startswith('_'):             raise attributeerror          return object.__getattribute__(self, attr)   >>> = a() >>> a.x = 5 >>> a.x 5 >>> a._x = 3 >>> a._x attributeerror 

this trying do.

class nosuper(object):     def __getattribute__(self, attr):         """prevent accessing inherited attributes"""         base in self.__bases__:             if hasattr(base, attr):                 raise attributeerror          return object.__getattribute__(self, attr) 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -