Thursday 15 July 2010

python - Would it be pythonic to use `or`, similar to how PHP would use `or die()`? -



python - Would it be pythonic to use `or`, similar to how PHP would use `or die()`? -

is pythonic utilize or, similar how php utilize or die()?

i have been using quiet or print(stuff) instead of if verbose: print(stuff) lately.

i think looks nicer, same thing, , saves on line. 1 improve other in terms of performance?

the bytecode both pretty much same me, don't know i'm looking at...

or

2 0 load_fast 0 (quiet) 3 jump_if_true_or_pop 15 6 load_global 0 (print) 9 load_const 1 ('foo') 12 call_function 1 (1 positional, 0 keyword pair) >> 15 pop_top 16 load_const 0 (none) 19 return_value

vs if

2 0 load_fast 0 (verbose) 3 pop_jump_if_false 19 3 6 load_global 0 (print) 9 load_const 1 ('bar') 12 call_function 1 (1 positional, 0 keyword pair) 15 pop_top 16 jump_forward 0 (to 19) >> 19 load_const 0 (none) 22 return_value

no, not pythonic. many of decision made along way have been discourage kind of coding.

the right way write obvious way:

if verbose: print(stuff)

or, if computing stuff in isn't expensive/dangerous/irrevocable, wrap print in function checks flag:

def printv(*args, **kwargs): if verbose: print(*args, **kwargs)

… so can this, concise you're going get:

printv(stuff)

or, better, utilize logging module instead of reinventing wheel.

but if stuff expensive, , want save line?

well, don't need save line, , doing sacrificing readability brevity; "readability counts" key part of the zen of python. python allow set one-line suite on same line condition:

if verbose: print(stuff)

as pep 8 puts it, "sometimes it's okay", it's "generally discouraged". guido set in email on ideas mailing list, "if must save line, utilize one-line if statement rather 'clever'. if must save line, don't want read code."

if want in look (which shouldn't, i'll explain below), or want technically follow pep 8 letter while blatantly violating spirit:

print(stuff) if verbose else none

so, what's not pythonic it?

for 1 thing, you're using result of print in expression, though print has no useful result, , called side effects. misleading.

and in general, python has 1 side effect per line, , happens far left possible, makes easy skim code , see what's changing. reinforced strong split between statements , expressions (and, e.g., assignments beingness statements), methods side effects idiomatically returning none instead of self, , on.

but ignoring that, using and , or short-circuiting, rather results, potentially confusing and, @ to the lowest degree in people's minds, ugly. that's why ternary look (spam if eggs else beans) added: stop people writing eggs , spam or beans. why confusing? 1 thing, doesn't read english language @ all, unless you've been reading more perl code actual english. another, it's easy accidentally utilize valid value happens falsey; know not that, , check that, when see if, don't here. note print(stuff) if verbose else none makes explicit creating value nil with; explicit improve implicit, when you're doing uncommon.

finally, performance: (a) cares, , (b) why not measure instead of seek guess reading bytecode don't understand?

in [511]: quiet = true in [512]: %timeit quiet or none 10000000 loops, best of 3: 48.2 ns per loop in [513]: verbose=false in [514]: %timeit if verbose: pass 10000000 loops, best of 3: 38.5 ns per loop

so there go, in fast-pass case, if statement 20% faster, not slower—and they're both fast it's unlikely ever impact programme anyway. if you're doing billion times in tight loop , need squeeze out performance, you're going want lift rest out of loop, if means repeating 2 near-clones of same code (especially considering loop without prints more fit cache, etc.).

if want know why, well, you'd have @ implementation of bytecodes on particular version of particular implementation care about… but likely, needing pop_top instead of having 1 merged previous operation part of difference.

python conditional-statements boolean-expression

No comments:

Post a Comment