def __dir__(self):
def get_attrs(obj):
import types
if not hasattr(obj, '__dict__'):
return []
if not isinstance(obj.__dict__, (dict, types.DictProxyType)):
raise TypeError(
'{0}.__dict__ is not a dictionary'.format(obj.__name__))
return obj.__dict__.keys()
def dir2(obj):
attrs = set()
if not hasattr(obj, '__bases__'):
# obj is an instance
if not hasattr(obj, '__class__'):
# slots
return sorted(get_attrs(obj))
klass = obj.__class__
attrs.update(get_attrs(klass))
else:
# obj is a class
klass = obj
for cls in klass.__bases__:
attrs.update(get_attrs(cls))
attrs.update(dir2(cls))
attrs.update(get_attrs(obj))
return list(attrs)
return dir2(self) + self.keys()
# TODO: Ditch this behavior. (Backwards incompatible change!)
# It's probably that at some point a user is going create a method named
# the same as a key fetched from PIX.
评论列表
文章目录