[Python] Get Function Args

getargs = lambda func: (func.func_code.co_varnames[:func.func_code.co_argcount])

Example Session


#Example Session

getargs = lambda func: (func.func_code.co_varnames[:func.func_code.co_argcount])
def addition(x,y):
    return x+y
print getargs(addition)
#('x', 'y')

Yep, it’s really that simple. No weird exec functions needed, no intrusive introspection via the disassembler.

This entry was posted in Blog, Programming, Python, Snippets. Bookmark the permalink.

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>