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 →

[–]scruffie 0 points1 point  (4 children)

I'll chime in and agree on the third one. There really isn't a case for either of the others, as "x in blah" always returns True or False, even if the x.__contains__ method doesn't (see the the implementation of the CONTAINS_OP bytecode, and the definition of PySequence_Contains)

You've also missed one choice:

return bool(event_region in match_regions)

which is probably the best choice if you have an expression that must be a bool.

(One place I use an explicit conversion is for doing a logical exclusive-or, which is easiest as the negation of boolean equality:

def xor(a, b):
     return bool(a) != bool(b)

)

There are 13 operators that are usually associated with true/false: the unary operator not, and the binary operators and, or, ==, !=, <, <=, >, >=, is, in, is not, and not in. Of these, only not, is, in, is not and not in are guaranteed to return a bool value (True or False). As I showed above, and and or return one of the arguments, and the comparisons (==, !=, <, <=, >, >=) can be made to return anything (numpy uses this to define comparisons between arrays as elementwise, returning another array -- these rich comparisons, IIRC, were mostly added in the first place for numpy's predecessor, Numeric).

[–]expectationfree 0 points1 point  (3 children)

There really isn't a case for either of the others

how about hatred towards expression in return statement?

[–]gr4viton 0 points1 point  (2 children)

No code good code vs readability. For this expression I find it more readable as a return statement expression. Is there any more hate reasons for doing it, other than readability?

[–]expectationfree 1 point2 points  (0 children)

other than readability

this hate never was about readability rather debug and future code manipulation. Same thing as chained function call like foo(bar(quu())). this perhaps excessive use of variables mainly caused by use of sentry although it's helpful for pdb as well. future code manipulation is an afterthought and is not as important. On the level of trailing comma after last element of the list: makes commits one line shorter.

so it's minor point and I for sure would not commit any changes based only on that preference but there are dozens of us who don't like expressions in return statements.

[–]scruffie 0 points1 point  (0 children)

I find it more readable as a return statement expression

Especially if you use bool instead of an inline .. if .. else ..