You can return a tuple instead change the syntax...
def res1, res2 = foo():
#code here
can be
def foo():
#code here
return (res1,res2)
Até mais!
Ass.: Arthur Julião
------------------------------------------------------------------------------------------------
"Quero que a estrada venha sempre até você e que o vento esteja sempre a seu
favor, quero que haja sempre uma cerveja em sua mão e que esteja ao seu lado
seu grande amor." (Tempo Ruim - A Arte do Insulto - Matanza)
2011/2/12 <python-ideas-request@python.org>
Send Python-ideas mailing list submissions to
python-ideas@python.org
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.python.org/mailman/listinfo/python-ideas
or, via email, send a message with subject or body 'help' to
python-ideas-request@python.org
You can reach the person managing the list at
python-ideas-owner@python.org
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Python-ideas digest..."
Today's Topics:
1. adding possibility for declaring a function in Matlab's way (G?za)
2. Re: adding possibility for declaring a function in Matlab's
way (Terry Reedy)
3. Re: adding possibility for declaring a function in Matlab's
way (Bruce Leban)
4. adding possibility for declaring a function in Matlab's way
(Stephen J. Turnbull)
----------------------------------------------------------------------
Message: 1
Date: Fri, 11 Feb 2011 19:48:38 -0800
From: G?za <kgeza7@gmail.com>
To: "python-ideas" <python-ideas@python.org>
Subject: [Python-ideas] adding possibility for declaring a function in
Matlab's way
Message-ID: <7C2FD842074A470387F4F83B280C22A3@GezaVAIO>
Content-Type: text/plain; format=flowed; charset="ISO-8859-1";
reply-type=original
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
------------------------------
Message: 2
Date: Fri, 11 Feb 2011 23:05:20 -0500
From: Terry Reedy <tjreedy@udel.edu>
To: python-ideas@python.org
Subject: Re: [Python-ideas] adding possibility for declaring a
function in Matlab's way
Message-ID: <ij50tv$tmn$1@dough.gmane.org>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
On 2/11/2011 10:48 PM, G?za wrote:
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.
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.
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.
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
------------------------------
Message: 3
Date: Fri, 11 Feb 2011 20:10:30 -0800
From: Bruce Leban <bruce@leapyear.org>
To: G?za <kgeza7@gmail.com>
Cc: python-ideas <python-ideas@python.org>
Subject: Re: [Python-ideas] adding possibility for declaring a
function in Matlab's way
Message-ID:
<AANLkTikZ=mdhsKwqO3DhBzBXPZKCDo5zn80RVy=BH4Mz@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
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:
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
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
http://mail.python.org/mailman/listinfo/python-ideas