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:

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    Markdown is turned off in code blocks:
     [This is not a link](

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>