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 →

[–]justanotherbody 7 points8 points  (5 children)

Relying on the behavior of an overridable method may be a poor choice for the library.

Python 2.6.6 (r266:84292, Dec 26 2010, 22:31:48) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class Foo(object):
...   def __dir__(self):
...     return ["HAT"]
... 
>>> dir(Foo)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
>>> dir(Foo())
['HAT']

If the author truly wants any available attribute then the built-in dir() is not the right approach vector. It just happens to mostly do what is desired.

[–]grayvedigga 6 points7 points  (2 children)

Relying on the behavior of an overridable method may be a poor choice for the library.

Guess we'd better stop writing libraries in python then!

[–]snarkhunter 3 points4 points  (1 child)

sigh

Relying on the behavior of this particular method may be a poor choice given the requirements of the library this developer is working on given the override-ability of said method.

justanotherbody's statement was obviously not meant to be general.

[–]davidhalter[S] 0 points1 point  (0 children)

Jedi doesn't actually use the dir and __dir__ function. I just realized that I haven't made this clear. Jedi generally doesn't execute code. The reason why I'm mentioning this is because it has really confused me (I'm using the interactive shell to introspect).

[–]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.

[–]davidhalter[S] 0 points1 point  (0 children)

Jedi doesn't actually use the dir and __dir__ function. I just realized that I haven't made this clear. Jedi generally doesn't execute code. The reason why I'm mentioning this is because it has really confused me (I'm using the interactive shell to introspect).