First Class Function & DocString & Lambda Function

First Class Function & DocString & Lambda Function By Sagar Jaybhay

All functions in python are first class functions.

Characteristics of first-class functions :

  1. It can be passed as an argument like int string. Means we can pass the whole function as an argument to second functions as an argument.
  2. It can be written as return value like int, string mean s we can write function a function from that called function.
  3. It is assignable to variable or it can assign to a variable
  4. We can store in a data structure like tuple, sets, dictionary.

So the function is an object and it’s first-class objects.

Higher Order Function: This is those function which takes a function as an argument and returns function.



It returns this document if available. But if we want to achieve this to our function we can achieve this using docstring. In below code, we write a string as the first line after function start this will represent as a comment in function. And when you use help over our function it will give this string which we write on the first line of the function is documentation about that function.

def doc_function_check():
    "This is help document"  # this does not comment but a string
output of above code is 
    This is help document

We see functions are a first class object so whatever we write as documentation string which is stored somewhere. It is stored in a __doc__ property of an object. So by using function Name.__doc__ you can get documentation string.

def doc_function_check():
    "This is help document"  # this is not comment but a string

print(doc_function_check.__doc__)  # This is help document

Function with annotations:

def demo(a: str, b: int) -> str:

print(demo.__doc__)  # output - none
print(help(demo))  # output demo(a: str, b: int) -> str

annotations are not stored in a __doc__ property of an object but it is stored in __annotations__ property of a function/object.

__annotations__ is the dictionary and in this dictionary parameter names are keys. Values are the annotations. And for return value it automatically get return keyword as key and type of return expression is value.

# output - {'a': <class 'str'>, 'b': <class 'int'>, 'return': <class 'str'>}

Lambda Expression:

This is a function or we can say simple inline function creation way. Also called as anonymous functions


Expression this can be assigned to a variable or passed as an argument to another function. In this, we are not given a name to function so that’s why it is called an anonymous function. Lambdas are anonymous functions and they are not closure. Also, you can not assign value in lambda function and it always contains one expression only.

lista = ['B', 'D', 'E', 'a', 'c', 'e']

print(sorted(lista))  # output- ['B', 'D', 'E', 'a', 'c', 'e']


in python, for iterable item, storing sorted function is given but this is case sensitive to achieve correct
result we need to implement lambda function in sorted

# output - ['a', 'B', 'c', 'D', 'E', 'e']
print(sorted(lista, key=lambda x: x.upper()))

Functions attributes:

  1. __name__ : contains the name of function
  2. __defaults__ : tuples which contains positional parameters
  3. __kwdefaults__:  dictionary containing keyword-only parameters
  4. __code__: it return instance of code object
def add(a, b):
    return a + b
print(add.__code__)  # output- <code object add at 0x000002557C46C660, file "d:\Notes daily\Machine learning\Python\Python Code\allFunctions\", line 14>

You may also like...

1 Response

  1. June 5, 2019

    […] Method or Function in Python – Sagar […]

Leave a Reply

Your email address will not be published. Required fields are marked *