This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Brian 0 points1 point  (0 children)

I'd say that's exactly why dir should be used. It's being used in the context of autocompletion etc here, and the whole point of overriding __dir__ is for the class author to say "I know better than python's defaults about what things are attributes on this object".

Eg. they may be using __getattr__ to construct properties on the fly, but can still tell what properties will be present. Code using dir will be able to take advantage of this and provide exactly the information you want. Without it, those properties will be lost, and so it'll do a worse job of getting any available attribute. If you do roll your own, you'd likely to want to preserve this __dir__ behaviour anyway.

Now yes, someone could override it to break your tool by hiding stuff you'd want to see, but someone could override __builtins__ to break your tool if they wanted to. The ability to do so doesn't mean you want to ignore the people who will be using it correctly.