Function from method: increment and decrement without function definition

TrustyTony

You can fix one parameter of function call by calling underlying method of fixed parameter instead of using functools.partial or lambda statement.

256 Views
About the Author

IT Pro doing Eng-Fin-Eng translations

inc = (1).__add__
print(inc(4))
#5
sub = (-1).__radd__
print(sub(5))
#4
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Actually, nothing beats the convenience of C language n++ and n--
The eggheads of Python have discussed this, but have not acted.

paddy3118 11 Light Poster

... The argument about n++ and n-- is that there is also --n and ++n and that C allows assigning as well as auto increment/decrement in one statement, which is both error prone and hard to maintain.

Overall, I'm happy with the decision to leave it out of Python. writing x += 1 and not having to read and debug some horrible x, y = ++y, x++ or whatever is fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.