User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 397,803 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,502 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:
Views: 4460 | Replies: 3 | Solved
Reply
Join Date: Aug 2005
Posts: 1,020
Reputation: Ene Uran is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 64
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Veteran Poster

Closure in Python

  #1  
Aug 11th, 2006
What is a closure? I keep reading this word in programming, but never got a good explanation.
drink her pretty
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,414
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 9
Solved Threads: 173
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Re: Closure in Python

  #2  
Aug 12th, 2006
A closure is a block of code that can be passed as an argument to a function call. In languages like Lisp and Ruby closures are easily implemented. In Python you can do it, but it is a bit more code. Lambda is often used for that.

Here would be an example ...
[php]class Employee:
"""sets up an employee structure/record"""
pass

# some employee data
olaf = Employee()
olaf.name = "olaf"
olaf.salary = 55

nathan = Employee()
nathan.name = "nathan"
nathan.salary = 175

def pay(amount):
return lambda emp: emp.salary > amount

# set the pay_scale, any amount above 150 is high
pay_scale = pay(150)

print pay_scale # <function <lambda> at 0x009D70F0>

# now set the employee instance
# feeds the emp data to lambda code returned by function pay()
# this constitutes the block of code of the closure
is_high = pay_scale(nathan)

print "Is nathan paid high? ", is_high # Is nathan high paid? True

# set another employee instance
is_high = pay_scale(olaf)

print "Is olaf paid high? ", is_high # Is olaf high paid? False
[/php]
Just found a somewhat simpler example ...
[php]# define a function within a function and return it to the calling scope
# the inner function can access variables in the calling function, i.e. its closure

def times(n):
def _f(x):
return x * n
return _f

t3 = times(3)
print t3 # <function _f at 0x009D70F0>
print t3(7) # 21
[/php]
Here the inner function _f() is the block of code of the closure, similar to the lambda code in the first example.
Last edited by vegaseat : Aug 12th, 2006 at 12:42 pm.
May 'the Google' be with you!
Reply With Quote  
Join Date: May 2006
Location: Birmingham, AL
Posts: 33
Reputation: azimuth0 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 5
azimuth0 azimuth0 is offline Offline
Light Poster

Re: Closure in Python

  #3  
Aug 22nd, 2006
Originally Posted by Ene Uran View Post
What is a closure? I keep reading this word in programming, but never got a good explanation.

People always come up with the generator case, and I think that's the worst way of all to show off closures. Where they really come in handy is introducing lexical scoping.

Lexical scoping just means that the environment is defined by the context in the code, not in runtime. An example in Common Lisp (because I don't really know Python and my predecessor explained it using PHP):

(defun add-to-list (list number)
 (map 'list
   (lamba (x)
     (+ x number))
   list)))

This is a function called add-to-list that takes a list and adds a number to each of its elements. defun defines a function, map applies a
function to each element of a list, and lambda defines an anonymous function.

Now consider the anonymous function - it references both X, a parameter to the function, and NUMBER, which is a parameter to add-to-list. It must have access to NUMBER regardless of the context in which it's called. So it's placed into a closure, where all the variables in its environment are available. In that way, MAP doesn't have to worry about providing access to variables.

This is the primary advantage closures have over other function-variable patterns, like function pointers or functors.
Anonymous functions declared in-place "just work."
Reply With Quote  
Join Date: Aug 2005
Posts: 1,020
Reputation: Ene Uran is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 64
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Veteran Poster

Re: Closure in Python

  #4  
Aug 23rd, 2006
Thank you vegaseat and azimuth0. I have to play with that for a while to comprehent it. By the way, vegaseat's code is Python put into a PHP code field to give it some color highlighting (use a [php] tags instead of [code]).
drink her pretty
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Python Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 6:12 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC