number of arguments a function takes

Elaine Jackson elainejackson7355 at home.com
Thu Jan 29 20:05:16 EST 2004


I think you're expectations of this are a little high. It's just something I did
for amusement while I was drinking my coffee this morning. Anyway, here it is.
And yes, I'd be very interested to see your code.

Peace (code follows)

NEG = lambda A: not A
CONJ = lambda *args: False not in args
DISJ = lambda *args: True in args
IMPL = lambda A,B: (not A) or B
EQUIV   = lambda A,B: (A and B) or (not A and not B)

def truthTable(boolFunct):
    assignmentList = lambda numArgs: (numArgs==0 and [[]]) or \
        reduce(list.__add__,[[X+[False],X+[True]] for X in
assignmentList(numArgs-1)])
    import inspect
    numArgs=len(inspect.getargspec(boolFunct)[0])
    for assignment in assignmentList(numArgs):
        print assignment," : ",boolFunct(*assignment)

## EXAMPLE:
f = lambda x,y,z: DISJ(CONJ(x,y),NEG(z))
truthTable(f)


"Corey Coughlin" <corey.coughlin at attbi.com> wrote in message
news:a8623416.0401291606.1fa4de31 at posting.google.com...
| "Elaine Jackson" <elainejackson7355 at home.com> wrote in message
news:<YVbSb.319671$JQ1.192347 at pd7tw1no>...
| > Exactly what I was looking for. Thank you very much.
| >
| > "Duncan Booth" <me at privacy.net> wrote in message
| > news:Xns947FAE6461014duncanrcpcouk at 127.0.0.1...
| > | "Elaine Jackson" <elainejackson7355 at home.com> wrote in
| > | news:CSaSb.329909$ts4.189798 at pd7tw3no:
| > |
| > | > Suppose one of the arguments of a function f_1 is another function
| > | > f_2. Can f_1 access the number of arguments taken by f_2?  (I'm
| > | > writing a little script to automate the construction of logical
| > | > truth-tables.)  Thanks.
| > |
| > | Look at the inspect module, in particular you probably want
| > | inspect.getargspec:
| > |
| > | >>> import inspect
| > | >>> def f(a,b): pass
| >
| > | >>> inspect.getargspec(f)
| >  (['a', 'b'], None, None, None)
| > | >>> help(inspect.getargspec)
| > | Help on function getargspec in module inspect:
| > |
| > | getargspec(func)
| > |     Get the names and default values of a function's arguments.
| > |
| > |     A tuple of four things is returned: (args, varargs, varkw, defaults).
| > |     'args' is a list of the argument names (it may contain nested lists).
| > |     'varargs' and 'varkw' are the names of the * and ** arguments or None.
| > |     'defaults' is an n-tuple of the default values of the last n
arguments.
| > |
| > | >>>
|
| So you're working on a truth table program?  I have a truth table
| object I did for my own application, more focused on finding
| input/output transitions in arbitrary truth tables, but if you're
| interested, let me know.  Regardless, are you working on something
| that will ultimately become public?  If so, be sure to announce it
| when you're done, I'd love to have a look.  Especially if you're doing
| anything with functional simplification, my object can return a simple
| SOP function for a table (reducing out unused inputs is also an
| option) but they can get kind of lengthy for complicated functions.
| And if you need any other help, be sure to let me know!





More information about the Python-list mailing list