python - Techniques for condensing code -
as understand it, python designed force people use indentation possible break rule. example:
y=[1,2,3] print('listy:') x in y: print(x)
now, can condense last 2 lines such:
for x in y:print(x)
but cannot do:
print('listy');for x in y:print(x)
but there way can?
first of all, should agree such tricks may of use. not often, though. example code in doctests. clear enough readable when compacted, , making compact makes less problems making "as readable possible". however, regular code joining lines not practice. when not able create breakpoint inside if
or for
statement, it's bigger problem line. coverage tools give more information in case not practice such tricks.
however, answering question, seems there no way want. there many limitations in using ;
. compound statements can not used ;
. these limitations reasonable, regret strict.
upd: if focused on making one-liner, there lot of tricks. example, generators , list comprehensions (instead of for
), reduce()
, on, , in python 3 print()
can used inside them.
Comments
Post a Comment