BY DIVAKAR KUMAR PANDAY
1. Variable Assignment
Similar to functions such as args and kwargs, you can use the same syntax in variable assignments. Using iterable variable assignment methods can be useful when merging dictionaries. But you need to be cautious as there are many common keys between these two dictionaries. The latest Python 3.9 offers a fresh new syntax for this exact problem.
2. Frozensets
You can use sets, which are unordered collections of distinct objects. These sets are mutable, which can help in changing them with add() and remove(). You can also use these immutable frozenset() but cannot change its values. Using the frozenset() as a dictionary key is not that useful but it can give more explicit intentional code.
3. Better than Lambdas
Using lambdas for quick and easy one-liners is pretty common. You can rarely use it to build multi-argument functions. These lambdas can be used to create elegant one-liner functions. Before you start coding lambdas everywhere, be aware that this is probably one of the most hated uses of syntax in Python.
4. Multiple conditionals
Cleaning up messy if-statements is possible with multiple conditionals. You can use the multiple conditionals to add more conditional statements and chain them together with bitwise operators.
5. Check if a variable exists
You can check for variables in both the global and local scopes using globals() and locals() respectively. Both scope functions return dictionaries to implement our dictionary merging syntax. The code then checks for both test1 and test2 in this merged scope dictionary.
Comments
Post a Comment