A python is an object-oriented programming language, so it’s also following the same rules as it. Like having a Class, methods(functions), variables, etc, same other OOPs languages. Python class is a blueprint for creating an object. In this tutorial, we will explain python classes and object with exercise.
A class is an available procedure and definition a like a blueprint of a house or vehicle. It describes everything, contains all the information like member methods (function) and variables etc, but it is just a blueprint.
The object is an instance of classes or we can say they have now behavior, like an orange is an object, where orange have a property color orange, share round and it’s own smell.
Create a Python classes example
For defining or declare a class in python you have to use a keyword class, then after giving a name of it. Same as example creating MyClass name python class.
Let’s see an example how Python class create?
# Class name - MyClass class MyClass: # Variable number = 10
Create a Python classes object
So you know how to create a class in python now have to learn to create a class object in python of it.
Here is an example, how to create a python class object. You need just define the object and assign with the class name. We are also accessing a variable of the class and print() the value in the console.
Here is obj1 called Python object.
class MyClass: number = 10 # Create class object obj1 = MyClass() # access variable of class print(obj1.number)
Python class constructor
A Constructor has a use to initiate variables in programming languages.
Python class constructor will be the first piece of code to be executed when you create a new object of a class. Let’s see the example of the Python class constructor.
We are passing an argument when the object is creating and print the values.
class MyClass: # defining constructor def __init__(self, n, a): self.name = n self.age = a print(self.name, self.age) # Create class object with arguments obj1 = MyClass('EyeHunts', 99)
Output: EyeHunts 99
The __init__() Function
Above the example, in python constructor, you see the
def _inti_() function, it’s called python classes init.
All classes have__init__() function is a built-in function and always executed when the class is being initiated. It was used to assign the value of variables or attributes of class from an object or class instance (when creating).
The self Parameter
Again see the above example of python constructor, Where
self parameter is a reference to the class itself and is used to access variables that belong to the class.
It has to be the first parameter of any function in the class and you can name as you want, it’s not fixed with only python class self.
class MyClass: # using self def __init__(self, name, age): self.name = name self.age = age # Using abc parameter for reference def myfunc(abc): print("Python Tutorial on " + abc.name) obj1 = MyClass("EyeHunts", 99) obj1.myfunc()
Output: Python Tutorial on EyeHunts
Let’s come to the last term, deletes objects in python, there is a lot more you can check on the python official site, which is added in the last.
You can delete objects by using the
del keyword in python. If you try to access the delete object, it will raise an error
NameError: name 'obj1' is not defined.
class MyClass: number = 10 # Create class object obj1 = MyClass() # Delete object del obj1 # will generate error because object deleted print(obj1.number)
QA: What are Python classes attributes?
Python class attributes belong to the class itself they will be shared by all the instances. These attributes are defined at the starting of the class body or top in the class. Here is an example of how to define class attributes in Python.
class myClass: count = 0 # class attribute def addOne(self): myClass.count += 1 # Calling addOne() on an object obj1 = myClass() obj1.addOne() print(obj1.count) # Calling addOne() on one more obj2 = myClass() obj2.addOne() print(obj2.count)
Reference: https://docs.python.org/3/tutorial/classes.html (Visit Official website page to learn about all Classes)
Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
All Examples of Python classes created and object Exercise made in Python 3, so it may change its different from python 2 or upgraded versions.