[Python-bugs-list] [ python-Feature Requests-585915 ] assert return values

noreply@sourceforge.net noreply@sourceforge.net
Wed, 24 Jul 2002 08:42:43 -0700


Feature Requests item #585915, was opened at 2002-07-24 09:28
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=585915&group_id=5470

>Category: None
>Group: None
>Status: Open
Resolution: None
Priority: 5
Submitted By: Thomas Leonard (tal197)
Assigned to: Nobody/Anonymous (nobody)
Summary: assert return values

Initial Comment:
It would be nice to write:

def root(x):
  assert x >= 0
  assert return >= 0
  ...

or

def get_path(foo):
  if foo is None: return None
  assert return is not None
  ...

and have the 'return' assertion checked when the function
exits. 'return' is already a reserved keyword so the
syntax doesn't introduce any incompatibility.

This might make functions more self-documenting, since
asserts at the end / in the middle tend to get lost.

Even if the assert goes at the end, it's still clearer, eg:

def foo():
  ...
  assert min <= return <= max
  return bar(min, max)

vs

def foo():
  ...
  tmp = bar(min, max)
  assert min <= tmp <= max
  return tmp

Thanks!


----------------------------------------------------------------------

>Comment By: Tim Peters (tim_one)
Date: 2002-07-24 11:42

Message:
Logged In: YES 
user_id=31435

Re-opened but moved to the Feature Request tracker (it's 
certainly not "a bug", but there's no rule against asking for 
new features either, provided they're in the right place).

I don't believe there's any control flow in the suggestion, just 
that the name "return" be taken as being bound, in assert 
statements, to the possibly anonyous return expression.  
Eiffel does something very much like this for the benefit of 
expressing post-conditions.

I certainly agree this way of spelling it in Python has 
problems, though, and a PEP would be in order.

----------------------------------------------------------------------

Comment By: Jeremy Hylton (jhylton)
Date: 2002-07-24 11:06

Message:
Logged In: YES 
user_id=31392

I don't think this is a great feature.  It seems like
control flow via yield or return  could easily be overlooked
if it's inside an assert statement.

Regardless of whether I like the idea, the SF bug tracker
isn't the right place to make this suggestion.  The right
thing to do is write a PEP describing the feature in detail,
then come up with a patch to implement it.  If that's too
much work, but you really like the idea, you could try to
drum up interest on comp.lang.python.


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=585915&group_id=5470