Understanding First Class Function in python





Function in python are First- Class Objects. This means that they can be passed around and used just like any other data types. In Shorthand it's also known as "First-class function"
What is First Class Object Objects ?
  • Created at runtime
  • assigned to a variable or element in a data structure
  • passed as an arguments to a function
  • returned as the result of a function
Integers, Strings and dictionaries are other examples of first- class objects in Python.
Treating a function like an Object
Created at runtime
>>> def factorial(n): 
...  """return n!"""
...  return 1 if n < 2 else n * factorial(n - 1)
... 
>>> factorial(5)
120
>>> factorial.__doc__
'return n!'
>>> type(factorial)
<class 'function'>
>>>

In The above example we created a function at the runtime__doc__ is one of several attribute of the function objects, and factorial is an instance of function class

assigned to a variable or element in a data structure
>>> fact = factorial
>>> fact
<function factorial at 0x7fdfb7f7fbf8>
>>> fact(4)
24
>>>

We can assign it a variable fact and call it through that name

passed as an arguments to a function
>>> map(factorial, range(5))
<map object at 0x7fdfb7e93390>
>>>

We can also pass factorial as an argument to function In the above example we passed it to the map and italso returns a function as result, Which produces a below result when passed to list
returned as the result of a function
>>> list(map(factorial, range(5)))
[1, 1, 2, 6, 24]
>>>

Another example is the sorted built-in function: an optional key arguments lets you provide a function to be applied to each item for sorting.

>>> name = ['Ankur', 'Anand', 'Sam', 'John', 'KK', 'I']
>>> sorted(name, key=len)
['I', 'KK', 'Sam', 'John', 'Ankur', 'Anand']

Custom Reverse Function used for Sorting.

>>> def reverse_sort(word):
...  ''' reverse the word '''
...  return word[::-1]
... 
>>> reverse_sort('Ankur')
'ruknA'
>>> sorted(name, key=reverse_sort)
['I', 'KK', 'Anand', 'Sam', 'John', 'Ankur']
>>>

Share To :

No comments

No comments :

Post a Comment