python – intermediate cheatsheet

__repr__ and __str__

Python has two different ways to convert an object to a string: str() and repr(). Printing an object uses str(); printing a list containing an object uses str() for the list itself, but the implementation of list.__str__() calls repr() for the individual items.

So you should also overwrite __repr__(). A simple

__repr__ = __str__

New style class vs old style class

  • low-level constructors named new()

  • descriptors, a generalized way to customize attribute access

  • static methods and class methods

  • properties (computed attributes)

  • decorators (introduced in Python 2.4)

  • slots

  • a new Method Resolution Order (MRO)

now is Class(object): instead of Class():


getter and setter in python

@property -> getter

@property.setter -> setter

good article:

Read More