class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
Classes and Objects
In object-oriented programming (OOP), classes and objects are fundamental concepts that enable developers to create programs that are modular, reusable, and easy to understand.
Other than refreshing the basics of classses in Python, we’ll be expanding on some key concepts in OOP, which will be covered in the following sections:
- Inheritance: Allows a class to inherit attributes and methods from another class, promoting code reuse.
- Polymorphism: Enables objects of different classes to be treated as objects of a common superclass.
- Encapsulation: Bundles data and methods that operate on the data into a single unit or class, restricting direct access to some components.
- Abstraction: Defines a structure that must be followed by subclasses without specifying the implementation details, useful to define a common interface for different implementations.
Defining a Class
A class in Python is a blueprint for creating objects. It defines a set of attributes and methods that the objects created from the class can have.
Creating an Object
An object is an instance of a class. Creating an object means creating a specific instance of a class with its own unique attributes.
= Dog("Rex", 5)
my_dog my_dog.name
'Rex'
Methods
Methods are functions defined within a class that define the behaviors of the objects of the class.
Constructor / Initializer
A constructor method is a special method that is automatically called when a new instance (object) of a class is created. It is defined using the __init__
method. The primary purpose of the constructor is to initialize the new object’s state, i.e., to assign values to the object’s properties or to perform any initial setup necessary for the object to be used.
A standard method, on the other hand, is a function that is defined inside a class and is used to define behaviors or actions that the objects of the class can perform. Unlike the constructor method, which is called automatically when an object is created, methods must be called explicitly using the object.
class Dog:
def __init__(self, name, age): # initializer method
self.name = name
self.age = age
def get_age(self): # another method
return self.age
def set_age(self, age): # another method
self.age = age
Class Attributes
Class attributes are variables that are shared among all instances of a class. They are defined within the class construction but outside any of the instance methods. Class attributes are accessed using the class name or an instance of the class.
class Dog:
= "Canis familiaris"
species
def __init__(self, name, age):
self.name = name
self.age = age
print(Dog.species)
= Dog("Buddy", 3)
my_dog print(my_dog.species)
Canis familiaris
Canis familiaris
Example of Class
Below is a more complete example of the Dog
class, gathering together the previous bits, demonstrating how to define a class, create an object, and use its attributes and methods.
class Dog:
= "Canis familiaris"
species
def __init__(self, name, age):
self.name = name
self.age = age
def description(self):
return f"{self.name} is {self.age} years old"
def speak(self, sound):
return f"{self.name} says {sound}"
= Dog("Mikey", 6)
mikey
print(f"{mikey.name} is {mikey.age} years old.")
Mikey is 6 years old.
if mikey.species == "Canis familiaris":
print(f"{mikey.name} is a {mikey.species}.")
print(mikey.description())
print(mikey.speak("Woof Woof"))
Mikey is a Canis familiaris.
Mikey is 6 years old
Mikey says Woof Woof