Postfix conditionals

  • Thread starter GöktuÄŸ Kayaalp
  • Start date
G

Göktuğ Kayaalp

Hi,

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

The above piece of code is equivalent to this in Python:

py> for i in [False]:
... if not i
... break

I believe that the first example is superior to the second example when
the two is compared
for readability and intuitiveness. We already have a ternary statement
that looks similar,

py> print('hi') if True else None

so I reckon there would be no breakage in old code if this kind of
syntax was added. Ruby has
this, and AFAIK Perl also does.

I lack the knowledge of whether the community has opinions on this kind
of notation, so I am
posting this here instead of the ideas list. What are your thoughts on
this?

gk
 
G

Göktuğ Kayaalp

Göktu€ Kayaalp said:
py> for i in [False]:
... break if not i
Python is not Perl.
Well done! Good for you, that you know the fact; but you are not being
constructive.

Python is not C either, but we have the while loop. Python is not
Smalltalk, but we have
classes. Python is not LISP, but we have function literals.

Hopefully Guido did not have your approach back when he created Python,
or we'd have an
assembly language or something instead today.
 
B

BartC

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

The above piece of code is equivalent to this in Python:

py> for i in [False]:
... if not i
... break
What are your thoughts on this?

I develop my own language (not Python, but also dynamic and interpreted).

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.

What would be far more useful would be a proper 'switch' statement, but if
that's not in, then I don't think your proposal will get far!

(There are various clunky workarounds for switch - one idea is to use an
if-elseif chain, but that's just what it tries to avoid. Switch is
attractive for an interpreted language because - provided all cases are
constants, a bit of a problem in Python, because as soon as you give a name
to something, it's no longer constant - it can be implemented very
efficiently.)
 
G

Göktuğ Kayaalp

[comments inline]

BartC said:
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

The above piece of code is equivalent to this in Python:

py> for i in [False]:
... if not i
... break
What are your thoughts on this?

I develop my own language (not Python, but also dynamic and interpreted).

Would love to see that, if possible!
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.

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.
 
C

Chris Angelico

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.

I wouldn't have a problem with

if not i: break

in Python, as long as the condition is short. In something that reads
from a socket until the other end closes, for instance, I'm fine with
this:

while True:
data = sock.read(1024)
if not data: break
do_stuff_with(data)

which will stop as soon as sock.read() returns "", which it does when
the other end is gone. (I wrote something doing exactly this today,
and did exactly this. Probably could have made the code a bit simpler
if I could depend on Python 3.3, but it has to run on 2.7 and maybe
2.6 so I had to stick with their facilities.)

Yes, it's two statements, but a list comprehension is a whole pile of
statement-y things, and that's usually a single line. If it's doing
one conceptual action, it's okay to not split it.

ChrisA
 
B

BartC

GöktuğKayaalp said:
BartC said:
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 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!))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top