Mercurial > hg > config
comparison python/example/func_args.py @ 625:e92bd004b906
STUB: .emacs python/example/func_args.py
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Wed, 12 Feb 2014 16:00:32 -0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
624:b0144a39086d | 625:e92bd004b906 |
---|---|
1 #!/usr/bin/env python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 get args of your own function | |
6 | |
7 http://stackoverflow.com/questions/582056/getting-list-of-parameter-names-inside-python-function | |
8 """ | |
9 | |
10 import argparse | |
11 import inspect | |
12 import os | |
13 import subprocess | |
14 import sys | |
15 | |
16 def foo(bar=None, fleem=None, blah=None): | |
17 frame = inspect.currentframe() | |
18 args, _, _, values = inspect.getargvalues(frame) | |
19 print 'function name "%s"' % inspect.getframeinfo(frame)[2] | |
20 for i in args: | |
21 print " %s = %s" % (i, values[i]) | |
22 return [(i, values[i]) for i in args] | |
23 | |
24 def main(args=sys.argv[1:]): | |
25 | |
26 parser = argparse.ArgumentParser() | |
27 options = parser.parse_args(args) | |
28 foo(1, 2, 3) | |
29 | |
30 if __name__ == '__main__': | |
31 main() |