Lets assume that a class is a template which has data members without values assigned to it. When an object is created out of a class, values need to be passed, so that the data members of that class gets values assigned to it. This is made possible by a reserved method __init__ in python (a.k.a constructor in general).

fork-git

Object creation is not possible without the __init__ method. Hence, in case it is not done, it is automatically implemented by default, by python as in the following.

def __init__(self):
    # Empty default constructor

non-parameterized __init__ method, example:

class Topup_wallet:
    # non-parameterized constructor
    def __init__(self):
        self.topup_amount = 10

    # Topup method
    def topup(self):
        print('{} Points added up to wallet!'.format(self.topup_amount))


wallet = Topup_wallet()
wallet.topup()

Note that, in this example, __init__ method does not accept any arguments while creating an object.

Output:

10 Points added up to wallet!

parameterized __init__ method, example:

class Topup_wallet:
    # non-parameterized constructor
    def __init__(self, my_topup_amount):
        self.topup_amount = my_topup_amount

    # Topup method
    def topup(self):
        print('{} Points added up to wallet!'.format(self.topup_amount))


wallet = Topup_wallet('20')
wallet.topup()

Note that, in this example, __init__ method accepts an argument (my_topup_amount) while creating the object.

Output:

20 Points added up to wallet!

In-built class functions in python

hasattr(obj, attr_name)returns true if attribute exists
getattr(obj, attr_name)returns the value of the attribute
setattr(obj, attr_name, attr_value)sets value to the attribute
delattr(obj, attr_name)deletes attribute

Class functions, Example:

class Topup_wallet:
    # non-parameterized constructor
    def __init__(self, my_topup_amount):
        self.topup_amount = my_topup_amount

    # Topup method
    def topup(self):
        print('{} Points added up to wallet!'.format(self.topup_amount))

wallet = Topup_wallet('20')
wallet.topup()
print('')

print('Check if "topup_amount" attribute exists')
print(hasattr(wallet, "topup_amount"))
print('')

print('display the value assigned to attribute "topup_amount" during object creation')
print(getattr(wallet, "topup_amount"))
print('')

print('Change the value that was assigned  to attribute "topup_amount" during object creation, to 30')
setattr(wallet, "topup_amount", "30")
print('')

print('display the changed value')
print(getattr(wallet, "topup_amount"))
print('')

print('delete attribute "topup_amount"')
delattr(wallet, 'topup_amount')
print('')

print('Check if "topup_amount" attribute exists')
print(hasattr(wallet, "topup_amount"))
print('')

Output:

20 Points added up to wallet!

Check if "topup_amount" attribute exists
True

display the value assigned to attribute "topup_amount" during object creation
20

Change the value that was assigned  to attribute "topup_amount" during object creation, to 30

display the changed value
30

delete attribute "topup_amount"

Check if "topup_amount" attribute exists
False