Site Tools


python:start

Python

Overloading

Single dispatch

Overload in typing library

Is for type checking only, the actual implementation is still just done in one function. I guess these days since you can define multiple input types and return types with type hints i guess this decorator may be considered obsolete.

Overload (python docs)

Inheritance

Inheritence is very tight coupling. Should be used with care as complex inheritance hierachies can make maintence very complicated. Try not to go deeper than 1 level of inheritance.

Abstraction

Abstract base classes

Concept similar to interfaces, its a contract for sub classes.

Term abstract base class

Abstract base class

Protocol classes

Defines a interface by defining the properties and functions a class can adhere to. Its more close to duck typing. Does not rely on inheritance. In runtime the interpreter checks if all the members match.

Protocols allow for finer granularity (interface segregation), more protocols can be combined in a class before it becomes messy as there is less tight coupling in code.

Multiple inheritance

Python supports multiple inheritance. So a class can have multiple parent classes. The method resolution order (left to right by defined clases) is important to take into account as it defines which method in parent class gets called if the same method exists in multiple classes.

The multiple inheritance feature is also named mixins as you mix multiple types of classes actually multiple inheritance tree's together.

When using mixins you need to know the implementation details of classes to figure out a resolution order that will work. So the class structure can become complex and hard to work with.

Arjan advises against using mixins, and when used the parent classes should only have methods so that initialisation issues (think of resolution order) with parameters are avoided.

Mixin classes should not have instance variables.

So its powerfull but try to avoid and use with care as it will get complicated.

According to Python Mixin (Pythontutorial.net) a mixin class can be seen as the interface concept but with implementation. “A mixin bundles a set of methods for reuse”

__slots__

slots allow us to explicitly declare data members (like properties) and deny the creation of dict and weakref (unless explicitly declared in slots or available in a parent.)

(wiki.python.org) Using slots

(docs.python.org) Slots

PyGame

Pygame CE is a fork of pygame and continues to be updated where development of pygame has stopped.

python/start.txt · Last modified: by Niels