dir() and propertly list NEWBIE

Steve M S Tregidgo smst at quantisci.co.uk
Fri Nov 26 06:53:27 EST 1999

Donn Cave wrote:
> Quoth tiddlerdeja at my-deja.com:
> [snip]
> | Similarly, If I can get an objects functions, is it possible then
get a
> | list of the possible parameters for that function?
> No!  Wouldn't that be nice?

Take a look at the function's attribute 'func_code'.  Within this
you'll find the information you want.  func_code's attributes include
'co_varnames' and 'co_argcount'.  The first co_argcount items in
co_varnames give you the parameters.

>>> import rfc822
>>> rfc822.parseaddr.func_code.co_varnames
('address', 'a', 'list')
>>> rfc822.parseaddr.func_code.co_argcount

So the parameters to rfc822.parseaddr are ('address', 'a',
-- or just 'address'.

(Note:  func_code.flags also tells you something here.  If flags&4 is
true then the function takes a *arg type parameter:

 def f(a, b, *c):

The name of the relevant parameter is the next unused item in
co_varnames.  If flags&8 is true then it also takes keyword

 def f(a, b, *c, **kw):

Again, the next unused item in co_varnames is the parameter.)

Default arguments are defined in the bytecode of the function's
parent namespace -- they're harder to get hold of...

Have fun,
Steve Tregidgo

More information about the Python-list mailing list