How do you use `help` when write your code
Tim Chase
python.list at tim.thechases.com
Sun Jul 6 14:22:30 EDT 2014
On 2014-07-06 17:52, Steven D'Aprano wrote:
> I have a monkey-patched version of dir() which takes a second
> argument, a glob, to filter the list of names returned:
>
> py> len(dir(os)) # Too much!
> 312
> py> dir(os, 'env')
> ['_putenv', '_unsetenv', 'environ', 'environb', 'getenv',
> 'getenvb', 'putenv', 'supports_bytes_environ', 'unsetenv']
> py> dir(os, 'env*')
> ['environ', 'environb']
And for those of us that don't have Steven's monkey-patched dir()
call, I tend to do it with a list comprehension:
>>> [s for s in dir(obj) if 'env' in s]
{output here}
This works nicely for even more complex tests:
>>> [s for s in dir(obj) if not s.startswith('_')]
{list of public properties/methods}
>>> [s for s in dir(obj) if callable(getattr(obj, s))]
{list of callable attributes}
>>> [s for s in dir(obj) if isinstance(getattr(obj, s), basestring)]
{list of string attributes}
-tkc
More information about the Python-list
mailing list