Re: parameter name conflict. How to solve?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: parameter name conflict. How to solve?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: parameter name conflict. How to solve?
Delaney, Timothy C (Timothy) wrote: Is this a style guide thing? Why not just: def func(output_param=''): output(output=output_param) This is exactly the problem. There are a bunch of other functions that use output='' parameter. Changing parameter name for this single function may cause confusion. Bo -- http://mail.python.org/mailman/listinfo/python-list
Re: parameter name conflict. How to solve?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: parameter name conflict. How to solve?
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) -- http://mail.python.org/mailman/listinfo/python-list
Re: parameter name conflict. How to solve?
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 -- http://mail.python.org/mailman/listinfo/python-list
RE: parameter name conflict. How to solve?
Bo Peng wrote: Is this a style guide thing? Why not just: def func(output_param=''): output(output=output_param) This is exactly the problem. There are a bunch of other functions that use output='' parameter. Changing parameter name for this single function may cause confusion. So why not rename the `output` function? Or alternatively, you could change the parameter name for everything (not recommended). I can assure you that when someone goes to maintain this code, having a function go through all kinds of schenanigans to support what you want to do will be much more confusing than a different parameter name. Tim Delaney -- http://mail.python.org/mailman/listinfo/python-list
Re: parameter name conflict. How to solve?
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 -- http://mail.python.org/mailman/listinfo/python-list
RE: parameter name conflict. How to solve?
Steven Bethard wrote: If you ask: why do you choose these names? The answer is: they need to be conformable with other functions, parameter names. Is this a style guide thing? 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) Why not just: def func(output_param=''): output(output=output_param) ? Tim Delaney -- http://mail.python.org/mailman/listinfo/python-list