Introduction to Object — Oriented Programming (OOP)
1. What is a class?
A class is a specification of features that generate a kind of object (X). It has its own in-context methods and variables that demonstrate how X is structured.
To define a class, all we need to do is type the reserved keyword
class that followed by the class’s name. The first letter of the name has to be capitalized:
def __init__(self, name):
self.name = name
print("Hi this is my pet, its name is %s" % (self.name))
2. What’s an instance of a class?
An instance of a class is also called an object of that class. The instance has its own content built upon the structures predefined by its class.
x = Pet('Ralphie')
In the example above, we have just instantiated the class Pet to create object x.
3. What’s the difference between a function and a method?
A function and a method are basically the same. A function is only called a ‘method’ when it is built inside a class, which means it’s a property of that class.
With that in mind, a method cannot be called independently; instead, it has to be ‘attached’ to the object with a dot (’.’) when called:
4. What’s the difference between class methods and instance methods?
- A class method is one that belongs to the class itself. It doesn’t require an instance.
- An instance method, as referred to its name, requires an instance in order to be called. This one needs to have
5. What is a child class and how do you make one?
- A child class (subclass or derived class) inherits from its parent class (superclass or base class). Besides getting all general features from its parent class, the child class has its own specifications.
- We can think of this concept as an ‘is — a’ relationship: A dog is a Pet (inheritance).
In the example above, we pass class Dog with Pet parameter inside the brackets. This indicates that class Dog inherits from Pet, and has features generated from Pet.
6. Can a class inherit only from its parent?
A class can inherit from multiple classes. We call this multiple inheritance.
class Dog(Pet, Mammal, Adorable):
There’s also multilevel inheritance, in which a class can inherit from another class, from another class, and so on.
In the case above, class Derived_1 has characteristics from class Base and itself; class Derived_2 has characteristics from class Base, class Derived_1 and itself.
7. What is
self is a conventional name, which refers to the object itself.
self is used to access other attributes or methods of the object from inside the method.
See the example below for further explanation.
8. What does the
__init__ method do?
__init__ function is called a constructor, or initializer, and is automatically called when we create a new instance of a class. Within that function, the newly created object is assigned to the parameter
def __init__(self, name, breed, age):
self.name = name
self.breed = breed
self.age = age
When we use the notation
.age are properties of the Dog objects (not of the Dog class) that are going to be created.
We can initialize an instance of the class Dog, like this:
y = Dog('Sam', 'Corgi', 3)
y now becomes an object of the class. Here, we pass 3 parameters ‘Sam’, ‘Corgi’ and 3, which end up as arguments
age in Dog’s
__init__ method respectively.
9. What are magic methods?
One way to recognize magic methods is the way they are written: Every magic method starts and ends with double underscore; like
__init__, as we all already know.
But how does it actually work?
Turns out, when we write this code:
x = Pet('Georgie')
Behind the screen, Python calls:
This means that
__init__ is the method for Python to initialize an instance of class Pet.
Also, if we have an equation like this:
a + b
Python then checks the definition of class K that a belongs to. If K has a method
__add__, Python will call:
The two examples above indicate the underlying mechanism of Python. Basically, the language itself is designed using OOP, codes are written within classes and these magic methods are built — in tools for Python to interpret the input and display the output based on how they are structured.
10. What’s the point of making classes?
When discussing the use of classes, we are actually issuing Object — Oriented Programming (OOP).
In OOP, we focus on the data and the objects themselves instead of the logic required to manipulate them. In other words, no matter how big a project may be, it can be broken down into smaller components that it is made of, which are classes and their properties/structures.
- Maintainability: Since codes are stored in distinct ‘packages’ (a.k.a classes) and cannot interfere with others, it’s easier to maintain codes, identify errors and such.
- Reusability: Classes, as mentioned, are constructed with templated methods and variables, which makes it easy to reuse code in new systems. Messages provide a predefined interface to an object’s data and functionality. With this interface, an object can be used in any context.
- Efficiency: Typical OOP features also include concepts such as inheritance or encapsulation. They force data to be better structured; also optimize development time and ensure coding accuracy.
- Scalability: OOP offers programmers the control over data on a massive scale. It allows hiding data intentionally so that it would minimize the chance of breaking the system if a new feature/method is added to the codes.