Built-in types

Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development.

Interpreted

The key to rapid development. My notes on Python REPL.

Objects

Objects are Python’s abstraction for data - Documentation

This practically means that functions, methods, modules are also objects. There is no ‘value’ entity in Python. Or ‘type’ entity. These are all objects.

Python makes no distinction between basic and derived types as other programming languages may do. Everything is an object, a specific instance of a category called type.

>>> import collections
>>> type(collections)
<class 'module'>
>>> type(collections.namedtuple)
<class 'function'>
>>> type(5)
<class 'int'>
>>> type([])
<class 'list'>

Functions are first class objects.

>>> len("hello")
5
>>> len.__doc__
'Return the number of items in a container.'

Attributes (.__doc__) are objects, owned by other objects. We can look them up by name. The type of an object defines the attributes it has, and the way we can access them.

High level

High level languages “may automate (or even hide entirely) significant areas of computing systems (e.g. memory management)”.

Every object has an identity, a type and a value. An object’s identity never changes once it has been created; you may think of it as the object’s address in memory. The ‘is’ operator compares the identity of two objects

Out of these, only the value of an object can change over time.

>>> a = 5
>>> b = 5
>>> a is b
True

Sometimes the language uses equal by value (implemented as a hashmap), instead of equal by id:

>>> d = {1: 'a', True: 'b', 1.0: 'c'}
>>> d
{1: 'c'}

Built-in types

>>> help('TYPES')

Another grouping

Terminology

Categories for objects with common operations:

Sequence

Container

Mutable / Immutable

Recipe: Mutable argument defaults are bad

Schoolbus from “Fluent Python”. Strange things happen when bus starts empty (passengers became an alias).

__init__.__defaults__

Think twice before assigning a mutable argument to a class attribute.

Recipe: Avoid mutable function arguments

Backlinks: The problem with dict

#python