GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Python closures - using closure functions in Python Python closureslast modified January 29, 2024 Python closures tutorial shows how to use closure functions in Python. Python functions are first-class citizens. This means that functions have equal status with other objects in Python. Functions can be assigned to variables, stored in collections, created and deleted dynamically, or passed as arguments. A nested function, also called an inner function, is a function defined inside another function. nested_fun.py
#!/usr/bin/python
def main():
def build_message(name):
msg = f'Hello {name}'
return msg
name = input("Enter your name: ")
msg = build_message(name)
print(msg)
if __name__ == "__main__":
main()
The Python closuresA closure is a nested function which has access to a free variable from an enclosing function that has finished its execution. Three characteristics of a Python closure are:
A free variable is a variable that is not bound in the local scope.
In order for closures to work with immutable variables such as numbers and
strings, we have to use the Python closures help avoiding the usage of global values and provide some form of data hiding. They are used in Python decorators. Python simple closure exampleThe following is a simple example of a Python closure. simple_closure.py
#!/usr/bin/python
def make_printer(msg):
msg = "hi there"
def printer():
print(msg)
return printer
myprinter = make_printer("Hello there")
myprinter()
myprinter()
myprinter()
In the example, we have a myprinter = make_printer("Hello there")
The $ ./simple_closure.py hi there hi there hi there Python closure with nonlocal keyword
The counter.py
#!/usr/bin/python
def make_counter():
count = 0
def inner():
nonlocal count
count += 1
return count
return inner
counter = make_counter()
c = counter()
print(c)
c = counter()
print(c)
c = counter()
print(c)
The example creates a counter function. def make_counter():
count = 0
def inner():
nonlocal count
count += 1
return count
return inner
By using the $ ./counter.py 1 2 3 Python closures vs classesPython closures can be an alternate solution to small classes. summer.py
#!/usr/bin/python
class Summer():
def __init__(self):
self.data = []
def __call__(self, val):
self.data.append(val)
_sum = sum(self.data)
return _sum
summer = Summer()
s = summer(1)
print(s)
s = summer(2)
print(s)
s = summer(3)
print(s)
s = summer(4)
print(s)
We have a def __init__(self):
self.data = []
The data is kept in the object attribute and is created in the constructor. def __call__(self, val):
self.data.append(val)
_sum = sum(self.data)
return _sum
Each time the instance is called, the value is appended and the sum is calculated and returned. The following is an alternate solution with Python closure. summer2.py
#!/usr/bin/python
def make_summer():
data = []
def summer(val):
data.append(val)
_sum = sum(data)
return _sum
return summer
summer = make_summer()
s = summer(1)
print(s)
s = summer(2)
print(s)
s = summer(3)
print(s)
s = summer(4)
print(s)
We have the same functionality with a Python closure. def make_summer():
data = []
def summer(val):
data.append(val)
_sum = sum(data)
return _sum
return summer
Because the SourceIn this article we have worked with Python closures. AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Python tutorials. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |