By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,530 Members | 1,576 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,530 IT Pros & Developers. It's quick & easy.

Howto get the argument defaults of a function using an AST-object?

P: n/a
Hi,

for a school project I'm trying to analyze some python modules. I just
struggeld over a little detail I cannot solve:

shortened example of walking trough an AST with my own visitor:

class MyVisitor:
def visitFunction(self, t):
# Here I do not know know how to associate the default values
in t.defaults
# with the arguments in t.argnames
params = '???' # howto join the right args with their
default???
print "def %s(%s)" % (t.name, params)

fname = 'test.py'
ast = compiler.parseFile(fname)
compiler.walk(ast, MyVisitor())

I can only get a list with the arguments or the defaults. The following
examples would return the same:

def foo(x, y=True)
def foo(x=True, y)

Anyone an idea?

Thanks,

Stefan

Jul 4 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
st****@mayr-stefan.de wrote:
I can only get a list with the arguments or the defaults. The following
examples would return the same:

def foo(x, y=True)
def foo(x=True, y)
the latter is a syntax error.
Anyone an idea?
the defaults array contains the defaults for the len(defaults) last arguments.

</F>

Jul 4 '06 #2

P: n/a

Fredrik Lundh schrieb:
....
def foo(x, y=True)
def foo(x=True, y)

the latter is a syntax error.
...

the defaults array contains the defaults for the len(defaults) last arguments.
And again the own stupidity: I didn't check all my testcases for syntax
errors.

Thanks a lot,

Stefan

Jul 4 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.