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.
The key to rapid development. My notes on Python REPL.
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 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'}
>>> help('TYPES')
Another grouping
Categories for objects with common operations:
in
,len()
,
min()
,max()
and orderedlist
, tuple
, str
,
bytes
dict
is not a sequence, but subscriptableset
is not a sequence and not
subscriptableContainer
.__iter__
Mutable / Immutable
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.
#pythonBacklinks: The problem with dict