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? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 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?

24th May 2020, 3:48 PM
John
John - avatar
7 Answers
+ 1
You can make inc(num) and dec(num) functions...
24th May 2020, 4:28 PM
Steven M
Steven M - avatar
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.
24th May 2020, 3:51 PM
Josshual A. Toro M.
Josshual A. Toro M. - avatar
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?
24th May 2020, 6:55 PM
John
John - avatar
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) """
24th May 2020, 7:41 PM
Steven M
Steven M - avatar
- 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
25th May 2020, 4:01 AM
John
John - avatar
- 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.
25th May 2020, 4:29 AM
John
John - avatar
- 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?
25th May 2020, 4:45 AM
John
John - avatar