closure - PowerPoint PPT Presentation

About This Presentation
Title:

closure

Description:

There are some of the features in python that can be used in functions.Decorators,closures as well as generators can be used modify,create or return a function.These concepts are discussed along with examples of each. – PowerPoint PPT presentation

Number of Views:98

less

Transcript and Presenter's Notes

Title: closure


1
Closure
2
  • A function which returns another function as
    output.
  • A closureunlike a plain functionallows the
    function to
  • access those captured variables through the
    closures copies of
  • their values or references, even when the
    function is invoked
  • outside their scope.
  • As closures are used as callback functions, they
    provide some
  • sort of data hiding. This helps us to reduce the
    use of
  • global variables.

3
  • Egdef f1() print('This is f1 Function') def f
    2() print('This is f2 function') return f2a 
     f1()print(a) a()
  • O/P This is f1 Function ltfunction
    f1.ltlocalsgt.f2 at 0x7fc192fc66a8gt This is f2
    function

4
Decorators in Python
  • Decorators are function which takes one function
    as input and returns a modified function as output
    , used mainly to modify an existing function
  • Decorators are very powerful and useful tool in
    Python since it allows programmers to modify the
    behaviour of function or class. Decorators allow
    us to wrap another function in order to extend
    the behaviour of wrapped function, without
    permanently modifying it.
  • In Decorators, functions are taken as the
    argument into another function and then called
    inside the wrapper function.
  • There are some decorators already defined in
    python such as _at_classmethod ,_at_staticmethod
    ,_at_property
  • Syntax _at_abc_decoratordef hello_decorator()
    print(abc")

5
  • Egdef decorator(func)  def wrapper(x,y)  if
     y  0 print('Number can not be divided by 0'
    ) else print(func(x,y))return wrapper_at_deco
    ratordef existing_func(a,b) return a//bexistin
    g_func(10,2)
  • O/P 5

6
Generators in Python
  • Generator-Function  A generator-function is
    defined like a normal function, but whenever it
    needs to generate a value, it does so with
    the yield keyword rather than return. If the body
    of a def contains yield, the function
    automatically becomes a generator function.
  • Generator-Object  Generator functions return a
    generator object. Generator objects are used
    either by calling the next method on the
    generator object or using the generator object in
    a for in loop (as shown in the above program).

7
  • Eg def my_gen()  yield 10   yield 20
    yield 20
  •  for i in my_gen()  print(i)
  • O/P10 20 20
  • Eg2def my_gen()  num-5
  • while(numlt3) yield num num1
  • amy_gen()
  • for i in a
  •    print(i)
  • O/P-2 -1 0 1 2 3

8
Thanks for Watching!!!
  • For more follow us on our social media platforms
  • Instagram learnbay_datascience
  • Facebook learnbay
  • LinkedIn Learnbay
  • Twitter Learnbay1
Write a Comment
User Comments (0)
About PowerShow.com