Traits are classes which contain methods that can be used to extend
other classes, similar to mixins, with exception that traits do not use
inheritance. Instead, traits are composed into other classes. That is;
methods, properties and internal state are copied to master object.

The point is to improve code reusability by dividing code into simple
building blocks that can be then combined into actual classes.

Traits are meant to be small pieces of behavior (functions or classes) used to
extend other objects in a flexible, dynamic manner. Being small and independent
entities, they are easy to understand, maintain and test. Traits also give an
alternative approach in Python to handle diamond inheritance cases due to fact
that no inheritance is happening at all (not saying multiple inheritance is an
issue in Python).

The dynamic nature of traits enables some interesting use cases that are
unreachable for conventional inheritance; Any changes made to class or instance
are applied immediately, and they affect whole application. In practice, this
means it is possible to add new functionality to any class or instance and it
can be from your own module, some 3rd party module (e.g Django) or even Python’s
own internal classes (e.g. collections.OrderedDict).

For example, there is feature you would need from framework someone else has
written. Only thing to do is to write traits for those classes that needs to
be updated and extend them. After extending the classes, framework will behave
based on those extended classes. Or if there is need to alter the behavior only
some specific situation (or you just want to be careful), instances of classes
can be extended only.

Other example would be a situation, where you discover a bug in 3rd party
framework. Now you can create own solution safely, while waiting for the official
patch to appear. Updating the framework code won’t override your extensions
as they are applied dynamically. Your changes are only removed when you don’t
need them anymore.

In the simplest form, traits are very similar to any class, that is inherited
by some other class. That is a good way to approach traits in general; If you
can inherit some class, then you can also use it as a trait. Let’s look an
example:

To be effective with traits, one must have some knowledge about how to
write code that can be reused effectively through out the system. It also
helps to know something about good coding practices, for example:

Especially in Law of Demeter, the interfaces tend to bloat because many small
and specific functions needs to be implemented for classes. Traits can help to
keep interfaces more manageable since one trait would contain methods only for
some specific situation.

Traits can really shine, when the application is layered both vertically and
horizontally. Vertical layer basically means different components of the system,
such as: User, Address, Account, Wallet, Car, Computer, etc.
Horinzontal layers would contain: Security, Serialization, Rendering, etc.
One approach with traits for above layering would be to create modules for
horizontal parts and then create trait for each type object needing that
behavior. Finally, in your main module, you would combine traits into classes.

# This is a trait for address renderingclassAddress:defrender(self):data=dict(street=self.__street,number=self.__number)return"<p>Address: {street} {number}</p>".format(**data)classWallet:defrender(self):# It is extremely straight-forward to render money situation.return"<p>Money: 0€</p>"