Applications Google
Menu principal

Post a Comment On: Only Python

"Teaching an old Python keyword new tricks"

13 Comments -

1 – 13 of 13
Blogger Ian Bicking said...

I like the block syntax, but where does the error message go? Sometimes that's not necessary, but for a lot of contract issues it's not entirely clear why the restriction exists without an explanation, and it's nice to suggest proper usage.

6:33 PM

Blogger André Roberge said...

Hmmm to be honest, I didn't think about error messages. Good point... perhaps one needs an additional keyword (like "where") after all.. I'll have to think about that one!

6:48 PM

Blogger Hamish said...

Regarding where an error message might go, could each line of the assertion block not have an optional error message separated by a comma, similar to what the existing assert statement does?

5:39 AM

Anonymous Anonymous said...

Interesting how having a more pythonic syntax makes the static typing looks more optionnal, more acceptable and opens towards a different design perspective.
Alex

6:07 AM

Anonymous Anonymous said...

Alex? By tone and content of the previos comment I have the nagging suspicion this might be Alex Martelli speaking... In this case, use your power and draw
Guido's attention to this well-balanced syntax proposal, Alex!

8:04 AM

Anonymous Anonymous said...

Sorry but I'm only another (anonymous french) Alex even quite new to the python language.
Alex

9:04 AM

Anonymous Anonymous said...

I *very* much like the "isinstance(a, int)" form of this. It is, I feel, *far* more pythonic in nature: very explicit, and when read aloud does not require any innate knowledge of the language. It does what it says.

It is also probably wordy enough to encourage people to *not* use static typing, which *should* be one of the design goals. There are few cases where static typing is required, ergo it should be less easy to do than leaving things dynamic.

12:36 PM

Anonymous Anonymous said...

I don't like the way that your syntax requires a double set of indentation though I can see how it arises from the need to specify a name for the return value. I would find this easier to read (with dots to represent the indentation as the spaces seem to be stripped out :-/):

assert:
....isinstance(a, int)
....isinstance(b, int)
....a != b # or whatever
assert return c:
....isinstance(c, int)
....c != 0

But I'm still not sure that I like it!

12:50 PM

Blogger hathawsh said...

I really like this suggestion. May I suggest the following minor changes:

def gcd(a, b):
    """Return the greatest common divisor."""
    assert:
        "Both inputs must be integers"
        a: int
        b: int
        "Both inputs must be nonzero integers"
        a != 0 and b != 0
    assert return c:
        "This function should return a nonzero integer"
        c: int
        c != 0
    while a:
        a, b = b%a, a
    return b

In this version, you can mix docstrings in pre-conditions and post-conditions. Conditions that fail should include the preceding docstring in the exception. Also, the post-conditions are not embedded inside the pre-conditions and the post-conditions are less indented.

1:02 PM

Blogger Gheorghe said...

regarding error description maybe:
isinstance(a, int) or raise "Must be a number"
a: int or raise "Must be a number"
a<0 or raise "Must be less the zero"
But then will this look ugly ?:
((a>0 and b<5) or a < -2) or raise "whatever business logic description"

3:52 PM

Blogger Gheorghe said...

which way?

assert:
....a:int or raise "must be a number"
....((a > 0 and b < 5) or a < -2) or raise "some business logic description"

or ?

assert:
....a:int
....(a > 0 and b < 5) or a < -2
except TypeError:
....raise "must be a number"
except AssertionError:
....raise "some business logic description"

4:07 PM

Anonymous Anonymous said...

What's really missing in python today?

01 def gcd(a, b):
02 ____'''Returns the Greatest Common Divisor,
03 ____implementing Euclid's algorithm.
04 ____Input arguments must be integers;
05 ____return value is an integer.'''
06 ____assert isinstance(a, types.IntType)
07 ____assert isinstance(b, types.IntType)
08 ____while a:
09 ________a, b = b%a, a
10 ____assert isinstance(b, types.IntType)
11 ____return b

That keeps the return value assertion close to the return statement, where it belongs. If you want some explanatory text in the exception object then raise one explicitly as in:

06 ____if not (isinstance(a, types.IntType) 07 _______and isinstance(b, types.IntType)):
08 ________raise TypeError("GCD applies to pairs of integers.")

or

10 ____if not isinstance(b, types.IntType)"
11 ________raise TypeError("GCD was supposed to produce an integer.")

Already, I tend to liberally sprinkle my code with assert statements, often simply as a reminder to myself but also as a form of test-driven development or design by contract (I write the assertions first, then fill in the program logic).

5:11 PM

Blogger André Roberge said...

To Anonymous who wrote:
What's really missing in python today?

01 def gcd(a, b):
02 ____'''Returns the Greatest Common Divisor,
03 ____implementing Euclid's algorithm.
04 ____Input arguments must be integers;
05 ____return value is an integer.'''
06 ____assert isinstance(a, types.IntType)
07 ____assert isinstance(b, types.IntType)
====
This series of 3 posts was prompted by Guido van Rossum's musings about adding optional static typing and related features, with a proposed syntax that many people, including myself, found "non-pythonic". Of course, the simple example I used can be done, as you've shown explictly, with python's actual syntax. However, the features suggested by GvR require some fundamental change. I was just trying to contribute in my own way by suggesting a "more pythonic" syntax that could act as a bridge between Python as it is today and how it could become with static typing, interfaces, etc.
By all means, if you can show how GvR's suggestions could be implemented "nicely" using today's Python, I am sure that many people would be interested. (Let me emphasized that I do NOT write this to be sarcastic.) I am trying to generate a constructive "debate" with all those interested.

9:53 PM

Spammers: none shall pass.
You can use some HTML tags, such as <b>, <i>, <a>

Comments on this blog are restricted to team members.

You will be asked to sign in after submitting your comment.
Please prove you're not a robot