Odd behaviour of dir(object) with metaclasses.

Dirk Gerrits dirk at gerrits.homeip.net
Thu Apr 10 08:18:32 CEST 2003


Tim Peters wrote:
> [Dirk Gerrits]
> 
>>I am a bit puzzled by the behaviour of the dir builtin.
[snip]
>>
>>Why is there an unnatural (IMHO) distinction between type/class objects
>>and other non-module objects? I assume that a class's class is not
>>considered become dir comes from the pre-metaclass era?
> 
> 
> No, dir() was rewritten from scratch for 2.2.  The comments in the C code
> say
> 
> 	/* Elif some form of type or class, grab its dict and its bases.
> 	   We deliberately don't suck up its __class__, as methods belonging
> 	   to the metaclass would probably be more confusing than helpful. */
> 
> That's what Guido believed at the time, and I agreed at the time, so that's
> what the code does.  Across at least seven pre-releases of 2.2, nobody
> disagreed <wink>.

Well I think its understandable, but in my mind, what dir does to 
classes should be called something like instancedir. Anyway, I guess you 
have a point. People who don't use metaclasses won't care anyway. And 
those who do might not care either. ;)

> 
>>Shouldn't the docs really read
>>...
> 
> 
> No, becuase the suggested rewording doesn't describe the actual behavior --
> as you noted above, the current wording does.

Yeah I meant to say 'Shouldn't the docs really read ... with the 
behaviour changed accordingly?'. But I assume you'll say no to that as 
well. ;)

>>...
>>I hope you'll be able to clarify these things for me a bit.
> 
> 
> dir() was meant to be a convenience in interactive mode, and that's all.
> Because of that, no two people will agree on what it "should do" in all
> respects in all cases.  Changing it always makes people scream too, so I
> doubt it will be changed again.  If you don't like what it does, write a dir
> that does what you like -- doing so in Python is easy enough.

Yes, one of Python's many great strengths. :)

Thanks for clearing this one up.

Dirk Gerrits







More information about the Python-list mailing list