Class Instance specific methods (self) in Python

()

In the last post, we discussed the basics of creating and using classes and objects in Python programming language. At this point, we go a little further and discuss the use of class-specific methods or function methods inside a class with self.

The code is almost the same as in the last post, but the only change is that, in this, we have included a class-specific method (function with self). This is our focus on learning and discussion. If you’re new to this series then please check this last post to start from the basics: Python classes and objects are made easy with examples

So let’s get started!

Why do we need to use methods inside a class?

When the function is used within a class it’s called class methods. The use of functions is to automate repetitive tasks within code.

The use of ‘self’ inside a class method (function)?

The use of ‘self‘ inside a function serves as a reference point or placeholder to establish a connection with specific class instances. This is necessary because functions require such reference points to perform actions when the function method is called.

Flexibility in Keyword Choice

By default, ‘self’ is used for this purpose. It’s not mandatory to use only the ‘self’ keyword; you can also use any other word. However, the use of that word also aims to create a connection with class-specific instances, attributes, and values.

Calling Class Methods

When calling a class method, you simply need to pass the class instance as an argument to the function to perform an action.

Now let’s see this in action.

The following is our class code.

class Youtuber:
    name = ""
    channel_name = ""
    subscribers = 0

    def get_channel_info(self):
        print(f"Name: {self.name}")
        print(f"Channel_name: {self.channel_name}")
        print(f"Total Subscribers: {self.subscribers}")
     
youtuber1 = Youtuber()
youtuber2 = Youtuber()

youtuber1.name = "Vijay Sharma"
youtuber1.channel_name = "Klient Solutech"
youtuber1.subscribers = 10000

youtuber2.name = "Emily"
youtuber2.channel_name = "TechTool"
youtuber2.subscribers = 20000

total_subscirbers = youtuber1.subscribers+youtuber2.subscribers
#print(f"{youtuber1.name}, and {youtuber2.name} are both friends and they both have total subsribers: {total_subscirbers}")
youtuber2.get_channel_info()

In the provided code, there is a class named Youtuber that defines attributes and a method. We already discussed the other parts of the code in the last post. But in this let us focus on the get_channel_info function methods within the class:

def get_channel_info(self):
    print(f"Name: {self.name}")
    print(f"Channel_name: {self.channel_name}")
    print(f"Subscribers: {self.subscribers}")

This function is a method defined within the Youtuber class. It takes one parameter, self, which is a reference to the instance of the class that calls the method. Inside this method, it prints out the values of three attributes: name, channel_name, and subscribers.

The get_channel_info function is a way to display data about a specific Youtuber instance, and it makes use of the instance’s attributes to do so.

  • Here’s how this function works when called:
  • self.name refers to the name attribute of the specific Youtuber instance that calls the function.
  • self.channel_name refers to the channel_name attribute of the specific Youtuber instance that calls the function.
  • self.subscribers refer to the subscriber’s attribute of the specific Youtuber instance that calls the function.

In object-oriented programming, the self parameter in a class method creates a relationship between the method and the specific instance of the class that calls it. But it needs to be used as a first parameter inside a function. It allows you to access and manipulate the attributes and behavior of that specific instance.
When you call a method on an instance, like youtuber2.get_channel_info(), the self parameter inside the method refers to that particular instance (youtuber2 in this case).
It enables you to access the instance’s attributes and perform actions based on the data associated with that instance. This is how you can work with individual instances of a class, each having its own set of attributes and data, in a way that encapsulates and organizes related functionality.

Watch the video tutorial below – the explanation is in Hindi. But even if you’re not familiar with Hindi, you can still understand the use of class instance-specific methods and self by watching this video tutorial.

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Spread the love

Leave a Comment