OOP in Python
Object Oriented Programming
A defined class will have its own attributes and methods
An object can be created after a class is defined
Attributes are data that belong to an object
Methods are actionable code that the object can do
class
Keyword
class
KeywordThe example above is a class called "Car".
Within a
class
code block, we write all codes that would belong to the class.class
must be assigned a name, it follows the same name conventions as variables except that the name must be capitalized.__init(self)__
is an "initialization" method that auto runs when we create an object from a class.
__init__(self)
The initialization method
__init__(self)
The initialization methodThe __init__
special method, also known as a Constructor, is used to initialize the Car class with attributes such as make
, model
, and year
.
In Python, built-in classes are named in lower case, but user-defined classes are named in CamelCase, with the first letter capitalized.
Class Definition: First, you define a class. Inside the class, you can declare attributes (variables) that belong to objects of that class.
__init__()
Method: Within the class, you define the__init__()
method. This method takes at least one argument,self
, which is a reference to the object being created. It is a convention to name this parameterself
, but you can technically choose any name.self
allows you to access and modify the object's attributes within the method.Attribute Initialization: Inside the
__init__()
method, you set the initial values for the object's attributes usingself
. These attributes can vary from one instance to another, so you can customize them during object creation.Object Creation: When you create an instance of the class, the
__init__()
method is automatically called, and it initializes the attributes for that specific object.
Our example Car class
requires 3 arguments to create a car object: make, model,
and year
. We have these 3 arguments because we designed our class to have 3 attributes.
Attributes
Recall that: Attribute are the characteristics or qualities that an object of the class possesses as data of the object.
Attributes created in .
init
()
are called instance attributes. An instance attribute’s value is specific to a particular instance of the class.
Our Car class
has three instance attributes of make, model,
and year
. The init()
method also has three arguments to help initialize the three attributes our class requires.
We can access an object's attribute by using the dot notation and the referencing the name of the attribute like the example provided above.
Method vs Function
In Python, the key distinction between a method and a function lies in their association with objects. A function is a standalone block of code that takes input, performs operations, and returns a result.
In contrast, a method is a function that is associated with an object and is called on that object. Methods are invoked using dot notation on instances of a class and typically operate on the data within that instance.
For a class:
We can define what methods an object can use from the class. The definitiion is the same as creating a function, but you require one argument called
self
.The
car
class has two custom methods:start_engine()
andstop_engine()
The
car1
variable is aCar
object; therefore, we can invoke/use the methods that theCar
class has by usinga dot
then themethod name
. (Example:car1.start_engine()
)
In Python, self
is a convention used to represent the instance of a class.
It is the first parameter in the method definition of a class and refers to the instance of the class on which the method is invoked.
When you call a method on an object, the object itself is passed as the first parameter to the method, and by convention, this parameter is named self
.
Last updated