Prelude

It's undoubtly a tough task and a final point in Python functions scrutiny. But persistence and attention to details should eliminate most of headache.

Simple function

def primitive_function(arg_1, arg_2):
    """Multiply arguments and return result"""
    return arg_1 * arg_2

assert(primitive_function(2, 2) == 4)

Nested function

But what if we put the arbitrary function inside?

def parent_function(arg_1, arg_2):
    """Evaluates all nested functions"""

    def first_nested_function(*args):
        """Converts positional argument"""
        # args[0] += 1
        print(args)

    def second_nested_function(**kwargs):
        """Converts named argument"""
        kwargs['b'] += 1

    first_nested_function()
    #second_nested_function()
    #return args[0] + kwargs['b']
    pass

# assert(parent_function(4, b=7) == 13)

Syncactic shugar

Primitive example

def decorator(any_function_as_an_argument):
    """Do nothing, just returning an input function"""
    return any_function_as_an_argument


@decorator
def sample_function(a, b):
    return a + b

assert(sample_function(2, 3) == 5)
None



blog comments powered by Disqus

Published

19 May 2017

Category

literate programming

Tags