1 / 8

Closure

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.ttt<br>

Télécharger la présentation

Closure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Closure

  2. A function which returns another function as output. • A closure—unlike a plain function—allows the function to access those captured variables through the closure’s 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. Eg:def f1(): print('This is f1 Function') def f2(): print('This is f2 function') returnf2a = f1()print(a) a()Eg:def f1(): print('This is f1 Function') def f2(): print('This is f2 function') returnf2a = f1()print(a) a() • O/P: This is f1 Function <function f1.<locals>.f2 at 0x7fc192fc66a8> This is f2 function

  4. Decorators in Python • Decorators are function which takes one function as input and returnsa modified function as output, used mainly to modify an existingfunction • 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 @classmethod ,@staticmethod ,@property • Syntax:@abc_decoratordef hello_decorator(): print(“abc")

  5. Eg:def decorator(func):  def wrapper(x,y):  if y == 0: print('Number can not be divided by 0') else: print(func(x,y))return wrapper@decoratordef existing_func(a,b): return a//bexisting_func(10,2)Eg:def decorator(func):  def wrapper(x,y):  if y == 0: print('Number can not be divided by 0') else: print(func(x,y))return wrapper@decoratordef existing_func(a,b): return a//bexisting_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/P:10 20 20 • Eg2:def my_gen():  num=-5 while(num<=3): yield num num+=1 a=my_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

More Related