Connecting Tech Pros Worldwide Forums | Help | Site Map

parameter name conflict. How to solve?

Bo Peng
Guest
 
Posts: n/a
#1: Jul 18 '05
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

Steven Bethard
Guest
 
Posts: n/a
#2: Jul 18 '05

re: parameter name conflict. How to solve?


Bo Peng wrote:[color=blue]
> 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?[/color]

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
Daniel Dittmar
Guest
 
Posts: n/a
#3: Jul 18 '05

re: parameter name conflict. How to solve?


Bo Peng wrote:[color=blue]
> 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) )[/color]

output_alias = output

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

Daniel
Kent Johnson
Guest
 
Posts: n/a
#4: Jul 18 '05

re: parameter name conflict. How to solve?


Bo Peng wrote:[color=blue]
> 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) )[/color]

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

Kent
Pierre Barbier de Reuille
Guest
 
Posts: n/a
#5: Jul 18 '05

re: parameter name conflict. How to solve?


Bo Peng a écrit :[color=blue]
> 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[/color]

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
Bo Peng
Guest
 
Posts: n/a
#6: Jul 18 '05

re: parameter name conflict. How to solve?


Kent Johnson wrote:[color=blue]
> Bo Peng wrote:
>[color=green]
>> 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) )[/color]
>
>
> You could use a default argument:
> def func(output='', output_fn=output):
> output_fn(output=output)
>
> Kent[/color]

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
Duncan Booth
Guest
 
Posts: n/a
#7: Jul 18 '05

re: parameter name conflict. How to solve?


Bo Peng wrote:
[color=blue]
> 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.[/color]

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)

Greg Ewing
Guest
 
Posts: n/a
#8: Jul 18 '05

re: parameter name conflict. How to solve?


Bo Peng wrote:
[color=blue]
> def func(output=''):
> output(output=output)[/color]

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
Closed Thread