Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #8642
| Date | 2011-07-01 12:20 -0500 |
|---|---|
| From | Tim Chase <python.list@tim.thechases.com> |
| Subject | Re: Enhanced dir() function |
| References | <4e0d4d30$0$29990$c3e8da3$5496439d@news.astraweb.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.546.1309543383.1164.python-list@python.org> (permalink) |
On 06/30/2011 11:29 PM, Steven D'Aprano wrote:
> The dir() function is designed for interactive use, inspecting objects for
> the names of attributes and methods.
>
> Here is an enhanced version that allows you to pass a glob to filter the
> names you see:
>
> Comments and improvements welcome.
Having not seen any other comments on it fly by, I thought I'd
respond. While in general I like the idea, I'm not sure when I'd
go to the effort of bringing the function into my namespace when
I could just do something like
>>> [s for s in dir(...) if test_of_interest(s)]
If it came in as an effortless (i.e. O(1) where I do it once and
never again; not an O(n) where n=the number of times I invoke
Python) default replacement for dir(), I'd reach for it a lot
more readily. I seem to recall there's some environment-var or
magic file-name that gets sourced on every startup.
I use the list-comp version on a regular basis:
# implementation of which magic methods?
[s for s in dir(foo) if s.startswith('__') and s.endswith('__')]
# ignore magic methods
[s for s in dir(foo) if not (s.startswith('__') and
s.endswith('__'))]
# constants
[s for s in dir(foo) if s.isupper()]
# keywording
[s for s in dir(foo) if 'bar' in s.lower()]
Anyways, even if it just includes a brief blurb about "and this
is how you get it automatically in every Python session" (or a
link to the docs on how to do that), it would raise it from "meh"
to "nifty".
-tim
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Enhanced dir() function Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-01 14:29 +1000
Re: Enhanced dir() function Tim Chase <python.list@tim.thechases.com> - 2011-07-01 12:20 -0500
Re: Enhanced dir() function Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-02 04:32 +1000
Re: Enhanced dir() function rantingrick <rantingrick@gmail.com> - 2011-07-12 14:46 -0700
Re: Enhanced dir() function Chris Angelico <rosuav@gmail.com> - 2011-07-13 14:20 +1000
Re: Enhanced dir() function Ethan Furman <ethan@stoneleaf.us> - 2011-07-01 11:40 -0700
Re: Enhanced dir() function Ethan Furman <ethan@stoneleaf.us> - 2011-07-12 14:19 -0700
Re: Enhanced dir() function rantingrick <rantingrick@gmail.com> - 2011-07-12 14:38 -0700
csiph-web