Day #57 - Classes and Objects in Python

Day #57 - Classes and Objects in Python

ยท

3 min read

Introduction

Welcome to my 57th blog post on the Python journey. On day 57, I learned about classes and objects to be created in Object Oriented Programming. I also learned about the 'self' parameter that is used while creating a class method. Let's dive into more details and understand the Classes and Objects in Python.

So let's get started......

Classes and Objects

  • A class is a blueprint or a template for creating class objects.

  • It provides initial value for state (member variables or attributes), and implementations of behavior (member functions or methods).

  • The user-defined objects are created using the class keyword.

Creating a Class -

class Details:
    name = "Chintan Jain"
    age = 30

Creating an Object to class -

The object is an instance of the class. It is used to access the properties of the class i.e variables and methods (functions) of the class.

obj1 = Details()

Now we can print values:

class Details:
    name = "Chintan Jain"
    age = 30

obj1 = Details()
print(obj1.name)
print(obj1.age)

Output

Chintan Jain
30

'self' Parameter

The 'self' parameter is a reference to the current instance of the class, and is used to access variables that belong to the class.

An important note to keep in mind, it must be provided as an extra parameter inside the method definition.

Example -

class Details:
    name = "Divya"
    age = 20

    def desc(self):
        print("My name is", self.name, "and I'm", self.age, "years old.")

obj1 = Details()
obj1.desc()

Output

My name is Divya and I'm 20 years old.

Resources Used

You can watch the video of Day#57 by clicking on the below link ๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡

Conclusion

Thanks, guys for going through this blog post. Concluding on day 57, I learned about the Classes and Objects in Python. I also learned about how we can use the 'self' parameter to refer to the current instance of the class.

Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #57

See you in the next one.....


About Me

Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.

I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.

So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.


ย