PYTHON CLASSES AND OBJECTS

Sathvik S Bangera
4 min readAug 19, 2021

--

Classes

A class is part of the Object-Oriented Concept. A class is like a blueprint for creating objects. We use keyword class to define a class. A class name is a valid identifier used with the keyword class while defining a class.

Syntax : class class_name:

x = 10

Here in the above syntax class is a keyword whereas the class_name is any name that can be defined for the class followed with the colon(:), without the colon it results in an error. x = 10 are the attributes and the variables that belong to the particular class. These attributes can be accessed using the dot(.) operator and are always public by default.

Objects

An object is a real-world element with attributes i.e. data and behaviors i.e. functions. It is also known as the instance if the Class. We can have as many objects for a single class, but without the class, it is not possible to create an object.

Declaring an Object

An object can be declared as per the class type. That means an object is equivalent to the variable and class is equivalent to the datatype.

Syntax : object_name = class_name()

Object_name.classfunction_name

Here in the above syntax object_name is the name of the object that we are creating it can be any character or string, the class_name is the name of the class that has been declared at the beginning while creating the class it should be followed with braces() these braces are used to pass the arguments it can be even empty also if no arguments are passed, in the 2nd line we are using the dot(.) operator to access methods or the functions of the class, without the dot(.) it results in the syntax error.

Example :

Example 1

The output of the above code is 18, here Hello is the class_name, x is the object, age is the attribute.

Sample Diagram of Classes & Object

In the above diagram, Animal is the name of the class, Cat 1, Cat 2, Cat 3, Cat 4, Cat 5, Cat 6 is the object names that are inside the Animal Class. Name, Breed, Origin are the Attributes of that Class. Eat, Play, Sleep are the Behaviours of that Class Animal.

The Self method

The self-method is similar to the pointers in C and C ++, and this reference in Java. If we are having any method that does not take any arguments, then also we need to pass self as one argument at starting. The main use of the self is that it binds the attributes with the given arguments and Python does not use the ‘@’ syntax to refer to instance attributes.

The _init_method

Similar to the constructors in C++ and Java the _init_() is the same in Python. It is used to call when an object is created from the class, and access is required to initialize the attributes of the class. The _init_() runs as soon as the object of the class is initiated.

Example :

Example 2 (Self & init method)

OUTPUT

Output of Example 2

In the above example, Animal is the class_name, types is the object_name, breed is the function_name, with the help of the self we are accessing the 6 breed types that have been declared outside the function breed. The arguments ‘CAT’ and ‘ORIGIN’ are passed to the init function, by using the self the name and the origin details have been stored and can be printed anywhere in the function.

Object Properties

We can modify or delete the object properties as well as we can even delete the object itself by using the del keyword.

Modifying Object Properties

Example 3 Modifying Object Properties

OUTPUT

Output of Example 3

In the above code initially, the age is set to 18 and it has been printed in the output, but after giving x.age = 30 the output has been changed to 30 this is because the value 18 is no longer exists and it has been replaced with the new value 30.

Deleting Object Properties

Example 4 Deleting Object Properties

OUTPUT

Output of Example 4

In the above code the output 18 is printed which is right, but at the 2nd the print statement when we are trying to print the same age variable we are getting an error, this is because at the previous statement itself we have used del x.age which results in the deletion of the particular variable, the attribute age is now no longer exists in the memory and hence we get the attribute error.

Deleting the Object

Example 5 Deleting the Object

OUTPUT

Output of Example 5

In the above code, we are getting name error, this is because before the 2nd print statement we have already given the statement del x, where x is an object and del is the keyword for deleting, hence we are deleting the object itself which is no longer available in the memory.

Conclusion

Overall we can conclude by saying that classes and objects are very useful in Python. It makes it easy to write the code and easily modifications can be done. With the help of the classes and object many OOPs concepts can be implemented like Inheritance, Polymorphism, Packages, Abstract, Encapsulation, etc. Class attributes are necessary when each object needs to share one variable. Object attributes have the advantage when each unique object needs its own values, something that makes them different from other objects.

--

--