top of page
Writer's pictureHui Wang

94. Machine learning: Python Object Oriented Programming (Part 1/4)

Object-oriented programming (OOP) is a programming paradigm that uses objects and classes to represent and manipulate data. Python supports OOP and provides several features such as inheritance, encapsulation, and polymorphism to make it easy to create and maintain complex software.



1. Object Oriented Thinking


Object-oriented thinking is a way of designing and organizing software that focuses on the objects and their interactions. In Python, OOP is based on the concepts of classes and objects.


When thinking in an object-oriented way, you begin by identifying the objects in the problem domain and the relationships between them. Each object represents a real-world entity or concept and has its own state (attributes) and behavior (methods).


For example, in a game, you might have objects such as a player, an enemy, and a weapon. Each object would have its own state (e.g. player's health, enemy's strength, weapon's damage) and behavior (e.g. player's move method, enemy's attack method, weapon's reload method).


Once you have identified the objects and their relationships, you can use classes to model and implement them in Python. A class is a blueprint for creating objects and defines the attributes and methods that the objects will have.



2. Designing Classes


You can define a class using the class keyword, followed by the name of the class. The class definition typically includes an __init__ method, which is called when an object is created from the class and is used to initialize the attributes of the class.


For example, consider a class Person, which represents a person and has the following attributes: name, age, and address.


The class also has a method print_info() which prints the details of the person.

class Person:
    def __init__(self, name, age, address):
        self.name = name
        self.age = age
        self.address = address
        
    def print_info(self):
        print(f'Name: {self.name}')
        print(f'Age: {self.age}')
        print(f'Address: {self.address}')

To create an object of the class Person, you use the class name followed by parentheses, and you pass in the required arguments to the __init__ method:

person1 = Person("John Smith", 25, "123 Main St.")
person1.print_info()

# Output:
# Name: John Smith
# Age: 25
# Address: 123 Main St.

It's also possible to add, update and remove attributes from the class objects.

person1.gender = "Male"
print(person1.gender) # Output: Male

del person1.age

You can also use the class to create multiple objects, each with its own state.

person2 = Person("Jane Doe", 30, "456 Park Ave.")
person2.print_info()

# Output:
# Name: Jane Doe
# Age: 30
# Address: 456 Park Ave.

3. Classes and Objects


A class is a blueprint for creating objects, which are instances of the class. A class defines the attributes (state) and methods (behavior) that the objects created from it will have.


A class is defined using the class keyword, followed by the name of the class. For example:

class Dog:
    pass

This creates a class called Dog with no attributes or methods.


The class definition typically includes an __init__ method, which is called when an object is created from the class and is used to initialize the attributes of the class. For example:

class Dog:
    def __init__(self, name):
        self.name = name

This class has an attribute called name, and it will be initialized when the object is created.


To create an object of the class Dog, you use the class name followed by parentheses, and you pass in the required arguments to the __init__ method:

dog1 = Dog("Fido")

This creates an object of the class Dog with the name "Fido".


The class can also have methods which are functions that are associated with the class and are able to operate on the attributes of the class.

class Dog:
    def __init__(self, name):
        self.name = name

    def bark(self):
        print("Woof!")

This class has a method called bark() which when invoked will print "Woof!"

dog1 = Dog("Fido")
dog1.bark() # Output: Woof!

In summary, a class in Python is a blueprint for creating objects, and an object is an instance of a class. A class defines the attributes and methods that the objects created from it will have. Objects can have their own state and behavior and they can also be used to create multiple objects each with its own state.

 

Follow me on:

Recent Posts

See All

Comments


bottom of page