In the previous tutorial we covered how to work with python methods, how you can make your code look good and prevent writing the same piece of code, again and again, Today we will cover python classes.

It was a two-line code of how you can create a class. but the first thins we need to understand is what do we mean by class. Let's take a simple example lets say we have to make a person class.

class person:

Here We have a class named person, from this class I can create an unlimited instance of a person with different attributes like name, age, gender etc. 

Let's try to create a person and give it a name

class person:
albert = person() #here we are creating
# an instance of person Albert is just a variable
# it will print something like this
<__main__.person object at 0x03AC12D0> = 'Albert' # assigned name to the person
print( # will print Albert

So here in the example, we created a person instance and then assigned a name for it, I would suggest you play with it by assigning age, gender, country etc. 

Now diving deeper, how we can assign some methods to a person, methods are things a class can do for example we can make a method for sleeping, for eating etc. Let's make the sleeping method for Albert.

class person:
def sleeping(self):
print('I am sleeping dont disturb')
albert = person()
albert.sleeping()#will print I am sleeping dont disturb

you can also do some more advanced stuff, and can also pass arguments in a method like this.

class person:
def change_age(self, new_age):
self.age = new_age
albert = person()
albert. age = 20  #lets asign some age
print(albert. age)  #20
#now we can change it in this way
albert. age #25

So you can put any method you want,  it's not only about changing some attributes.

But here we have another question still unanswered till now, how it helps us.

let's say we have to make two person, with different names we can do this

class person:
albert = person() = 'albert'
nikola = person() = 'nikola'
print( ) #albert
print( #nikola

So in this way you can create unlimited persons. but you may want to assign a name while creating a person instead of doing it after assigning, here the __init__ comes it. its like initiate something.

class person:
def __init__(self, name): = name
def change_name(self, new_name): = new_name
albert = person('albert')
print( #albert
print( #Nikola

The __init__ method will run itself when you create an instance of the class, and you will have to pass the arguments in the class when you create the class. 

A class can have attributes and methods, in the person class name, age, gender, country etc can be counted as attributes, and the things a person can do are called methods.  For example, if you have a code for a wallet if may have attributes like account number,  balance, currency etc and we can have methods like add_money, send_money, etc. let's try to make it.

class account:
def __init__(self, account_number, account_holder):
self.account_number =account_number
self.account_holder = account_holder
self.balance = 0 #lets initiate with zero balance
def add_balance(self, balance_to_add):
self.balance = self.balance + balance_to_add

ac = account(001, 'Albert')
print(ac.balance ) # 0
print(ac.balance ) # 100


Continue Reading