May 30, 2019

Python __init__ vs __call__

######################################
# __init__ vs __call__
# x = Foo(1, 2, 3) # __init__
# x = Foo()
# x(1, 2, 3) # __call__
######################################


class Foo:
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c
        print('In Foo __init__ {}, {}, {}'.format(self.a, self.b, self.c))


class Bar:
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

    def __call__(self):
        print('In Bar __call__ {}, {}, {}' .format(self.a, self.b, self.c))


if __name__ == '__main__':
    print('--------------------------------------------------')
    f = Foo(100, 200, 300)
    print(f.a)
    #f()    #'Foo' object is not callable
    print('--------------------------------------------------')
    b = Bar(10, 20, 30)
    print(b.a)
    b()
    print('--------------------------------------------------')


"""
Output:

--------------------------------------------------
In Foo __init__ 100, 200, 300
100
--------------------------------------------------
10
In Bar __call__ 10, 20, 30
--------------------------------------------------
"""

Python Decorators

#########################
# A decorator is a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure.
# Decorators are usually called before the definition of a function you want to decorate.
#########################


# Decorated function
def escape_unicode(f):
    def wrap(*args, **kwargs):
        text = f(*args, **kwargs)
        return ascii(text)
    return wrap


def display_text(text):
    return ascii(text)


@escape_unicode
def display_text_ascii(text):
    return text


if __name__ == '__main__':
    # print('మీరు ఎలా ఉన్నారు?')
    # print(ascii('మీరు ఎలా ఉన్నారు?'))
    print(display_text('మీరు ఎలా ఉన్నారు?'))
    print(display_text_ascii('మీరు ఎలా ఉన్నారు?'))


"""
Output:

'\u0c2e\u0c40\u0c30\u0c41 \u0c0e\u0c32\u0c3e \u0c09\u0c28\u0c4d\u0c28\u0c3e\u0c30\u0c41?'
'\u0c2e\u0c40\u0c30\u0c41 \u0c0e\u0c32\u0c3e \u0c09\u0c28\u0c4d\u0c28\u0c3e\u0c30\u0c41?'
"""