Python Classes create | Explained Objects | Exercise

A python is an object-oriented programming language, so it’s also following the same rules of it. Like having a Class, methods(functions), variables etc, same other OOPs languages. Python class is a blueprint for creating an object. This tutorial we will explain about python classes and object with exercise.

Python Classes create Explained Objects Exercise example

Class

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.

 

Object

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?

 

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.

 

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 Python class constructor.

We are passing an argument when the object is creating and print the values.

Output: EyeHunts 99

 

The __init__() Function

Above the example, in python constructor, you see the def _inti_() functionit’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 attribute 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.

Output: Python Tutorial on EyeHunts

 

Delete Objects

Let’s come to the last term, deletes objects in python, there is a lot of more you can check in 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 delete object, it will raise an error NameError: name 'obj1' is not defined.

Output:

Python Classes create Explained delete object ouput error

 

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 in the starting of the class body or top in the class. Here is an example of how to define a class attributes in Python.

Output: 1

2

 

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)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6

Python 3.7

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.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.