Object-oriented python
A Class is a blueprint of an object. It creates a set of attributes to characterize any object created from this class. An Object is an instance of a class. For example:
#it will print out: The fruit is tastier when it’s fresh!
The functions within a class are called methods. The argument to these functions is the word: self which is a reference to objects that are made based on this class. To reference instances (or objects) of the class, ‘self’ will always be the first parameter, but it need not be the only one. Defining the class Fruit did not create any objects, but only gave the blueprint for creating them: in this example, we created the instance banana. Because the keyword ‘self’ was a parameter of the methods as defined in the Fruit class, the banana object gets passed to the methods. The 'self’ parameter ensures that the methods have a way of referring to object attributes. The CONSTRUCTOR method The constructor method (also known as __init__ ) is used to initialize data and it will be the first definition of a class. For example:
If we add this to the Fruit class and call it:
#this will print out: “This is the constructor method”
"The fruit is tastier when it’s fresh!”
This is because the constructor method is automatically initialized. You should use this method to carry out any initializing you would like to do with your class objects. Instead of using the constructor method above, let’s create one that uses a 'name’ variable that we can assign to an object:
#this will output: "Banana is tastier when it’s fresh!”
We see that the name we passed to the object is being printed out. Because the constructor method is automatically initialized, we do not need to explicitly call it, only pass the arguments in the parentheses following the class name when we create a new instance of the class.
In the next post, I will be talking about instance methods, class methods and static methods, so stay tuned!















