Categories:Viewed: 3 - Published at: a few seconds ago

class to show as how to make the class as callable

class CallShow:
    """
    This is the space to do documentation related to class.
    """
    def __init__(self, msg = 'Demo class to show how to make class object as callable'):
        self.msg = msg

    def __call__(self):
        """
        to make object as callable the class should have __call__ in it
        """
        return f"Code to be writen here above to act as per accling object of call"

Function to store the data of IPL match in Namedtuple

def store_ipl_date(tuple1):
    from collections import namedtuple
    IplData = namedtuple('IplData', 'match toss choice session1 session2 winner')
    return IplData(*tuple1)

Function to show namedtuple is instance of tuple

def show_ins_tup():
    from collections import namedtuple
    IplData = namedtuple('IplData', 'match toss choice session1 session2 winner')
    match1 = IplData('RCBvsKKR', 'KKR', 'bat', '229/9', '85/8', 'KKR')
    return isinstance(match1, tuple)

return dot product of two vectors

def dot_product(a: "Vector1", b: "Vector2"):
    return sum( e[0]*e[1] for e in zip(a,b) )

Function to showcast documemtation of namedtuple

def show_doc_named():
    from collections import namedtuple
    IplData = namedtuple('IplData', 'match toss choice session1 session2 winner')
    IplData.__doc__ = 'Namedtuple class to store the IPL match data'
    IplData.match.__doc__ = 'Team name'
    IplData.toss.__doc__ = 'Who won the toss'
    IplData.choice.__doc__ = 'Decision taken by wiinng team toss'
    IplData.session1.__doc__ = 'Run scored by Team1'
    IplData.session2.__doc__ = 'Run scored by Team2'
    IplData.winner.__doc__ = 'Winning Team'
    return help(IplData)

show all local values while one function is running

def show_local():
    import math
    a = 10
    b = 'Hello There'
    print(locals())

class to show implementation of static method

class Mathematics:
    """
    This is the space to do documentation related to class.
    """
    def __init__(self, msg="Demo class of Mathematics"):
        self.msg = msg

    def __str__(self):
        return f' String representation of an object'

    def __repr__(self):
        return f' repr representation of an object with parameter {self.msg}'

    @staticmethod
    def addition(a: "Variable1", b: 'Variable2'):
        """
        @staticmethod makes the mtethod of class as static method.
        It is always recommended to metion it via decorator.
        """
        return a+b