By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Python was an object-oriented language from the day it was made. Let’s take a quick revision on OOP (Object Oriented Programming) concepts. OOP Concepts - Class: A class is a user-defined prototype for an object that defines a set of attributes. The attributes are the data members (class or instance variables) and methods that are usually accessed via dot notation. - Class variable: A class variable is the class reference that is shared by all instances of a class. Class variables are defined within a class but they are always outside any of the class's methods. - Data member: It is a class or instance variable that holds data associated with a class and its objects. - Instance: An individual object is an instance of a certain class. - Instantiation: The creation of an instance of a class is called instantiation that creates a class object. - Method: It is the name given to the function that is defined inside the class. It performs the actual operation on the variables. - Function overloading (Function Polymorphism): Two or more functions with the same name but performing the different operation based on number of parameters, data type, etc. - Operator overloading (Operator Polymorphism): A single operator has assignment of more than one function. E.g. ‘+’ operator doing the mathematical addition of two numbers as well as concatenation of two strings. - Inheritance: The transfer of the characteristics or traits from parent class to the child class. It encourages reusability. - Instance variable: It is a variable that is defined inside a method and just belongs only to the current instance of a class. - Object: It is a unique instance of a data structure (variables and methods) that's defined inside its class. Creating Classes In Python, the class statement creates a new class definition. It has the following syntax.
class ClassName: 'Optional class documentation string' class_suite/component statements
- The class has an optional documentation string, which can be accessed via ClassName.__doc__. - The class_suite consists of all the component statements that define class members, data attributes and functions.
Creating Instance Objects An instance of class is created by calling the class using class name and pass in whatever arguments its __init__ method accepts.
Accessing Attributes Attributes of the class can be accessed through the object's attributes using the dot operator with object.
Class demonstration in Python In the below example, we are going to create a class and then instantiate its three objects to access their attributes.
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.