Python Programs

Python - Closures

Closures

Example

def multiple_of(x):
    def multiple(y):
        return x*y
    return multiple
c1 = multiple_of(5)  # 'c1' is a closure
c2 = multiple_of(6)  # 'c2' is a closure
print(c1(4))
print(c2(4))

Output

20
24

Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest

No comments:

Post a Comment

Home
Subscribe to: Posts (Atom)

Main

  • Sample Programs

Pages

  • Python - Home
  • Python - Database Connectivity
  • Python - Higher Order Functions
  • Python - Files and Regular Expressions
  • Python - Closures
  • Python - Decorators
  • Python - Descriptors
  • Python - Class and Static Methods
  • Python - Abstract Class
  • Python - Context Manager
  • Python - Coroutines
  • Python - doctest
  • Python - unittest
  • Python - nose
  • Python - Pytest
  • Python - NumPy
  • Python - NumPy Arrays
  • Python - NumPy Array Shaping
  • Python - Iterating NumPy Arrays
  • Python - Slicing NumPy Arrays
  • Python - Array Operations
  • Python - List Comparison
3509
Simple theme. Powered by Blogger.