Mathspp Blog

342 391,958 32,910
articles words lines of code

Deep unpacking (or nested unpacking) provides a more powerful way for you to write assignments in your code. Deep unpacking can be used to improve the readability of your code and help protect you against unexpected bugs. Learning about deep unpacking will also be very important in order to make the most out of the structural matching feature that is to be introduced in Python 3.10.

You are sunbathing when you decide to go and talk to some friends under a nearby sun umbrella, but first you want to get your feet wet in the water. What is the most efficient way to do this?

Recursion is a technique that you should have in your programming arsenal, but that doesn't mean you should always use recursion when writing Python code. Sometimes you should convert the recursion to another programming style or come up with a different algorithm altogether.

All Python objects can be used in expressions that should return a boolean value, like in an if or while statement. Python's built-in objects are usually Falsy (interpreted as False) when they are โ€œemptyโ€ or have โ€œno valueโ€ and otherwise they are Truthy (interpreted as True). You can define this behaviour explicitly for your own objects if you define the __bool__ dunder method.

Python's str and repr built-in methods are similar, but not the same. Use str to print nice-looking strings for end users and use repr for debugging purposes. Similarly, in your classes you should implement the __str__ and __repr__ dunder methods with these two use cases in mind.