Lecture 18 Classes and Methods

1. Object-oriented features


Python is an object-oriented programming language, which means that it provides features that support object-oriented programming. It is not easy to define object-oriented programming, but we have already seen some of its characteristics:
• Programs are made up of object definitions and function definitions, and most of the computation is expressed in terms of operations on objects.
• Each object definition corresponds to some object or concept in the real world, and the functions that operate on that object correspond to the ways real-world objects interact.

Methods are just like functions, with two differences:
• Methods are defined inside a class definition in order to make the relationship between the class and the method explicit.
• The syntax for invoking a method is different from the syntax for calling a function.

Examples of printing objects:
(PLEASE note that, the parentheses after the 'class Time' at the top of the script is redundant. It was used often in older versions of python)


Please note that '%.2d' makes '09' happen in the results. If you have '%.3d', it will show you '009' in the results.


You can put the printing function into the class Time() as a method.


The two scripts got the same results. The first method has the class 'Time()' and the function 'print_time()' working independently. However, the second method has the function embedded into the class Time() and acts as a 'method' of the class Time(). In that case, the function can be called as an attribute of the class and being called as 'Time.print_time()' in the script (the dot notation).

The third way is even more concise.


By convention, the first parameter of a method is called self, so it would be more common to write print_time like this:


The textbook made a very intuitive explanation on the differences among these methods:
The reason for this convention is an implicit metaphor:
 • The syntax for a function call, print_time(start), suggests that the function is the active agent. It says something like, “Hey print_time! Here’s an object for you to print.”
 • In object-oriented programming, the objects are the active agents. A method invocation like start.print_time() says “Hey start! Please print yourself.

But sometimes shifting responsibility from the functions onto the objects makes it possible to write more versatile functions (or methods), and makes it easier to maintain and reuse code

2. One more example
Look at the following example and try to understand why we use 'self' there:


let's take baby-steps of this code and try to understand this:
This works:


This also works:


However, this doesn't work:


If I delete the 'start' variable in the last line of the script and it starts working. The reason is 'start' itself contains the attributes already. It is like calling the object 'start', hey, do this 'print_time()' fuction for yourself, so all its attributes are treated by the function 'print_time()'.
The 'variable' in the function is working as a parameter that represents the object 'start'. It is surprizing that this works as well.


However, a more common to do this is using 'self' to pass the attributes of the object to the function.
This just works perfectly:




Tasks:

1. Create a class called 'Score' which has the following attributes: Score.chemistry, Score.physics, Score.engineering, and Score.art. This class has a method called 'scoreAverage(course1,course2,course3,course4)', which can calculate the average of all courses and report a GPA according to the following table:


Create an instance called 'Tommy_score" from 'Score', provide four scores as the inputs and print out the GPA in the form of : 'Tommy has a GPA of xxxxx'