


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Object-oriented programming was a huge conceptual change in 80s and 90s. Simula, Algol, Smalltalk were early O-O languages C++ was born from the language‘C’ Java was born
Our brains can only handle 7 +- 2 things at a time. We must create abstractions and think hierarchically. Function allow us to abstract sequence of operations. We also need to abstract groupings of data, something we call ‘complex data types’ or ‘classes’. Consider a social network site. What data might be recorded for each person in the system? Other examples of grouping data: Google result A color How about the Mastermind program? What data did we use that could have been grouped but wasn’t?
How would you represent a person in Python? ‘Lists’ don’t really help here as you have related but heterogeneous data. You want to name each part, e.g., person.lastName, person.firstName, etc.
The Class construct allows a programmer to define custom types ( programmer-defined types ). Similar syntax to function def, but it’s a class definition. Define your fields by setting them to initial values. class Person: # class definition name="" age= friends = [] Note that the variables are being set outside any function! Once you define a class, you can create instances of it. We call these instances objects. Create them with an object creation statement, set them using dot notation.
joe = Person() # this creates a Person object with name="" and age= bill = Person() # this creates a Person object with name="" and age= print joe.age # prints 0 joe.age= joe.name="Joe Williams" bill.name="William Joes" bill.age= Formally, object creation statements are of the form: = () Field assignment statements use "." to specify which field: .
Person is a class. joe and bill are instances of the class Person. joe and bill are objects of type Person. name and age are fields (also known as data members ) of the class Person. A class can also have functions defined within it (we’ll revisit this later).
The first parameter is usually, by convention, called self. Self refers to the object on the left-hand side of the "." in the object-oriented function call There are really still two parameters sent to the function distance.