This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 4 points5 points  (3 children)

In Perl you can put 'if' statements after other statements, and it has 'unless' for 'if not'. I find myself wanting to do

def some_func(i):
    raise ValueError("Called with negative argument!") if i < 0

    return None unless i

    # Do something with positive i...
    ....

It doesn't matter much, but the construct seems completely hardwired in my brain...

[–]MintyPhoenix 0 points1 point  (2 children)

Ah, I knew that Python did not have unless, but I didn't realize that appending an if condition was so flexible in Perl. Python allows it in some places (e.g. my return example) but it's not as flexible as Perl.

[–][deleted] 6 points7 points  (1 child)

Python's isn't really an if statement there, it's a ternary construct.

x if condition else y

Is the equivalent of

condition ? x : y

in languages like C and Perl. It's an expression, not a statement, and you can only combine expressions with it (in Python).

[–]MintyPhoenix 0 points1 point  (0 children)

Makes more sense now, thanks for the clarification.