473,325 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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

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
2 2014
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Bob | last post by:
I've tried everything; and I can't seem to get past this VERY (seemingly) simply problem. I want to work with an array variable within a function(s). I can't get it to work; if I: 1) global...
6
by: aa | last post by:
I use the following fragment of code to output datf from MySQL: ====================================================== $chan = mysql_connect ($db_host, $username, $password); mysql_select_db...
9
by: Maurice LING | last post by:
Hi, I know that PLY lex is able to do line counting. I am wondering if there is a way to count the number of each keywords (tokens) in a given file? For example, how many IF tokens etc? ...
8
by: Nick Coghlan | last post by:
Time for another random syntax idea. . . So, I was tinkering in the interactive interpreter, and came up with the following one-size-fits-most default argument hack: Py> x = 1 Py> def...
2
by: Noah | last post by:
Is there a simple way to get a dictionary of argument names and their default values for a method or function? I came up with one solution, but I feel like Python must have a simpler way. ...
19
by: Riko Wichmann | last post by:
hi everyone, I'm googeling since some time, but can't find an answer - maybe because the answer is 'No!'. Can I call a function in python inline, so that the python byte compiler does...
7
by: K. Jansma | last post by:
Hi, given the following example class class Test: def f(self,a, L=): L.append(a) return L and the following statements
13
by: seaside | last post by:
I have a method function appendChildNode( AST $aNewChild ) { ... } <<< where AST is a class. If I pass null, PHP renders this message: Catchable fatal error: Argument 1 passed to...
3
by: eliben | last post by:
Hello, I'm building a parser in Python, and while pondering on the design of my ASTs had the idea to see what Python uses. I quickly got to the compiler.ast module, and understood it's...
1
by: dmitrey | last post by:
hi all, howto check is function capable of obtaining **kwargs? i.e. I have some funcs like def myfunc(a,b,c,...):... some like def myfunc(a,b,c,...,*args):... some like
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.