Hi Darren,
I sometimes return a dict in cases like this:
fit = peakfit(x,y)
print fit['position']
print fit['height']
print fit['FWHM']
and so on...
Sometimes, the results I always want get returned on their own and optionally return all results:
p,h = peakfit(x,y) # return just the basic results
p,h,fit_extras = peakfit(x,y,full_output=True) # return everything
print fit_extras['FHWM']
(I think some scipy functions operate more-or-less like this, too. Perhaps without returning results in a dict, but merely as extra arguments.)
I have to agree with the opinions of Chris Barker and John Hunter cautioning against writing new code using an 'nargout-alike'. (When translating Matlab code to Python I can see that it might be useful.) Furthermore, I often find myself grabbing the results of a function call in one variable, stuffing it somewhere, and later, knowing it's a tuple, examining the elements later. This handy trick would be impossible with an nargout-based implementation.
To me, nargout, like so much of Matlab, is a brutish hack introduced because that language is not as capable or elegant as Python. (To digress further and probably show the era at which I left Matlab to be around 5.x, it seems to me that early design mistakes resulted in terrible backwards compatibility issues, as well. For example, I remember being driven mad trying to figure out whether parentheses were needed when calling certain functions. I guess for some they are required and for others they aren't. I think it would be very difficult to define the syntax for Matlab as done in the Python (Language) Reference Manual. And let's not get into issues of object oriented programming, types and 'everything is a (double precision) matrix'.)
Regardless of whether your sense of aesthetics is different than mine, the kind of low-level shenanigans suggested as a work-around seems, at the very least, brittle and prone to maintenance and portability issues. (Would that even work on IronPython or PyPy?)
Anyhow, if you're not scared by bytecode hacks in your initial toe-dipping forays into Python, I invite you to dive in!
Cheers!
Andrew
···
On Nov 16, 2004, at 4:51 PM, Darren Dale wrote:
Maybe my example wasn't clear. There are good reasons for using varargout and nargout in Matlab. If I write a function to fit a peak, I can return the position, height, FWHM and sumResiduals along with some information about how the peakfitting algorithm concluded. If a user calls [p,h,fwhm,sr] = peakfit(x,y), you use nargout to determine that the caller does not want the algorithm report.