First Class Function

First-Class Objects

First-class objects in python refers to the following characteristics –

  • We can pass it to a function as an argument
  • We can return it from a function
  • You can assign it to a variable
  • We can store it in a data structure such as a list, tuple, dictionary, etc.
  • types such as int, float, string tuple, list, and many more are first-class objects
  • A function is also a first-class object.

Higher-order Function: A function that returns a function is a higher-order function.

Function Docstrings and Annotations

We typically use help(x) to check the documentation x in a python interpreter. There many IDEs that have different shortcut key options to see the documentation of a specific class, and object. In a function, the lines just after the function definition as strings refer to these documentations. Sometimes it is a single line string and sometimes it is multi-line docs isolated by triple quotation(“””). It follows PEP 257 rules.

Here is an example –

>> def myf():
        """Its a doc.
        But it is very short."""
        return None

>> help(myf)
Help on function myf in module __main__:

     Its a doc.
     But it is very short.

>> myf.__doc__
'Its a doc.\n    But it is very short.'

Notice that, myf.__doc__ also returns the whole documentation but as raw strings.

Function annotation follows PEP 3107, and it is additional way to document a function. Function annotations works this way,

def myf(a: <expression>, b: <expression>) -><expression>:

<expression> is here the docs for the corresponding parameter. Annotations do not affect python parameters or values. It is just meta data.

Here is an example of function annotation –

>> def myf(a: "an integer", b: "a list" ) -> 'a merged list':
        return b

>> help(myf)
Help on function myf in module __main__: 

myf(a: 'an integer', b: 'a list') -> 'a merged list'

>> myf.__annotations__
{'a': 'an integer', 'b': 'a list', 'return': 'a merged list'}

Lambda Expression

Lambda expressions are simply another way to create functions, we can call them anonymous functions. A lambda expression starts with the lambda key followed by a parameter list and then the expression. The expression acts like a function body.

Lambda Function
Lambda Expression

We can use lambda function in different ways such as –

  • We can assign lambda expression to a variable. But the variable is then a function and it is callable. For example,
>> myf  = lambda x:x**2

>> myf(3)
  • We can pass a lambda function in another function.
>> def myf(x,fn):
      return fn(x)

>> myf(3, lambda x: x+5 )

Limitations of lambda expression –

  • You can not use assignments like this,
    lambda x: x = 5 or lambda x: x = x + 5
  • You can not use annotations here, such as
    lambda x: int : x*2
  • It is a single line one expression, however, line-continuation is OK. For example.
    lambda x : x * \

Leave a Reply

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