Consider any custom pythonic class. If I call dir(obect_of_class) , I get a list of its attributes:
['__class__', '__delattr__', '__dict__', '__dir__', ... '__weakref__', 'bases', 'build_full_name', 'candidates', ... 'update_name'].
In this list you can see 2 types of attributes:
- built-in attributes
- user defined.
I need to override __dir__ so that it returns only user-defined attributes. How can i do this?
Clearly, if I call myself in an overridden function, it gives me infinite recursion. So, I want to do something like this:
def __dir__(self): return list(filter(lambda x: not re.match('__\S*__', x), dir(self)))
but avoid endless recursion.
In general, how can I change a built-in function if I do not want to write it from scratch, but want to change an existing function?
source share