This question is meant to be more about __dir__
than about numpy
.
I have a subclass of numpy.recarray
(in python 2.7, numpy 1.6.2), and I noticed recarray
's field names are not listed when dir
ing the object (and therefore ipython's autocomplete doesn't work).
Trying to fix it, I tried overriding __dir__
in my subclass, like this:
def __dir__(self):
return sorted(set(
super(MyRecArray, self).__dir__() +
self.__dict__.keys() + self.dtype.fields.keys()))
which resulted with: AttributeError: 'super' object has no attribute '__dir__'
.
(I found here this should actually work in python 3.3...)
As a workaround, I tried:
def __dir__(self):
return sorted(set(
dir(type(self)) +
self.__dict__.keys() + self.dtype.fields.keys()))
As far as I can tell, this one works, but of course, not as elegantly.
Questions:
- Is the latter solution correct in my case, i.e. for a subclass of
recarray
?
- Is there a way to make it work in the general case? It seems to me it wouldn't work with multiple inheritance (breaking the
super
-call chain), and of course, for objects with no __dict__
...
- Do you know why
recarray
does not support listing its field names to begin with? mere oversight?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…