adding possibility for declaring a function in Matlab's way

It would be nice if you could write a function header like this (besides, of course, the current way): def result=functionname(params): ... result=something This would suffice for most functions, since you usually return one type of value, and it can be very convenient in certain cases. This is the way it is done e.g. in Matlab, which also has a huge user base. Some more details to the idea: - The return values are initialized to None. - Setting the return values does not need to be the last line in the function. - You can use the "return" statement just as before, but without arguments, to return from anywhere in the code. - If you specify arguments to the "return" statement, Python stops with an exception. - The return value can be a tuple: def (result1, result2, result3)=functionname(parameters) Some advantages: - You can easily see what the function returns, without having to read the function body, or hoping to find it in the comments. - You can initialize the return values (if None is not good enough), and then care about the cases where they change. - If you change the setup of the return value (e.g. insert a new item into the tuple), you do not need to change the "return" statement at possibly several places in the function body. - It is very easy to write the function call prototype: just copy the function declaration without the "def" and final colon. Python GUIs will be able to do the same, thus not only giving you the function parameter template automatically, but also the return value template. Some disadvantages: - I suggest it as an addition to the current way, so there isn't any serious disadvantage. One person may decide to use one way, one the other. - Of course, if you mix the two types of function declarations in your software, you may need to look at the function header to see which one you used in the specific case. - You need to be aware of both ways when reading someone else's code --- which is not hard, as both ways are quite easy to read. The idea at this stage of Python development may be surprising, but I hope that nevertheless you will consider it seriously. There has been a lot of experience and developlment regarding this in connection with Matlab, and I am sure that many of you know better than me how it would fit into Python's philosophy, and what consequences adding it may have. Thanks for your time, and best regard! Géza

On 2/11/2011 10:48 PM, Géza wrote:
Perhaps you should also suggest to the Matlab people that they add Python-style declarations to Matlab;-! After all, Python also has a huge user base.
This is a huge disadvantage. Everyone would have to learn two equivalent syntaxes instead of one, which would make the language much more difficult to learn. Python's syntax is essentially frozen except for possible minor additions that show some real gain. -- Terry Jan Reedy

Before suggesting "improvements" to Python (or anything else for that matter), it's helpful to identify exactly what problem you are trying to solve. I don't see one. And having multiple entirely different ways to do things for no good reason mean code is harder to read. Google TOOWTDI for more info. If you love this paradigm I suggest you write it this way: def foo(): global result result = None if bar() is not None: raise UnnecessaryException return result def bar(): pass # real code goes here --- Bruce New Puzzazz newsletter: http://j.mp/puzzazz-news-2011-02 On Fri, Feb 11, 2011 at 7:48 PM, Géza <kgeza7@gmail.com> wrote:

Géza writes:
This is probably true in Matlab, and *if* the programmer gives it a good name. But many programmers will care more about saving (len variable-name) keystrokes that about giving good names, so you will literally see def result = foo (**args): # code goes here So, is result a list? A function? An instance of some class? Maybe it's polymorphic. -1 overall; the syntax we have already is readable enough. Also, some of the things you might want to use this for probably can be done with decoraters.

Géza wrote:
This doesn't seem to be a great improvement over def functionname(params): ... result = something ... return result About the only one of your points it doesn't cover is returning from the middle of the function without an explicit value, which many people would regard as bad style anyway.
- It is very easy to write the function call prototype: just copy the function declaration without the "def" and final colon.
And delete any 'self' argument, default values, annotations, * and ** arguments, etc. etc. just as you have to do today... -- Greg

On 2/11/2011 10:48 PM, Géza wrote:
Perhaps you should also suggest to the Matlab people that they add Python-style declarations to Matlab;-! After all, Python also has a huge user base.
This is a huge disadvantage. Everyone would have to learn two equivalent syntaxes instead of one, which would make the language much more difficult to learn. Python's syntax is essentially frozen except for possible minor additions that show some real gain. -- Terry Jan Reedy

Before suggesting "improvements" to Python (or anything else for that matter), it's helpful to identify exactly what problem you are trying to solve. I don't see one. And having multiple entirely different ways to do things for no good reason mean code is harder to read. Google TOOWTDI for more info. If you love this paradigm I suggest you write it this way: def foo(): global result result = None if bar() is not None: raise UnnecessaryException return result def bar(): pass # real code goes here --- Bruce New Puzzazz newsletter: http://j.mp/puzzazz-news-2011-02 On Fri, Feb 11, 2011 at 7:48 PM, Géza <kgeza7@gmail.com> wrote:

Géza writes:
This is probably true in Matlab, and *if* the programmer gives it a good name. But many programmers will care more about saving (len variable-name) keystrokes that about giving good names, so you will literally see def result = foo (**args): # code goes here So, is result a list? A function? An instance of some class? Maybe it's polymorphic. -1 overall; the syntax we have already is readable enough. Also, some of the things you might want to use this for probably can be done with decoraters.

Géza wrote:
This doesn't seem to be a great improvement over def functionname(params): ... result = something ... return result About the only one of your points it doesn't cover is returning from the middle of the function without an explicit value, which many people would regard as bad style anyway.
- It is very easy to write the function call prototype: just copy the function declaration without the "def" and final colon.
And delete any 'self' argument, default values, annotations, * and ** arguments, etc. etc. just as you have to do today... -- Greg
participants (6)
-
Bruce Leban
-
Greg Ewing
-
Géza
-
Stefan Behnel
-
Stephen J. Turnbull
-
Terry Reedy