Serengeti logo BLACK white bg w slogan

Python Book of Recipes #7 – Sharp your Python Developers skill

Boško Savić, Senior Software Developer

Welcome to the Python Kitchen, where we explore the art of coding with a dash of culinary creativity! In this culinary adventure, we will be showcasing a delightful menu of Python techniques that will enhance your development skills and make your code taste even better.

1. Class Methods for Customization and Code Reusability

image 11


  • Data Import: Class methods can be useful when dealing with data import scenarios. In the given example, the from_csv method enables developers to instantiate User objects directly from CSV data without having to manually split and process the string outside of the class.  This makes importing user data from CSV files or other data sources convenient.
  • Encapsulation: By encapsulating the logic for creating objects from specific data representations within the class method, developers can keep the implementation details hidden from the external code.
  • Customization: Class methods allow developers to define custom parsing and validation logic while creating objects. In the example, additional parsing or data transformation steps can be added to the from_csv method to handle special cases or validate the input data before instantiating the object.
  • Code Reusability: Class methods can be reused across different parts of the codebase or even in subclasses. In the given example, if there is another class, such as AdminUser, that inherits from User, the from_csv class method can be inherited and used without any modifications. This promotes code reuse and reduces duplication, leading to more maintainable and efficient code.
  • Testability: Class methods can make unit testing easier by providing a dedicated entry point for creating objects.

2. Don’t use this antipattern!

image 12

The improper use of exception handling demonstrated in the example above has the following implications:

  • Bug Detection: Using exception handling, developers can quickly identify bugs in their code. In the given example, if there is an error in the calculation, such as a ZeroDivisionError, it will be silently ignored.
  • Accurate Error Reporting: Catching specific exceptions provides more accurate error reporting. When an exception occurs, it carries valuable information about the specific error type, enabling developers to understand the root cause of the problem. With generic exception handling, this information is lost, making it harder to diagnose and fix the underlying issue.
  • Maintainability: Using specific exception handling, developers can enhance the maintainability of their code. By explicitly catching and handling relevant exceptions, the code becomes more readable and self-documenting, making it easier for future developers to understand the expected error cases and the corresponding error handling logic.
  • Avoiding Silent Failures: Using generic exception handling, such as except pass, can lead to silent failures, where errors are suppressed, and the code continues execution without addressing the issue. This can result in incorrect calculations, data corruption, or unexpected behavior that may go unnoticed until it causes significant problems.

3. List Comprehensions

image 13


  • Readability: List comprehension provides a more concise and expressive way to define data structures. The one-liner approach is often easier to read and understand, especially for developers unfamiliar with list comprehension.
  • Code Efficiency: List comprehension enables you to write code more efficiently by reducing the number of lines required to achieve the same result. They provide a compact and elegant solution, which can improve code maintainability and reduce the potential for errors.
  • Code Consistency: By adopting list comprehensions, you promote consistency in your codebase. List comprehensions are widely used and recognized by Python developers, making your code more familiar and intuitive to others.

4. Transform Your Function with Yield

image 14


  • Memory Efficiency: Using a generator with yield, you avoid loading the entire CSV file into memory. Instead, you read and yield rows one by one, reducing the memory footprint. This is particularly advantageous when dealing with large CSV files that cannot fit entirely into memory.
  • Streaming and Scalability: Generators allow for streaming data processing, enabling you to process CSV files of any size. As you yield rows incrementally, you can start processing and consuming data immediately, without waiting for the entire file to be read.
  • Flexibility and Modularity: By encapsulating the CSV file processing logic within the generator function, your code becomes more flexible and modular. You can easily modify or extend the processing operations without having to rewrite the entire CSV file reading code.

5. Simplify and Enhance Classes with Data Classes

image 15


  • Concise Initialization: Data classes automatically generate an __init__ method for you, eliminating the need to write it explicitly. Instance variables are defined directly within the class body, making the initialization code concise and readable.
  • Default Values: Data classes allow you to provide default values, for instance variables. In this example, no default values are specified, but you can easily add them if desired.
  • Automatic Method Generation: The @dataclass decorator automatically generates commonly used methods like __repr__ and __eq__. This reduces the amount of repetitive code you need to write and makes the class more maintainable.
  • Type Hints: By including type hints in the data class definition, you provide documentation and improve code readability. Other developers can easily understand the expected types for each attribute, enhancing code clarity and reducing potential errors.

6. Ensure Code Quality and Robustness with Unit Tests

image 16


  • Code Quality: Unit tests promote code quality by providing a mechanism to detect and fix bugs early in the development cycle. By writing tests that cover different scenarios and edge cases, you can identify and address issues before they impact the overall functionality of your code. This leads to more robust and reliable software.
  • Confidence in Refactoring: Unit tests act as a safety net when making changes or refactoring existing code. They allow you to verify that the modified code continues to produce the expected results. With proper test coverage, you can confidently refactor or optimize your code without fear of introducing new bugs or regressions.
  • Documentation and Collaboration: Unit tests serve as executable documentation that illustrates the expected behavior of your code. They provide insights into how the code should be used and what results to expect. Additionally, tests facilitate collaboration among developers, as they can easily understand the intent of the code and verify its correctness by examining the associated unit tests.

7. Logging

image 17


  • Insight into Runtime Behavior: Logging allows you to capture information about the execution flow of your application. In the provided example, logging an error message when a customer ID is out of range provides valuable insight into potential issues that might occur during runtime. This information can be instrumental in identifying and addressing problems.
  • Production Monitoring: By incorporating logging into your application, you can collect logs and monitor them in a production environment. Log messages can provide telemetry and information about the application's behavior, allowing you to detect anomalies, troubleshoot errors, and make informed decisions based on real-time data.
  • Efficient Troubleshooting: When errors occur, logging provides a way to quickly identify the problematic areas of your code. Log messages act as breadcrumbs, guiding you through the execution path and helping you understand what went wrong. This can significantly reduce the time and effort required for debugging and troubleshooting, especially in complex or distributed systems.


By leveraging these techniques and concepts, developers can enhance their Python skills, streamline development processes, improve code quality and maintainability, and gain deeper insights into their applications' runtime behavior. Happy Pythoning!

Let's do business

The project was co-financed by the European Union from the European Regional Development Fund. The content of the site is the sole responsibility of Serengeti ltd.