Postfix conditionals

BartC bc at freeuk.com
Tue Feb 4 05:00:43 EST 2014


"GöktuğKayaalp" <self at gkayaalp.com> wrote in message
news:mailman.6377.1391490975.18130.python-list at python.org...
> "BartC" <bc at freeuk.com> writes:
>
>> "Göktuğ Kayaalp" <self at gkayaalp.com> wrote in message
>> news:mailman.4966.1388953508.18130.python-list at python.org...
>>
>>> AFAIK, we do not have "postfix conditionals" in Python, i.e. a condition
>>> appended to a
>>> statement, which determines whether the statement runs or not:
>>>
>>>   py> for i in [False]:
>>>   ...     break if not i

>>> What are your thoughts on this?
>>
>> I develop my own language (not Python, but also dynamic and interpreted).

(First, some apologies; I thought the OP was dated February not January!)

> Would love to see that, if possible!

(If you're into Python, then I doubt it because I don't have classes or any
of those other Pythonic things that people like. However my language is a
lot less dynamic and therefore can be much faster. I have a few interesting
variations on statements as well; syntax is cheap and I don't know why many
languages, Python included, have such a paucity of control and selection
statements.

I also have a project that translates my syntax into Python; the intention
there was to be able to make use of some of its libraries because I don't
have many of my own!)

>> I have this feature, and it's OK, but not indispensible.  I varied it a
>> bit
>> by allowing 'if', 'when' and 'unless' as the conditionals, just to break
>> it
>> up a little. However, it just maps internally to a regular if-statement.
>>
>> In Python though, the normal way of writing 'break if not i' is about the
>> same length (in my language it's somewhat longer), so I can't see it
>> getting
>> much support.
>
> I do not really think that string length is not of much significance.
> The actual fact that disallows my proposal from being favoured/implemented
> is that in Python, `break', `return' and `continue' are statements and the
> community encourages having one statement per line, so that the source
> code
> is easily understandable.  With my proposal implemented, the language
> would
> would be encouraging having multiple statements in one line, that looks
> like a single statement, but is indeed a composition of two.

But, Python already allows you to combine two statements on a line, as in:

 if a: s
 while b: t

So your:

 s if a

is little different (although s will need to be restricted; 'if b if a' will
look a bit odd). And someone mentioned the ternary expression which looks
similar to your proposal.

I suppose you can also argue that the if-part is not a statement at all,
just an expression that is part of the statement (you'd have to redefine
break and other statements to have an optional condition). If written as:

 break(not i)

then it certainly won't look like two statements! Your proposal has the 
advantage in that it gives more dominance to the statement, making the code 
somewhat clearer, comparing with burying it inside an if-statement.

> I rather dislike the statement-orientedness of Python, but still, it is
> a good device of easening for the language developers and beginners when
> the fact that we use indentation to denote blocks is considered.

(I used to have a syntax where statements and expressions were
interchangeable. Now I have them distinct, which makes many things much
easier, it picks up more errors (and makes it simpler to translate to
translate into languages which aren't quite as expressive!))

-- 
Bartc 




More information about the Python-list mailing list