View on GitHub

python-for-kids

Learning and exercise content on Python for Kids

Day 18 - Concept of the Class

A “class” in general to us means a group of students who study the same thing in a school. A class in scient we mean to say a group of things which are similar in nature or some attributes matches. Same thing for programming language as well. A class in programming language helps us put things together which in general being used together. In Python data types are class. You see that str represents string class and some methods we have explored earlier. Those methods are to be used with str. Hence they stay together. This helps development team to write and maintain the code base and helps focus on one perticular type. Programming language is not a static thing. I mean to say that once you write a piece of code you tend to come back again and again and change it time to time.

Let’s see how we can write class in Python.

# Define the class
class MyClass:
    a = 10
    b = 20

#Calling the class
classObject = MyClass()
print(classObject.a) # This will print 10
print(classObject.b) # This will print 20

Now this is a simple class which is no different from module which we have learned here.

What if I want to parameterize the values I am passing and store them in a and b. I can achieve it in couple of different ways.

# Define the class
class MyClass2:
    a = 0
    b = 0

    def initiate(self, x,y):
        self.a = x
        self.b = y

        print(self.a)
        print(self.b)

# Calling class MyClass2
classObj2 = MyClass2()
classObj2.initiate(22,33) # This will print 22 and 33

I am not happy with the above approach. It looks complicated to me. In programming the approach should be simple. For that programming language provides some smart way of doing stuff. One such method is __init__ (underscore underscore init) method as we generally call it. Everytime you declare a variable and assign its value from a class the __init__ method is called. It does not matter if you have written it explicitly or not. If you have written it then you will have more control on what happens when __init__ is called. Let’s explore how init works.

# Define the class
class MyClass3:
    def __init__(self, x,y):
        self.a = x
        self.b = y

    def printall(self):
        print(self.a)
        print(self.b)

# Calling class MyClass3
classObj3 = MyClass3(62, 70)
classObj3.printall() # This will print 62 and 70

This time you are passing the values as part of class initialization. You don’t need to call another method or use a class member as we did before.

Note: Here in the function we need to pass the first mandatory parameter named self, both for __init__ and printall which is what we have created.

self parameter represents the class object and using it you can access all the variables of that class instance.

Built in metods in a class

There are quite a few methods you have in your class. __init__ is one such. To find more you can use one built-in method call it like below,

# Define a blank class
class MyClass4:
    pass

# calling dir() function
print(dir(MyClass4))

The output would look like,

['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

Above are the default methods everytime you declare a class like

class MyClass:
    pass

Notice here we can use pass to simply do nothing. Somtimes these kind of blank fires are required. You will learn about the usability once you encounter it. Nothing in a programming language is unuseful. They are created becasuse a large set of people required it.


Next: Day 19 - Putting it all together

Back to Index