Classes

Variables, Lists, Dictionaries etc in python is a object. Without getting into the theory part of Object Oriented Programming, explanation of the concepts will be done along this tutorial.

A class is declared as follows

class class_name:

Functions
In [1]:
class FirstClass:
    pass

pass in python means do nothing.

Above, a class object named "FirstClass" is declared now consider a "egclass" which has all the characteristics of "FirstClass". So all you have to do is, equate the "egclass" to "FirstClass". In python jargon this is called as creating an instance. "egclass" is the instance of "FirstClass"

In [2]:
egclass = FirstClass()
In [3]:
type(egclass)
Out[3]:
__main__.FirstClass
In [4]:
type(FirstClass)
Out[4]:
type

Now let us add some "functionality" to the class. So that our "FirstClass" is defined in a better way. A function inside a class is called as a "Method" of that class

Most of the classes will have a function named "__init__". These are called as magic methods. In this method you basically initialize the variables of that class or any other initial algorithms which is applicable to all methods is specified in this method. A variable inside a class is called an attribute.

These helps simplify the process of initializing a instance. For example,

(Without the use of magic method or __init__ which is otherwise called as constructors. One had to define a init( ) method and call the init( ) function.)-> this is for python 2

In [5]:
eg0 = FirstClass()
eg0.__init__()

But when the constructor is defined the __init__ is called thus intializing the instance created.

We will make our "FirstClass" to accept two variables name and symbol.

I will be explaining about the "self" in a while.

In [6]:
class FirstClass:
    def __init__(self,name,symbol):
        self.name = name
        self.symbol = symbol

Now that we have defined a function and added the __init__ method. We can create a instance of FirstClass which now accepts two arguments.

In [7]:
eg1 = FirstClass('one',1)
eg2 = FirstClass('two',2)
In [8]:
print (eg1.name, eg1.symbol)
print (eg2.name, eg2.symbol)
one 1
two 2

dir( ) function comes very handy in looking into what the class contains and what all method it offers

In [9]:
dir(FirstClass)
Out[9]:
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__']

dir( ) of an instance also shows it's defined attributes.

In [10]:
dir(eg1)
Out[10]:
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'name',
 'symbol']

Changing the FirstClass function a bit,

In [11]:
class FirstClass:
    def __init__(self,n,s):
        self.name = n
        self.symbol = s

Changing self.name and self.symbol to self.n and self.s respectively will yield,

In [12]:
eg1 = FirstClass('one',1)
eg2 = FirstClass('two',2)
In [13]:
print (eg1.n, eg1.s)
print (eg2.n, eg2.s)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-13-ff360c7b632b> in <module>()
----> 1 print (eg1.n, eg1.s)
      2 print (eg2.n, eg2.s)

AttributeError: 'FirstClass' object has no attribute 'n'

AttributeError, Remember variables are nothing but attributes inside a class? So this means we have not given the correct attribute for the instance.

In [14]:
dir(eg1)
Out[14]:
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'name',
 'symbol']
In [15]:
print (eg1.name, eg1.symbol)
print (eg2.name, eg2.symbol)
one 1
two 2

So now we have solved the error. Now let us compare the two examples that we saw.

When I declared self.name and self.symbol, there was no attribute error for eg1.name and eg1.symbol and when I declared self.n and self.s, there was no attribute error for eg1.n and eg1.s

From the above we can conclude that self is nothing but the instance itself.

Remember, self is not predefined it is userdefined. You can make use of anything you are comfortable with. But it has become a common practice to use self.

In [16]:
class FirstClass:
    def __init__(asdf1234,name,symbol):
        asdf1234.n = name
        asdf1234.s = symbol
In [17]:
eg1 = FirstClass('one',1)
eg2 = FirstClass('two',2)
In [18]:
print (eg1.n, eg1.s)
print (eg2.n, eg2.s)
one 1
two 2

Since eg1 and eg2 are instances of FirstClass it need not necessarily be limited to FirstClass itself. It might extend itself by declaring other attributes without having the attribute to be declared inside the FirstClass.

In [19]:
eg1.cube = 1
eg2.cube = 8
In [20]:
dir(eg1)
Out[20]:
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'cube',
 'n',
 's']

Just like global and local variables as we saw earlier, even classes have it's own types of variables.

Class Attribute : attributes defined outside the method and is applicable to all the instances.

Instance Attribute : attributes defined inside a method and is applicable to only that method and is unique to each instance.

In [21]:
class FirstClass:
    test = 'test'
    def __init__(self,name,symbol):
        self.name = name
        self.symbol = symbol

Here test is a class attribute and name is a instance attribute.

In [22]:
eg3 = FirstClass('Three',3)
In [23]:
print (eg3.test, eg3.name)
test Three

Let us add some more methods to FirstClass.

In [24]:
class FirstClass:
    def __init__(self,name,symbol):
        self.name = name
        self.symbol = symbol
    def square(self):
        return self.symbol * self.symbol
    def cube(self):
        return self.symbol * self.symbol * self.symbol
    def multiply(self, x):
        return self.symbol * x
In [25]:
eg4 = FirstClass('Five',5)
In [26]:
print (eg4.square())
print (eg4.cube())
25
125
In [27]:
eg4.multiply(2)
Out[27]:
10

The above can also be written as,

In [28]:
FirstClass.multiply(eg4,2)
Out[28]:
10

Inheritance

There might be cases where a new class would have all the previous characteristics of an already defined class. So the new class can "inherit" the previous class and add it's own methods to it. This is called as inheritance.

Consider class SoftwareEngineer which has a method salary.

In [29]:
class SoftwareEngineer:
    def __init__(self,name,age):
        self.name = name
        self.age = age
    def salary(self, value):
        self.money = value
        print (self.name,"earns",self.money)
In [30]:
a = SoftwareEngineer('Ritveak',26)
In [31]:
a.salary(40000)
Ritveak earns 40000
In [32]:
dir(SoftwareEngineer)
Out[32]:
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'salary']

Now consider another class Artist which tells us about the amount of money an artist earns and his artform.

In [33]:
class Artist:
    def __init__(self,name,age):
        self.name = name
        self.age = age
    def money(self,value):
        self.money = value
        print (self.name,"earns",self.money)
    def artform(self, job):
        self.job = job
        print (self.name,"is a", self.job)
In [34]:
b = Artist('Vraj',20)
In [35]:
b.money(50000)
b.artform('Musician')
Vraj earns 50000
Vraj is a Musician
In [36]:
dir(Artist)
Out[36]:
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'artform',
 'money']

money method and salary method are the same. So we can generalize the method to salary and inherit the SoftwareEngineer class to Artist class. Now the artist class becomes,

In [37]:
class Artist(SoftwareEngineer):
    def artform(self, job):
        self.job = job
        print (self.name,"is a", self.job)
In [38]:
c = Artist('Nishanth',21)
In [39]:
dir(Artist)
Out[39]:
['__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 '__weakref__',
 'artform',
 'salary']
In [40]:
c.salary(60000)
c.artform('Dancer')
Nishanth earns 60000
Nishanth is a Dancer

Suppose say while inheriting a particular method is not suitable for the new class. One can override this method by defining again that method with the same name inside the new class.

In [41]:
class Artist(SoftwareEngineer):
    def artform(self, job):
        self.job = job
        print (self.name,"is a", self.job)
    def salary(self, value):
        self.money = value
        print (self.name,"earns",self.money)
        print ("I am overriding the SoftwareEngineer class's salary method")
In [42]:
c = Artist('Nishanth',21)
In [43]:
c.salary(60000)
c.artform('Dancer')
Nishanth earns 60000
I am overriding the SoftwareEngineer class's salary method
Nishanth is a Dancer

If not sure how many times methods will be called it will become difficult to declare so many variables to carry each result hence it is better to declare a list and append the result.

In [44]:
class emptylist:
    def __init__(self):
        self.data = []
    def one(self,x):
        self.data.append(x)
    def two(self, x ):
        self.data.append(x**2)
    def three(self, x):
        self.data.append(x**3)
In [45]:
xc = emptylist()
In [46]:
xc.one(1)
print (xc.data)
[1]

Since xc.data is a list direct list operations can also be performed.

In [47]:
xc.data.append(8)
print (xc.data)
[1, 8]
In [48]:
xc.two(3)
print (xc.data)
[1, 8, 9]

If the number of input arguments varies from instance to instance asterisk can be used as shown.

In [49]:
class NotSure:
    def __init__(self, *args):
        self.data = ''.join(list(args)) 
In [50]:
yz = NotSure('I', 'Do' , 'Not', 'Know', 'What', 'To','Type')
In [51]:
yz.data
Out[51]:
'IDoNotKnowWhatToType'

Where to go from here?

Now that you have been introduced to python, You can try out the different python libraries in the field of your interest. I highly recommend you to check out this curated list of Python frameworks, libraries and software http://awesome-python.com

The official python documentation : https://docs.python.org/3/

signup for https://dbader.org/ get cool python tips and tricks.