Clean Code in Python
eBook - ePub

Clean Code in Python

Develop maintainable and efficient code, 2nd Edition

Mariano Anaya

Partager le livre
422 pages
ePUB (adapté aux mobiles)
Disponible sur iOS et Android
eBook - ePub

Clean Code in Python

Develop maintainable and efficient code, 2nd Edition

Mariano Anaya

Détails du livre
Aperçu du livre
Table des matières

À propos de ce livre

Tackle inefficiencies and errors the Pythonic wayKey Features‚ÄĘ Enhance your coding skills using the new features introduced in Python 3.9‚ÄĘ Implement the refactoring techniques and SOLID principles in Python‚ÄĘ Apply microservices to your legacy systems by implementing practical techniquesBook DescriptionExperienced professionals in every field face several instances of disorganization, poor readability, and testability due to unstructured code. With updated code and revised content aligned to the new features of Python 3.9, this second edition of Clean Code in Python will provide you with all the tools you need to overcome these obstacles and manage your projects successfully. The book begins by describing the basic elements of writing clean code and how it plays a key role in Python programming. You will learn about writing efficient and readable code using the Python standard library and best practices for software design. The book discusses object-oriented programming in Python and shows you how to use objects with descriptors and generators. It will also show you the design principles of software testing and how to resolve problems by implementing software design patterns in your code. In the concluding chapter, we break down a monolithic application into a microservices-based one starting from the code as the basis for a solid platform. By the end of this clean code book, you will be proficient in applying industry-approved coding practices to design clean, sustainable, and readable real-world Python code.What you will learn‚ÄĘ Set up a productive development environment by leveraging automatic tools‚ÄĘ Leverage the magic methods in Python to write better code, abstracting complexity away and encapsulating details‚ÄĘ Create advanced object-oriented designs using unique features of Python, such as descriptors‚ÄĘ Eliminate duplicated code by creating powerful abstractions using software engineering principles of object-oriented design‚ÄĘ Create Python-specific solutions using decorators and descriptors‚ÄĘ Refactor code effectively with the help of unit tests‚ÄĘ Build the foundations for solid architecture with a clean code base as its cornerstoneWho this book is forThis book is designed to benefit new as well as experienced programmers. It will appeal to team leads, software architects and senior software engineers who would like to write Pythonic code to save on costs and improve efficiency. The book assumes that you have a strong understanding of programming

Foire aux questions
Comment puis-je résilier mon abonnement ?
Il vous suffit de vous rendre dans la section compte dans param√®tres et de cliquer sur ¬ę¬†R√©silier l'abonnement¬†¬Ľ. C'est aussi simple que cela¬†! Une fois que vous aurez r√©sili√© votre abonnement, il restera actif pour le reste de la p√©riode pour laquelle vous avez pay√©. D√©couvrez-en plus ici.
Puis-je lire sur ma tablette ou mon smartphone ?
Pour le moment, tous nos livres en format ePub adaptés aux mobiles peuvent être téléchargés via l'application. La plupart de nos PDF sont également disponibles en téléchargement et les autres seront téléchargeables très prochainement. Découvrez-en plus ici.
Est-ce que Perlego impose des restrictions sur les livres dans la bibliothèque ?
Les deux abonnements vous donnent un accès complet à la bibliothèque et à toutes les fonctionnalités de Perlego. Les seules différences sont les tarifs ainsi que la période d'abonnement : avec l'abonnement annuel, vous économiserez environ 30 % par rapport à 12 mois d'abonnement mensuel.
Proposez-vous des réductions groupées ou des tarifs professionnels ?
Nous sommes un service d'abonnement √† des ouvrages universitaires en ligne, o√Ļ vous pouvez acc√©der √† toute une biblioth√®que pour un prix inf√©rieur √† celui d'un seul livre par mois. Avec plus d'un million de livres sur plus de 990¬†sujets, nous avons ce qu'il vous faut¬†! D√©couvrez-en plus ici.
Puis-je lire un livre autant de fois que je le souhaite ?
Recherchez le symbole √Čcouter sur votre prochain livre pour voir si vous pouvez l'√©couter. L'outil √Čcouter lit le texte √† haute voix pour vous, en surlignant le passage qui est en cours de lecture. Vous pouvez le mettre sur pause, l'acc√©l√©rer ou le ralentir. D√©couvrez-en plus ici.
Est-ce que Clean Code in Python est un PDF/ePUB en ligne ?
Oui, vous pouvez accéder à Clean Code in Python par Mariano Anaya en format PDF et/ou ePUB ainsi qu'à d'autres livres populaires dans Computer Science et Programming in Python. Nous disposons de plus d'un million d'ouvrages à découvrir dans notre catalogue.


Packt Publishing


Getting More Out of Our Objects with Descriptors

This chapter introduces a new concept that is more advanced in Python development since it features descriptors. Moreover, descriptors are not something programmers of other languages are familiar with, so there are no easy analogies or parallelisms to make.
Descriptors are another distinctive feature of Python that take object-oriented programming to another level, and their potential allows users to build more powerful and reusable abstractions. Most of the time, the full potential of descriptors is observed in libraries or frameworks.
In this chapter, we will achieve the following goals that relate to descriptors:
  • Understand what descriptors are, how they work, and how to implement them effectively
  • Analyze the two types of descriptors (data and non-data descriptors) in terms of their conceptual differences and implementation details
  • Reuse code effectively through descriptors
  • Analyze examples of good uses of descriptors, and how to take advantage of them for our API libraries

A first look at descriptors

First, we will explore the main idea behind descriptors to understand their mechanics and internal workings. Once this is clear, it will be easier to assimilate how the different types of descriptors work, which we will explore in the next section.
Once we have a general understanding of the idea behind descriptors, we will look at an example where their use gives us a cleaner and more Pythonic implementation.

The machinery behind descriptors

The way descriptors work is not all that complicated, but the problem with them is that there are a lot of caveats to take into consideration, so the implementation details are of the utmost importance here.
To implement descriptors, we need at least two classes. For this generic example, the client class will take advantage of the functionality we want to implement in the descriptor (this is generally just a domain model class, a regular abstraction we create for our solution), and the descriptor class will implement the logic of the descriptor itself.
A descriptor is, therefore, just an object that is an instance of a class that implements the descriptor protocol. This means that the interface of this class must contain at least one of the following magic methods (part of the descriptor protocol as of Python 3.6+):
  • __get__
  • __set__
  • __delete__
  • __set_name__
For the purposes of this initial high-level introduction, the following naming conventions will be used:
The domain-level abstraction that will take advantage of the functionality to be implemented by the descriptor. This class is said to be a client of the descriptor.
This class contains a class attribute (named descriptor by this convention), which is an instance of DescriptorClass.
The class that implements the descriptor itself. This class should implement some of the aforementioned magic methods that entail the descriptor protocol.
An instance of ClientClass.
client = ClientClass().
An instance of DescriptorClass.
descriptor = DescriptorClass().
This object is a class attribute that is placed in ClientClass.
Table 6.1: Descriptor naming conventions used in this chapter
This relationship is illustrated in Figure 6.1:
Picture 1
Figure 6.1: The relationship between ClientClass and DescriptorClass
A very important observation to keep in mind is that for this protocol to work, the descriptor object has to be defined as a class attribute. Creating this object as an instance attribute will not work, so it must be in the body of the class, and not in the __init__ method.
Always place the descriptor object as a class attribute!
On a slightly more critical note, readers can also note that it is possible to implement the descriptor protocol partially‚ÄĒnot all methods must always be defined; instead, we can implement only those we need, as we will see shortly.
So, now we have the structure in place‚ÄĒwe know what elements are set and how they interact. We need a class for the descriptor, another class that will consume the logic of the descriptor, which, in turn, will have a descriptor object (an instance of DescriptorClass) as a class attribute, and instances of ClientClass that will follow the descriptor protocol when we call for the attribute named descriptor. But now what? How does all of this fit into place at runtime?
Normally, when we have a regular class and we access its attributes, we simply obtain the objects as we expect them, and even their properties, as in the following example:
>>> class Attribute: ... value = 42 ... >>> class Client: ... attribute = Attribute() ... >>> Client().attribute <__main__.Attribute object at 0x...> >>> Client().attribute.value 42 
But, in the case of descriptors, something different happens. When an object is defined as a class attribute (and this one is a descriptor), when a client requests this attribute, instead of getting the object itself (as we would expect from the previous example), we get the result of having called the __get__ magic method.
Let's start with some simple code that only logs information about the context, and returns the same client object:
class DescriptorClass: def __get__(self, instance, owner): if instance is None: return self "Call: %s.__get__(%r, %r)", self.__class__.__name__, instance, owner ) return instance class ClientClass: descriptor = Descriptor...

Table des matières

Normes de citation pour Clean Code in Python
APA 6 Citation
Anaya, M. (2021). Clean Code in Python (2nd ed.). Packt Publishing. Retrieved from (Original work published 2021)
Chicago Citation
Anaya, Mariano. (2021) 2021. Clean Code in Python. 2nd ed. Packt Publishing.
Harvard Citation
Anaya, M. (2021) Clean Code in Python. 2nd edn. Packt Publishing. Available at: (Accessed: 15 October 2022).
MLA 7 Citation
Anaya, Mariano. Clean Code in Python. 2nd ed. Packt Publishing, 2021. Web. 15 Oct. 2022.