parameter name conflict. How to solve?

B

Bo Peng

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++,
::eek:utput(output) )

Thanks.
Bo
 
S

Steven Bethard

Bo said:
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
 
D

Daniel Dittmar

Bo said:
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++,
::eek:utput(output) )

output_alias = output

def func (output=''):
output_alias(output=output)

Daniel
 
K

Kent Johnson

Bo said:
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++,
::eek:utput(output) )

You could use a default argument:
def func(output='', output_fn=output):
output_fn(output=output)

Kent
 
P

Pierre Barbier de Reuille

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++,
::eek:utput(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
 
B

Bo Peng

Kent said:
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
 
D

Duncan Booth

Bo said:
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)
 
G

Greg Ewing

Bo said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,222
Messages
2,571,140
Members
47,755
Latest member
Grazynkaa

Latest Threads

Top