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.
Here is an example of making a very simple class with a single property:
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:
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:
Object Methods
Objects can also have methods. Object methods are functions for that object. We can add a function to the dog class:
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.
Modifying Object Properties
Here's how to modify object properties:
Deleting Objects and Object Properties
Deleting object properties and objects can be done with the del
command:
The Pass Statements
The pass statement is used to allow an empty class to continue.
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