Hiding Attributes, Equality and Reduce
The last two lectures (November 18 and 20) consisted of material I have covered in the past, as well as material I have never touched on. Hiding attributes was a concept I learned back in high school, so the reasoning why one would hide attributes was not new to me. The main thing I learned regarding this during those lectures was how to hide attributes in Python. We learned that it was through putting an underscore in the variable name (i.e. self._char instead of self.char) and the use of the built-in function property(), which can either make a certain attribute or variable read-only or re-writable. I found the built-in function property() to be extremely useful for hiding important attributes from programmers who change the internal coding.
Equality, otherwise known as __eq__ in Python, is used to change what the notion of equivalence means. The standard operator for equality, ==, only asserts the boolean True if the two objects are the same, meaning that they are referenced at the same memory location. The special function __eq__ allows the programmer to customize '==' as whether or not two objects are identical to one another, rather than having to be located in the same memory location. This way, the boolean True is always asserted if two objects are identical to one another, even if they are not the same object! This customization can apply to other situations, such as changing what '+' means or what '*' means, making the concept of customizing built-in functions quite useful for programmers.
Reduce() is a built-in function that reduces a list or an iterable to a single value. As Professor Heap stated during the lecture, say there is a list of integer values and I wanted to add those those values together. The function reduce() takes in three arguments (in this case, the function that does the actual addition of values, the list, and the value that would be returned if the list was empty, which is 0) and reduces the list produced to a single value; that value would be the total sum of the numbers in the list. It is a confusing concept to grasp, and I am still understanding exactly how it works. The best way I look at the concept of reduce() is with the built-in functions sum(), max() and min(). These functions are, in a way, reduce() functions; they iterate of a list or iterable and reduce the list to a single value (sum() reducing the list to the sum of the list, max() reducing the list to the maximum value of the list, and min() reducing the list to the minimum value of the list).