Title: Theory / Important / CS / Programming / Python / Manual{ / 9 Classes;
Tags: FromInstruction computer_to_instruction hasNotNewEffectReturnsMeaning declaration automatedNewLineAfterCommaSpace fromAlgorithm oldAlgoNewName questionExample todo_declaration
On every note of this deck, unless a note says otherwise:
Page: https://docs.python.org/3/tutorial/classes.html

Name: A generator
Page: 337
Chapter: 9
Section: 4.1
Notation: A function returning multiple times

Name: Aliasing
Definition: The fact that an object may have multiple names
Page: https://docs.python.org/3/tutorial/classes.html
Chapter: 9
Section: 1

Question: Interest of aliasing ?
Answer: Passing an argument to a function is quick, since it is done by reference and not by copy.
Page: https://docs.python.org/3/tutorial/classes.html
Chapter: 9
Section: 1

Question: Risk with aliasing ?
Answer: Having two names for the same object without knowing it. Hence a modification to one apply to the other
Page: https://docs.python.org/3/tutorial/classes.html
Chapter: 9
Section: 1

Question: When does the risk with aliasing disappear ?
Answer: When objects are not mutable.
Page: https://docs.python.org/3/tutorial/classes.html
Chapter: 9
Section: 1

Name: Namespace
Definition: A mapping from names to objects.
Page: https://docs.python.org/3/tutorial/classes.html
Chapter: 9
Section: 1
Example: Set of built-in names
Example2: global names in a module
Example3: Local names in a function invocation

Name: An attribute of z
Definition: A text t such that z.t exists
Page: https://docs.python.org/3/tutorial/classes.html
Chapter: 9
Section: 2
Variables: z an object

Instruction: del z.a
Effect: Remove a from the object names z
Language: Python
Chapter: 9
Section: 2
Variables: z an object,
a an attribute
Assuming: a is writable

Question: When is a function's name space created ?
Answer: When the function is called
Page: https://docs.python.org/3/tutorial/classes.html
Chapter: 9
Section: 2

Question: When is a function's name space deleted ?
Answer: When the function returns, or raise an uncatched exception
Page: https://docs.python.org/3/tutorial/classes.html
Chapter: 9
Section: 2

Name: scope of n
Definition: A textual region of a Python program where n is directly accessible.
Page: https://docs.python.org/3/tutorial/classes.html
Chapter: 9
Section: 2
Variables: n a namespace

Question: When accessing a variable, which is the considered order of scope ?
Answer: the innermost scope, which is searched first, contains the local names
Page: https://docs.python.org/3/tutorial/classes.html
Chapter: 9
Section: 2
Answer2: the scopes of any enclosing functions, which are searched starting with the nearest enclosing scope, contains non-local, but also non-global names
Answer3: the next-to-last scope contains the current module’s global names
Answer4: the outermost scope (searched last) is the namespace containing built-in names

Instruction: global v
Effect: All references and assignments to v refer to the module's scope
Language: Python
Chapter: 9
Section: 2

Instruction: nonlocal v
Effect: All references and assignments to v refer to the innermost name space not containing v which is not the inner namespace
Language: Python
Chapter: 9
Section: 2

Instruction: classs foo(p_1,...,p_n):
bar_1
...
bar_n
Page: https://docs.python.org/3/tutorial/classes.html
Meaning: Create a new namespace, containing each of the p_i, used as the local scope for the statements bar_i.
Executes each bar_i.
This is associated to the name foo
Language: Python
Chapter: 9
Section: 3.1, 5

Instruction: foo.bar
Effect: The value of bar in foo
Language: Python
Chapter: 9
Section: 3.1
Variables: foo a class
bar an element of its namespace

Instruction: foo(pars)
Effect: Create a new instance of the class.
Language: Python
Chapter: 9
Section: 3.1
Variables: foo a class

Instruction: o.foo(vars)
Effect: Equivalent to c.foo(o,vars) where c is the class of o
Language: Python
Chapter: 9
Section: 3.4
Variables: o an object

Instruction: A variable, not declared global, accessed without prefix
Language: Python
Context: In a class definition
Chapter: 9
Section: 3.4
Name: A class variable

Instruction: A variable, prefixed via self, or in general via an object's name
Language: Python
Context: In a class definition
Chapter: 9
Section: 3.4
Name: An object variable

Name: A data attribute
Definition: An attribute which is not a method attribute.
Page: https://docs.python.org/3/tutorial/classes.html
Chapter: 9
Section: 4
Context: In a class definition

Name: A method attribute
Definition: An attribute which is declared in the class's definition using def
Page: https://docs.python.org/3/tutorial/classes.html
Chapter: 9
Section: 4
Context: Python: In a class definition

Question: How to call an object method from another object method
Answer: Prefix the method by self. (assuming self is the name of the current object)
Page: https://docs.python.org/3/tutorial/classes.html
Chapter: 9
Section: 4

Instruction: o.__class__
Effect: The class of o
Language: Python
Chapter: 9
Section: 4
Variables: o an object

Question: How to call the version of m of c' if it is overrided in c ?
Answer: c'.m(self,args)
Page: https://docs.python.org/3/tutorial/classes.html
Chapter: 9
Section: 4
Variables: c a class inheriting of c',
m a method

Instruction: issubclass(c,c')
Language: Python
Chapter: 9
Section: 4
Returns: Whether c is a subclass of c'
c'.__subclasscheck__(c)
Variables: c,c' a class,

Smaller0: m starts with at least two underscore
Greater0: the actual name of m is _c__m, with m's leading underscore stripped
Page: https://docs.python.org/3/tutorial/classes.html
Chapter: 9
Section: 4
Variables: c a class,
m a name introduced in c
Smaller1: ends with at most one underscore
Name: Name mangling

Name: A private variable
Definition: A variable prefixed by an underscore
Page: https://docs.python.org/3/tutorial/classes.html
Chapter: 9
Section: 4
Variables: c a class,
m a name introduced in c

Instruction: m.__self__
Effect: the instance object with the method m()
Language: Python
Chapter: 9
Section: 4
Variables: m an instance method object
Extra: Not understood

Instruction: m.__func__
Effect: the function object corresponding to the method.
Language: Python
Chapter: 9
Section: 4
Variables: m an instance method object
Extra: Not understood

Name: An iterator
Definition: An object with a method __next__(), either returning something, or raison StopIteration.
Page: https://docs.python.org/3/tutorial/classes.html
Chapter: 9
Section: 4

Instruction: next(it)
Language: Python
Chapter: 9
Section: 4
Returns: The next element of the iterator
Variables: it an iterator
Exception: StopIteration if there is no more element in the iterator

Instruction: o.__iter__()
Language: Python
Chapter: 9
Section: 4
Returns: an iterator for the object o.
Variables: o an object
Abbreviation: iter(o)

Instruction: StopIteration
Effect: An iterator is called while having no more elements
Language: Python
Chapter: 9
Section: 4

Name: A generator
Definition: A method using the yield instruction
Page: https://docs.python.org/3/tutorial/classes.html
Chapter: 9
Section: 4

Instruction: iter(g)
Language: Python
Chapter: 9
Section: 4
Returns: An iterator, returning an element for each yielded value, and StopIteration when g halts
Variables: g a generator

Name: A linearization of C
Definition: A list of the ancestors of a class C, including the class itself, ordered from the nearest ancestor to the furthest,
Page: https://www.python.org/download/releases/2.3/mro/
Variables: C a class
Name2: The MRO of C

Name: Method Resolution Order
Definition: the set of rules that construct the linearizations
Page: https://www.python.org/download/releases/2.3/mro/
Abbreviation: MRO

Name: A monotonic MRO
Definition: if C1 precedes C2 in the linearization of C, then C1 precedes C2 in the linearization of any subclass of C.
Page: https://www.python.org/download/releases/2.3/mro/

Question: Example of a class hierarchy admitting no monotonic MRO
Answer: X precedes Y in A, and is preceded in B
Page: https://www.python.org/download/releases/2.3/mro/

Question: What occurs in python >2.3 when no monotonic MRO can be derived
Answer: raise exception: TypeError: MRO conflict among bases Y, X
Page: https://www.python.org/download/releases/2.3/mro/

Name: The MRO of C according to C3
Definition: C + merge(L[B_1] ... L[B_N], B_1 ... B_N)
Page: https://www.python.org/download/releases/2.3/mro/
Variables: C a class
Inheriting from B_1, ..., B_n
Notation: L[C]

Page: https://www.python.org/download/releases/2.3/mro/
Implementation: by induction on the sum of the size of the L_is
Let H_i the head of the list L_i
Let i minimal such that H_i does not occurs in a list L_j. Raise an exception if no such i exists.
Return H_i :: f(L'_1, ..., L'_n), where L'_j is equal to L_j without its potential occurence of H_i
Variables: L_1, ..., L_n lists
Name: merge(L_1, ..., L_n)
Language: Python
Returns: A list, containing the elements of the L_i, without repetition, such that if x<y in a list L_i, x<y in the result
Context: MRO, C3

Name: A bad MRO
Definition: A MRO not satisfying either local precedence ordering, or monotonicity
Page: https://www.python.org/download/releases/2.3/mro/
Context: MRO, C3, Python

Name: A MRO satisfying local precedence ordering
Definition: A MRO such that:
for a class C, with parents P_1, ..., P_n.
If its ordering of ancestors is A_1, ..., A_m the function associating to i the least integer j such that A_j, is an ancestor of B_i, is non-decreasing.
Page: https://www.python.org/download/releases/2.3/mro/
Context: Python

Question: What occurs if a class inherits twice directly from the same class
Answer: TypeError: duplicate base class A
Page: https://www.python.org/download/releases/2.3/mro/
Context: Python

Instruction: class foo(
  bar_1,
  ...,bar_n
):
plop
Page: https://www.python.org/download/releases/2.3/mro/,
  https://docs.python.org/3.7/tutorial/classes.html
Meaning: Create a class of name foo,
its attribute are searched in the order generated by the MRO applied to foo. Intuitively,
  attribute in foo are considered first,
  then in bar_1 and its ancestors ... then in bar_n. With exceptions if some ancestors of bar_i occurs as ancestors of bar_j with i<j
Language: Python
Chapter: 9
Section: 5.1

Instruction: super().foo in some part of D
Page: https://rhettinger.wordpress.com/2011/05/26/super-considered-super/
Effect: Let L the MRO of C. Then it represents the attribute foo in the first class occurring after D in L
Language: Python
Variables: C,D classes
C inherits from D
o an object of type C
Assuming: Called from o

Question: How to create a family of class, cooperating on super().m ?
Answer: Either each definition of m contains the same arguments, and those arguments are given to each occurrence of super().m()
Page: https://rhettinger.wordpress.com/2011/05/26/super-considered-super/
Variables: m a method name
Context: Python
Answer2: Or they all take **kwds, a dictionnary of named argument. They remove their argument from the dictionnary, and send the remaining of the dic to super().m()
Answer3: All classes inherits from a root class, which is always the last one called by the sequence of super()

Question: How to insert a non-cooperating class C in a family F of cooperating class
Answer: create a class adaptater, inheriting from the root of the family.
Page: https://rhettinger.wordpress.com/2011/05/26/super-considered-super/
Context: Python
Answer2: It has an attribute a which is an object of type C,
Answer3: It has a copy of each method of the family, and of the non-cooperating class. From them, calling super() for method of the family, and a.m for the methods of C

Name: A cooperating family of classes
Definition: all inherits from a common root
Page: https://rhettinger.wordpress.com/2011/05/26/super-considered-super/
Context: Python
Definition2: has a set (in mathematical sens) of methods m, such that each of those method call super().m(), appart from the method in the root
Conjdef: A set (in mathematical sens) of classes, satisfying: