Sitemap
Vipul Kaitke

Vipul Kaitke

Developer

Class/Object in Python

3 min readFeb 26, 2024

--

Python is an object oriented programming language.

Almost everything in Python is an object, with its properties and methods.

A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state. Class instances can also have methods (defined by their class) for modifying their state.

To understand the need for creating a class and object in Python let’s consider an example, let’s say you wanted to track the number of dogs that may have different attributes like breed and age. If a list is used, the first element could be the dog’s breed while the second element could represent its age. Let’s suppose there are 100 different dogs, then how would you know which element is supposed to be which? What if you wanted to add other properties to these dogs? This lacks organization and it’s the exact need for classes.

Syntax: Class Definition:

class ClassName:
# Statement

Syntax: Object Definition:

obj = ClassName()
print(obj.atrr)

The class creates a user-defined data structure, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object.

Some points on Python class:

  • Classes are created by keyword class.
  • Attributes are the variables that belong to a class.
  • Attributes are always public and can be accessed using the dot (.) operator. Eg.: My class.Myattribute
class Cars:
color = "Blue"

Object of Python Class

An Object is an instance of a Class. A class is like a blueprint while an instance is a copy of the class with actual values. It’s not an idea anymore, it’s an actual dog, like a dog of breed pug who’s seven years old. You can have many dogs to create many different instances, but without the class as a guide, you would be lost, not knowing what information is required.

Get Vipul Kaitke’s stories in your inbox

Join Medium for free to get updates from this writer.

An object consists of:

  • State: It is represented by the attributes of an object. It also reflects the properties of an object.
  • Behavior: It is represented by the methods of an object. It also reflects the response of an object to other objects.
  • Identity: It gives a unique name to an object and enables one object to interact with other objects.

Declaring Claas Objects (Also called instantiating a class)

When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and the behavior of the class. But the values of those attributes, i.e. the state are unique for each object. A single class may have any number of instances.

class GFG:
def __init__(somename, name, company):
somename.name = name
somename.company = company

def show(somename):
print("Hello my name is " + somename.name +
" and I work in "+somename.company+".")


obj = GFG("John", "vevcodelab")
obj.show()
class GFG:
def __init__(self, name, company):
self.name = name
self.company = company

def show(self):
print("Hello my name is " + self.name+" and I" +
" work in "+self.company+".")


obj = GFG("John", "vevcodelab")
obj.show()

Output: Output for both of the codes will be the same.

Hello my name is John and I work in vevcodelab.

Explanation:

In this example, we are creating a GFG class and we have created the name, and company instance variables in the constructor. We have created a method named say_hi() which returns the string “Hello my name is ” + {name} +” and I work in “+{company}+”.”.We have created a person class object and we passing the name John and Company vevcodelab to the instance variable. Finally, we are calling the show() of the class.

Pass Statement

The program’s execution is unaffected by the pass statement’s inaction. It merely permits the program to skip past that section of the code without doing anything. It is frequently employed when the syntactic constraints of Python demand a valid statement but no useful code must be executed.

Function:

#VEVCodelab

Happy Coding!!!

--

--