- 1
Any ideas for more neet and clear code? Is it possible to use a sort of macro or inline inc(x) dec(x) instead of annoying x+=1?
12 odpowiedzi
+ 1
You can make inc(num) and dec(num) functions...
0
In python it is not possible to use increment / decrement operators like x ++ / x--. If you could put your code to help you clean or reduce it it would be another option.
0
n3v375, yes, indeed, I did ment exactly just the same you mean. Main disadvantage of this way is ‹significant› productivity loss (increased timing because of function call in each iteration). That is the reason to apply slightly different solution similar to macro or inline known in other languages. Is it possible in Python?
0
There is the range() function and it's pretty fast
"""
#inc
for each in range(10):
print(each)
#dec
for each in range(10,-1,-1):
print(each)
"""
- 1
Steven M,
There is the range() function and it's pretty fast
"""
#inc
for each in range(10):
print(each)
#dec
for each in range(10,-1,-1):
print(each)
"""
I see, this way is widely used and convenient, though in fact it perfectly suits for only viewing and processing <each> element of itetable or hashable. I ment other case, when you need to count only elements, which does suit to particular condition:
"""
counter = 0
for element in sequence:
if element in some_set and len(element) >= x:
counter += 1
- 1
Of cource, this case is too simple. Not surprising cases, when you deal with counters, which itself are elements of complex data structure, then expression, containing increment may semed enough monstruous.
When you take a glance at long complex expression, there would be nice to have neet increment statement alias.
- 1
Perhaps there are exists some sort of code viewers, that does work as "postprocessor", transforming standard conventional code before displaying it helping to read too scary (complex) expressions,
changing "+= 1 " to "++" FOR VIEW ONLY, not modifying original source?
If such kind of tools are not available for now, does anybody consider such instrument helpful?