Python is a beautiful programming language. It contains really useful features that make writing code more efficient than ever.

Here are 5 tips for writing more efficient and compact code.

🧵🧵🧵
1. Decorator

A decorator is a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure.

https://book.pythontips.com/en/latest/decorators.html
2. Generator

A generator is a special type of function which does not return a single value, instead, it returns an iterator object with a sequence of values.

https://www.tutorialsteacher.com/python/python-generator
3. Ternary operator (condition)

A Ternary operator allows you to quickly test a condition instead of a multiline if statement.

https://book.pythontips.com/en/latest/ternary_operators.html
4. Enumerate

Enumerate adds a counter to an iterable and returns it in a form of an enumerate object.

https://www.geeksforgeeks.org/enumerate-in-python/
5. setattr & getattr

The setattr function sets the value of the specified attribute of the specified object. The getattr method returns the value of the named attribute of an object.
Let me know what functions or features you find useful and are worth sharing!
You can follow @dannysteenman.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled: