473,396 Members | 2,129 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,396 software developers and data experts.

number of arguments a function takes

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.

Peace
Jul 18 '05 #1
5 1975
In article <CSaSb.329909$ts4.189798@pd7tw3no>,
Elaine Jackson <el***************@home.com> wrote:

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.)


In theory, yes (because of Python's introspection capabilities). In
practice, you're likely to want to choose a different mechanism that
doesn't require that information. If you tell us more about your actual
problem, we may be able to point you in a better direction.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"The joy of coding Python should be in seeing short, concise, readable
classes that express a lot of action in a small amount of clear code --
not in reams of trivial code that bores the reader to death." --GvR
Jul 18 '05 #2
"Elaine Jackson" <el***************@home.com> wrote in
news:CSaSb.329909$ts4.189798@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.

Jul 18 '05 #3
Exactly what I was looking for. Thank you very much.

"Duncan Booth" <me@privacy.net> wrote in message
news:Xn***************************@127.0.0.1...
| "Elaine Jackson" <el***************@home.com> wrote in
| news:CSaSb.329909$ts4.189798@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.
|
| >>>
Jul 18 '05 #4
"Elaine Jackson" <el***************@home.com> wrote in message news:<YVbSb.319671$JQ1.192347@pd7tw1no>...
Exactly what I was looking for. Thank you very much.

"Duncan Booth" <me@privacy.net> wrote in message
news:Xn***************************@127.0.0.1...
| "Elaine Jackson" <el***************@home.com> wrote in
| news:CSaSb.329909$ts4.189798@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!
Jul 18 '05 #5
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" <co************@attbi.com> wrote in message
news:a8**************************@posting.google.c om...
| "Elaine Jackson" <el***************@home.com> wrote in message
news:<YVbSb.319671$JQ1.192347@pd7tw1no>...
| > Exactly what I was looking for. Thank you very much.
| >
| > "Duncan Booth" <me@privacy.net> wrote in message
| > news:Xn***************************@127.0.0.1...
| > | "Elaine Jackson" <el***************@home.com> wrote in
| > | news:CSaSb.329909$ts4.189798@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!
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steven D'Aprano | last post by:
I'm trying to keep an open mind, but I am perplexed about something in Python that strikes me as a poor design. py> def func(a,b): py> print a,b py> func(1) Traceback (most recent call...
4
by: Martin Magnusson | last post by:
I'm using a matrix and vector library, that won't compile. When running g++ I get the error message "macro "minor" passed 5 arguments, but takes just 1" The definition of "minor" looks like...
7
by: Ganny | last post by:
Is it possible to write a template function min that takes variable number of arguments? It should be without using ugly C var args, arrays, or statically overloading the method for N arguments....
5
by: Andrej Prsa | last post by:
Hi! Why do I get a warning about incompatible pointer type if I try to assign a pointer to the function with variable argument number: int func (int argc, ...) , but everything is ok...
7
by: vivekian | last post by:
Trying to write a function which can accept variable number of arguments of the same data type , ranging from 1 ... n . What would be the best way to go about this. Thanks, vivekian
2
by: Leslaw Bieniasz | last post by:
Cracow, 26.06.2006 Hi, I have the following problem. I have a class, let's say A (I omit irrelevant details) class A { public:
2
by: Ramashish Baranwal | last post by:
Hi, I need to process few out of a variable number of named arguments in a function and pass the remaining to another function that also takes variable number of named arguments. Consider this...
16
by: Peng Yu | last post by:
Hi, I'm wondering if there is a min function (in boost, maybe?) that accepts any number of arguments? std::min only accepts two arguments. If I want to get the minimum number out of many, I have...
0
by: James Mills | last post by:
On Fri, Oct 31, 2008 at 8:49 AM, mark floyd <emfloyd2@gmail.comwrote: Mark, this is correct behavior. You have 3 positional arguments in the function definition. You _must_ aupply _all_ 3 of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.