Is there any shorter way to write compound ‘if’ conditions in C++?

Published on

shorter way to write compound ‘if’ strchr() can be used to see if the character is in a list. const char* list = “ABCXZ”; if (strchr(list, ch)) { // ‘ch’ is ‘A’, ‘B’, ‘C’, ‘X’, or ‘Z’} ————————————— OR ————————————— In this case you could use a switch: switch (ch) { case ‘A’: case ‘B’: … Continue reading Is there any shorter way to write compound ‘if’ conditions in C++?

How to remove duplicates from a list in Python

Published on

Important Edit 2015 As @abarnert notes, the more_itertools library (pip install more_itertools) contains a unique_everseen function that is built to solve this problem without any unreadable (not seen.add)mutations in list comprehensions. This is also the fastest solution too: >>> from more_itertools import unique_everseen >>> items = [1, 2, 0, 1, 3, 2] >>> list(unique_everseen(items)) [1, … Continue reading How to remove duplicates from a list in Python

How to make two decorators in Python

Published on

If you are not into long explanations, see Paolo Bergantino’s answer. Decorator Basics Python’s functions are objects To understand decorators, you must first understand that functions are objects in Python. This has important consequences. Let’s see why with a simple example : def shout(word=”yes”): return word.capitalize()+”!” print(shout()) # outputs : ‘Yes!’ # As an object, … Continue reading How to make two decorators in Python