What are Python classes?
If you haven’t yet grasped the basics of Python classes, here’s where you can learn about the purpose, the syntax and the characteristics of classes in Python in just five minutes.
Classes are defined as a logical grouping of function and data. They can also be thought of as a blueprint, and you can create a lot of objects from that blueprint, technically known as “Instance”.
All methods of a class have access to all instance data of the class. A class is a container for shared state, combined with functions (methods) that execute or operate on that state. By placing state variables into member fields, they are available to all the methods of the class, without any need to pass as parameters.
SEE ALSO: Why learn Python?
A variation on the theme of shared data is to have one function that creates some data, and other functions that operate on the data. Its constructor parses data from an HTTP FORM submission and stores the data in instance data.
Characteristics of Python Classes
Python classes deliver all the standard features of Object Oriented Programming:
- The class inheritance mechanism enables multiple base classes
- A derived class can override any methods of its base class or classes
- A method can call the method of a base class with the same name
Let’s take a look at the syntax.
In Python, keyword “class” is used to define a class. They are created at runtime, and can be modified further after creation.
class myClass(): def method1(self): print "Guru99" def main(): # exercise the class methods c = myClass () c.method1() if __name__== "__main__": main()
This will give the output as “guru99”.
While using the class, you will also sometime see __init__(self). By using init you will be able to save a couple of lines of code.
SEE ALSO: What is a function in Python?
In Python, you can make attributes pass (variable inside a method) to the class and method that will get inside of the method, and independently for each object. In a method which is a function inside of a class, the first parameter is always “self”. “Self” is nothing but referring to the object you passed into the method.
All Python classes are subclasses of the special class named object. In class inheritance, when one class calls the other (parent) class to get most of its features, the child and parent class can interact in three different ways.
- Actions on the child class imply an action on the parent class
- Actions on the child class override the action on the parent class
- Actions on the child class alter the action on the parent class
A subclass that is able to inherit from more than one parent class is enabled to access functionality from both of them. The most useful and simplest form of multiple inheritance is known as a mixin.
Usually, a mixin is a superclass that is not supposed to exist on its own, but is supposed to be inherited by some other class to provide extra functionality.
Python Class Attributes
Classes have five predefined attributes:
- Tuple of classes
- String or None
The difference between the class attribute and instance attribute is that, in the class attribute case, it refers to a single object. In the instance-attribute-set-at-instantiation, it can refer to multiple objects.
This Python tutorial series was provided by Guru99 – hands-on tutorials for programming languages.