Week III Cheat Sheet

Class

Classes and objects are the two main aspects of object-oriented programming. An object is simply a collection of data (variables) and methods (functions) that act on those data. A class is the constructor, or a blueprint for that object.

Objects can store data using ordinary variables that belong to the object. Variables that belong to an object or class are referred to as fields. Objects can also have functionality by using functions that belong to a class. Such functions are called methods of the class. This terminology is important because it helps us to differentiate between functions and variables which are independent and those which belong to a class or object. Collectively, the fields and methods can be referred to as the attributes of that class.

self

Class methods have only one specific difference from ordinary functions - they must have an extra first name that has to be added to the beginning of the parameter list, but you do not give a value for this parameter when you call the method, Python will provide it. This particular variable refers to the object itself, and by convention, it is given the name self.

Methods

We have already discussed that classes/objects can have methods just like functions except that we have an extra self variable.

__init__ Method

There are many method names which have special significance in Python classes. The __init__ method is run as soon as an object of a class is instantiated. The method is useful to do any initialization (i.e. passing initial values to your object) .

Last updated