Classes

By Anish Ahuja (9/20/2020)

Introduction

Python is an object-oriented programming language. Almost everything in Python is an object, with fields (properties) and methods (actions). Objects are a classification of variables and functions. Classes are used as templates, from which objects can be made.

Object-Oriented Programming

Object-oriented programming is a way of structuring and bundling properties and actions into objects. Objects are the basis of Python and correspond to items in your program. Object-oriented programming is used to represent real-world items as software objects with data. For example, an object could be a person. The person has properties of age, hair color, height, etc. They would also have specific actions, such as eating, sleeping, and playing video games. This is the basic idea of the basis of objects in Python.

There is also procedural programming, which structures a code as steps through functions to complete a task. Object-oriented programming is the combination of using objects as data through procedural programming but also as a source of structure to base your program on.

For more information, visit this link: https://realpython.com/python3-object-oriented-programming/

Classes

Classes are the blueprints for objects in programming. They contain data members and functions for the objects to inherit. This is important as all objects are simply instances of classes, which means they must all follow the structure of the blueprint class. For example, if we were making a game with different human characters, we could make a Person class, and create different objects with different qualities from this class. The Person class would have attributes such as height and weight, and each object would have different values for this. The Person class would also have basic methods that all of the objects would share.

Making Classes

To make a class, simply begin with the class keyword and name the class.

class <className>:
    '''
    attributes of the class
    '''

Here is an example of making a very simple class with a single property:

class myClass:
    x = 3

Creating Objects

When we wish to use the blueprints of the class to make items, you must create an object of the class. Here is an example of making an object of the class:

obj1 = myClass()
print (obj1.x)

With this above example, an object was made from myClass. The object has the same properties as the class so obj1.x is equal to 3.

The __init__() Function

To make classes that can have objects with modifiable attributes. When you make the object, custom values can be given to the properties or other necessary operations. Here is an example of making a dog class with a customizable name and age:

class dog:
    def __init__(self, name, age):
        self.name = name
        self.age = age
doggo = dog("Chuck", 4)
print (doggo.name)
print (doggo.age)

Object Methods

Objects can also have methods. Object methods are functions for that object. We can add a function to the dog class:

class dog:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def intro (self):
        print ("My dog's name is " + self.name)
doggo = dog("Chuck", 4)
print (doggo.name)
print (doggo.age)
doggo.intro()

The self Parameter

The self keyword is used to refer to the current instance of the class. This parameter does not need to be named self. It can be named anything, but it must be the first parameter of any of the functions in the class.

class dog:
    def __init__(cats, name, age):
        cats.name = name
        cats.age = age
    def intro (pineapple):
        print ("My dog's name is " + pineapple.name)
doggo = dog("Chuck", 4)
print (doggo.name)
print (doggo.age)
doggo.intro()

Modifying Object Properties

Here's how to modify object properties:

class dog:
    age = 40
doggo = dog()
doggo.age = 30
print (doggo.age)

Deleting Objects and Object Properties

Deleting object properties and objects can be done with the del command:

del doggo.age
del doggo

The Pass Statements

The pass statement is used to allow an empty class to continue.

class myClass:
    pass

Conclusion

In conclusion, classes are one of the most critical aspects of programming. In this lesson, we covered the concept of object-oriented programming, making classes, and using objects. In later lessons, we will learn the concept of inheritance, which allows you to make classes from other classes.

Last updated