Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #74040

Re: How do you use `help` when write your code

Date 2014-07-06 13:22 -0500
From Tim Chase <python.list@tim.thechases.com>
Subject Re: How do you use `help` when write your code
References <mailman.11547.1404665036.18130.python-list@python.org> <53b98cf2$0$29985$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.11550.1404671011.18130.python-list@python.org> (permalink)

Show all headers | View raw


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



Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How do you use `help` when write your code Shiyao Ma <i@introo.me> - 2014-07-07 00:36 +0800
  Re: How do you use `help` when write your code Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-07-06 17:52 +0000
    Re: How do you use `help` when write your code Tim Chase <python.list@tim.thechases.com> - 2014-07-06 13:22 -0500
    Re: How do you use `help` when write your code Roy Smith <roy@panix.com> - 2014-07-06 14:48 -0400
  Re: How do you use `help` when write your code Rick Johnson <rantingrickjohnson@gmail.com> - 2014-07-06 11:48 -0700
    Re: How do you use `help` when write your code Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-07-06 19:58 +0100
      Re: How do you use `help` when write your code Roy Smith <roy@panix.com> - 2014-07-06 15:15 -0400
        Re: How do you use `help` when write your code Chris Angelico <rosuav@gmail.com> - 2014-07-07 08:54 +1000
          Re: How do you use `help` when write your code Roy Smith <roy@panix.com> - 2014-07-06 20:52 -0400
            Re: How do you use `help` when write your code Chris Angelico <rosuav@gmail.com> - 2014-07-07 13:06 +1000
              Re: How do you use `help` when write your code Roy Smith <roy@panix.com> - 2014-07-07 07:22 -0400
                Re: How do you use `help` when write your code Chris Angelico <rosuav@gmail.com> - 2014-07-07 21:36 +1000
        Re: How do you use `help` when write your code Cameron Simpson <cs@zip.com.au> - 2014-07-07 11:51 +1000

csiph-web