Skip to content

Instantly share code, notes, and snippets.

@luizpericolo
Created August 1, 2016 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luizpericolo/3dfa3a7c3909d5e2075438b6fce3992b to your computer and use it in GitHub Desktop.
Save luizpericolo/3dfa3a7c3909d5e2075438b6fce3992b to your computer and use it in GitHub Desktop.
Aprendizados da aula ministrada por Luciano Ramalho

Things I learned today

Defensive coding

  • 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)

Dunder methods

** _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.

using isinstance

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.

Woah

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment