- Copy lists passed as parameters to class so that the this that was passed is not changed. People rarely expect this. (1)
- Ensure parameter passed is an iterable. Fails fast. (2)
Example:
class Vector:
typecode = "d"
def __init__(self, components):
self._components = list(components) (1, 2)
** _repr_ **
_repr_ dictates how a class will be represented in debuggers, etc. We must always extract any class specific strings from class and metaclass information, as opposed to hard coding the class name as a string for instance, since it will work as expected for any subclass of this class.
Usage of isinstance should be kept to a minimum. Using it too much may indicate that polymorphism is not being well used.
We are "allowed" to use isinstance as much as we need when we do operator overloading since we must decide what to do when we operate on operand of this or that type.
However, we must compare to generic types. Check if operand is of type numbers.Integral instead of int. Numpy has integer types that are not a subclass of int, but of numbers.Integral. This allows better integration for extensions.