I didn’t say or imply that Python offers the full set of statement modifiers supported by Perl. But it is absolutely capable of providing the same semantics as the specific statement modifier you referred to.
The Perldoc says:
“if executes the statement once if and only if the condition is true.”
which is the same semantics as the Python code if condition: statement
, just a different syntax.
Likewise, Perl’s “unless” is semantically the same as Python’s if not condition: statement
. The others (while, until, for, foreach, when) do not have direct Python equivalents.
If you still wish to argue that if condition: statement
does not provide the same semantics as Perl’s statement if condition
, you’ll need to justify that argument with an example.
Okay.
I was talking about the control flow of the if
, which is vastly more important to notice than the return
in this case.
If you want to emphasise the return statement, it is easy enough to put it on its own line, with an indent, perhaps even a blank line after it to make it stand out. IDEs and programmer’s editors will also highlight the return
keyword.
In practice, I don’t think that the return
statement is that hard to miss. It either stands out because it is on a line of its own following the condition, or it stands out because the if
statement isn’t followed by an indented block, which is unusual.
On the other hand, if we swap the order, I do think that the if
may be easy to miss, especially if the return expression is complicated or very long. Most people, when skimming, read down the left side of the code, where they will read what appears to be an unconditional return.
But on the gripping hand, the apparent “dead code” after the return is a hint to look more closely:
return something(long and complicated) blah blah blah blah ... if condition
more(code).here()
fe()
fi()
fo()
fum()
Whichever way we look at it, I think the “emphasis” argument is not particularly strong.
The point here is not that the return if condition
syntax is unspeakably awful, or that people can’t get used to it, but that it is unnecessary. Python already supports the same semantics.
Line breaks are cheap and there is very little reason to insist on putting the return and the if on the same line, but if you want to, you can.
Okay.
Would you have been less offended if I had repeated the Perl motto “There’s more than one way to do it” instead?
I don’t know who first came up with that motto, but Larry Wall was using it in 1990 (before Python even existed!) so it’s surely not a put-down. I think that most Perl hackers would be proud to be associated with a language that gave them the freedom to express themselves with multiple ways of stating the same thing. In the Perl community, that’s a virtue.
On the other hand, Wall did say “Although the Perl Slogan is There’s More Than One Way to Do It, I hesitate to make 10 ways to do something”, so I’m glad I wrote 5 and not 10
Right, that’s my point.
I don’t understand your objection here. There’s no reason to bring statements into this, and your argument about English not translating here seems wrong. Sure it does.
# What am I doing on the weekend?
activity = "I will visit zoo" if forecast('weather') == 'fine' else "I'll watch DVDs"
# or if you prefer something more OOP:
activity = Visitor(zoo) if forecast('weather') == 'fine' else Watcher(dvd_collection.search('David Attenborough'))
If you want to bind the result of the ternary expression to a variable, you don’t need two separate assignments. One will do. But since Python now has the walrus operator, we can put an assignment in each clause of the ternary if
operator. It’s just ugly:
# Ewww ewww ewww.
(activity:=Visitor(zoo)) if forecast('weather') == 'fine' else (activity:=Watcher(dvd_collection.search('David Attenborough')))
Your objection here feels to me like mere arguing for the sake of an argument, and since you haven’t paid I’m not allowed to argue any more.
Except in my spare time.
shrug
I might ask why you consider x() if y else z()
so strange, when the equivalent conditional missing one clause x() if y
is clear to you. Just add an else
clause to what you are already used to.
We might argue that the Perl statement modifier is equivalent to the pseudocode statement if condition else pass
except that Perl has not yet got around to allowing an explicit else
clause.
As I said above, it’s not that x() if y
is “strange” or especially bad, as that it’s unnecessary and redundant (not to mention surplus to requirements, and any other synonyms you prefer) since we already have a perfectly cromulent good way of writing it: if y: x()
.
(Are we allowed to use cromulent non-ironically yet? I so want to use it but I fear people will think I’m being ironic.)
I’m not ashamed of the fact that I like the language choices Python has made, mostly. I think that, more often than not, they have good objective strengths as well as being subjectively nice. Python’s syntax is, mostly, a good fit for my brain. Why should I apologise for that? If I liked the design of Perl, I would be using Perl.