Dear list,
If you ask: why do you choose these names? The answer is: they need to
be conformable with other functions, parameter names.
I have a function that pretty much like:
def output(output=''):
print output
and now in another function, I need to call output function, with again
keyword parameter output
def func(output=''):
output(output=output)
Naturally, I get 'str' object is not callable. Is there a way to tell
func that the first output is actually a function? (like in C++,
::output(output) )
Thanks.
Bo 7 1583
Bo Peng wrote: Dear list,
If you ask: why do you choose these names? The answer is: they need to be conformable with other functions, parameter names.
I have a function that pretty much like:
def output(output=''): print output
and now in another function, I need to call output function, with again keyword parameter output
def func(output=''): output(output=output)
Naturally, I get 'str' object is not callable. Is there a way to tell func that the first output is actually a function?
Yes, but you probably don't want to go that way. Can you explicitly
indicate the location of the 'output' function? e.g.:
py> def output(output=''):
.... print output
....
py> def func(output=''):
.... __import__(__name__).output(output)
....
py> func('abc')
abc
or possibly:
py> def func(output=''):
.... globals()['output'](output)
....
py> func('abc')
abc
This is easier if output is in another module -- you can just write
something like:
outputmodule.output(output)
STeVe
Bo Peng wrote: def func(output=''): output(output=output)
Naturally, I get 'str' object is not callable. Is there a way to tell func that the first output is actually a function? (like in C++, ::output(output) )
output_alias = output
def func (output=''):
output_alias(output=output)
Daniel
Bo Peng wrote: def func(output=''): output(output=output)
Naturally, I get 'str' object is not callable. Is there a way to tell func that the first output is actually a function? (like in C++, ::output(output) )
You could use a default argument:
def func(output='', output_fn=output):
output_fn(output=output)
Kent
Bo Peng a écrit : Dear list,
If you ask: why do you choose these names? The answer is: they need to be conformable with other functions, parameter names.
I have a function that pretty much like:
def output(output=''): print output
and now in another function, I need to call output function, with again keyword parameter output
def func(output=''): output(output=output)
Naturally, I get 'str' object is not callable. Is there a way to tell func that the first output is actually a function? (like in C++, ::output(output) )
Thanks. Bo
What I'd suggest is :
def func(output=''):
gobals()["output"](output=output)
that way, the function resolution is still dynamic, but you explicitly
ask for a name global and not local ...
Pierre
Kent Johnson wrote: Bo Peng wrote:
def func(output=''): output(output=output)
Naturally, I get 'str' object is not callable. Is there a way to tell func that the first output is actually a function? (like in C++, ::output(output) )
You could use a default argument: def func(output='', output_fn=output): output_fn(output=output)
Kent
Thank everyone for the quick responses. All methods work in general.
Since func() has to take the same parameter set as some other functins,
I can not use func(output='', output_fn=output). "output_alias=output"
etc are fine.
Bo
Bo Peng wrote: Thank everyone for the quick responses. All methods work in general. Since func() has to take the same parameter set as some other functins, I can not use func(output='', output_fn=output). "output_alias=output" etc are fine.
One suggestion that I haven't seen so far:
Where does this mysteriously named function 'output' come from? If it is
defined in another module, then simply call it qualified by the module
name.
i.e. if the code looks like:
from othermodule import output
def func(output=''):
output(output=output)
change it to:
import othermodule
def func(output=''):
othermodule.output(output=output)
Bo Peng wrote: def func(output=''): output(output=output)
Another solution that hasn't been mentioned yet:
def func(**kwds):
output(output = kwds['output'])
You might want to do some more checking on the
contents of kwds to make sure it doesn't
contain any other nonsense parameters.
--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand http://www.cosc.canterbury.ac.nz/~greg This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: chunmok |
last post by:
When compile using g++, following error messages appears.
/usr/include/sys/sysinfo.h:101: `struct sysinfo_t sysinfo' redeclared
as differe
nt kind of symbol
/usr/include/sys/systeminfo.h:77:...
|
by: John |
last post by:
I am using two large libraries which both have an implementation
of a "matrix" class. I have the source code for both.
I need to use them in the same project and when I try to compile
I get a...
|
by: Catherine Jones |
last post by:
Hi all, we need urgent help in a matter.
We are trying to pass a COM object from the client to server and are
facing some problems in the same.
We've our client in C# as well as the Server...
|
by: Wayne |
last post by:
I have the following Namesapces (only for example, would not name them this
way in production code)
Wayne.Test.Data
Wayne.Test.BR
With in Wayne.Test.Data name space I have the following...
|
by: tizmagik |
last post by:
I am having a lot of difficulty generating a CrossTab Query based
report. I have looked online for several tutorials and whatnot but I
have not been able to really find what I'm looking for, nor...
|
by: crystal |
last post by:
I've checked the threads but haven't been able to come up with a
solution to my issue. Help......
I have a simple form based on a table.
Within the form is a subform that is also, through a Q,...
|
by: laurentc |
last post by:
Dear all,
I have several tables based on exactly the same fields
(Key/Date/Price1/Price2).
I do some statistics on the prices.
However, as I have many different tables (the tables are...
|
by: mick |
last post by:
Hi,
if I add a record to a table in my database, everything goes well.
When I then place the table in another database and link to it from my
database I suddenly get a key conflict when I add a...
|
by: Meendar |
last post by:
Hi,
Below is my code snippet having only one form,
<form>
<input type ="radio" name="action" value="xyz" checked>xyz
<input type ="radio" name="action" value="zyx">zyx
<input type ="radio"...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
| |