How to Access Variables That Belongs to Class in Python


In this example, we will learn how to access variables that belongs to the class using the self parameter in Python.

Source Code

class Sum:
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def my_function(self):
        s = self.a + self.b
        print(s)

sum1 = Sum(2, 6)
sum1.my_function()

Output:

8
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Advertisement