Think Python, Chs. 16, 17¶
Reading Notes
Pure Functions¶
Modifiers¶
- These are functions that modify the parameter objects.
1 2 3 4 5 6 7 8 9 10 | |
Anything that can be done with modifiers can be done also with pure functions.
Functional Programming:
Write pure functions whenever it is reasonable; resort to modifiers only if there is a compelling advantage.
Designed Development¶
- Example is of writing the time conversion function using Base 60 arithmetic.
Debugging¶
-
invariants -- requirements that should always be true. If they are not true, something has gone wrong.
-
Assert statement -- checks a given invariant, raises exception if the check fails.
1 2 3 4 | |
Classes & Methods¶
Object-oriented Features¶
-
Object-oriented programming language :
- Programs include class and method definitions;
- Most computation is expressed in terms of operations on objects;
- Objects can represent real-world things; methods can correspond to real-world interactions/operations.
-
A method is a function associated with a particular class.
-
Syntactic differences between methods and functions:
- Methods are defined inside a class definition to make the relationship between method-class explicit.
- Invoking a method takes different syntax from calling a function.
Printing Objects¶
1 2 3 | |
1 2 3 4 5 | |
Sometimes, shifting responsibility from the functions ontothe objects makes it possible to write more versatile functions (or methods), and makes it easier to maintain and reuse code.
Another example¶
1 2 3 4 5 | |
1 2 3 4 5 6 | |
- The subject,
start, gets assigned to the first parameter,self. -
The argument,
1337, gets assigned to the second parameter,seconds. -
A positional argument is one without a parameter name; not a keyword argument.
More complex example¶
- Here,
is_after()takes two Time objects as parameters. It is conventional to name the first parameterself, and the second,other.
1 2 3 4 | |
1 2 | |
The init method¶
- A special method, invoked when an object is instantiated. The full name is
__init__().
1 2 3 4 5 6 | |
self.hour = hour stores the value of the parameter hour as an attribute of self.
- The parameters are optional;
-
Calling the class without arguments \(\rightarrow\) default values.
-
Override the first parameter, and it assigns the value to
hour:
1 2 3 4 5 6 7 8 9 10 11 | |
... and so forth.
The __str__ method¶
- Will return a string representation of an object.
1 2 3 4 | |
- Write a new class \(\Rightarrow\) write
__init__for instantiation purposes, and__str__for debugging purposes.
Operator Overloading¶
- Define other special methods to specify the behavior of operators on programmer-defined types.
1 2 3 4 5 | |
1 2 3 4 | |
-
When we apply the
+operator toTimeobjects, Python invokes__add__. Printing the result invokes__str__. -
Operator overloading -- changing the behavior of an operator so that it works with programmer-defined types.
Type-based dispatch¶
- A version of
__add__that checks the type ofotherand invokes eitheradd_timeorincrement:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
-
built-in
isinstancetakes a value and a class object, returnsTrueif the value is an instance of the class. -
If
otheris aTimeobject,__add__invokesadd_time. Otherwise, it takes the parameter as a number, and invokesincrement. -
This is called type-based dispatch. The computation is sent to different methods based on the argument types.
-
This is not commutative, though. Putting the class object in the first parameter slot will result in a
TypeError.
The solution for this is __radd__, or right-side add. It is invoked when a Time object appears on the right side of the + operator.
1 2 3 4 | |
Polymorphism¶
- Functions that work with multiple data types are polymorphic.
- Can facilitate code reuse;
Interface & Implementation¶
- Design Principle \(\Rightarrow\) keep interfaces separate from implementations. The methods provided by a class should not depend on how the attributes are represented.