473,322 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Is this a closure?

A method on a class:

def printSelf(self):
def printReviews():
for review in self.reviews:
review.printSelf()
print "Idnbr: ", self.idnumber, "Reviews: ", printReviews()

I don't have to pass an argument to printReviews because everything
defined inside printSelf is aware of outer variables? Or is that
wrong? If it is right, is this what a closure means?

Because Python is lexically scoped right? Is lexical scope+closures =
organized dynamic scope kind of if you get my point?

Aug 31 '08 #1
2 930
Yes, printReviews() is a closure. In particular, it's closing over the
variable "self", which it's getting lexically from printSelf().
- Chris

On Sun, Aug 31, 2008 at 4:53 PM, ssecorp <ci**********@gmail.comwrote:
A method on a class:

def printSelf(self):
def printReviews():
for review in self.reviews:
review.printSelf()
print "Idnbr: ", self.idnumber, "Reviews: ", printReviews()

I don't have to pass an argument to printReviews because everything
defined inside printSelf is aware of outer variables? Or is that
wrong? If it is right, is this what a closure means?

Because Python is lexically scoped right? Is lexical scope+closures =
organized dynamic scope kind of if you get my point?

--
http://mail.python.org/mailman/listinfo/python-list

--
Follow the path of the Iguana...
http://rebertia.com
Sep 1 '08 #2
On Sep 1, 9:53 am, ssecorp <circularf...@gmail.comwrote:
A method on a class:

def printSelf(self):
def printReviews():
for review in self.reviews:
review.printSelf()
print "Idnbr: ", self.idnumber, "Reviews: ", printReviews()
The above appears to be more or less identical in effect to:
def printSelf(self):
print "Idnbr: ", self.idnumber, "Reviews: "
for review in self.reviews:
review.printSelf()
except for spacing and more importantly the second version won't print
the gratuitous None value returned by printReviews().

What are you aiming for? If your purpose is to explore/understand
lexical scopes, I suggest that you get it right in your head in the
context of a simple non-recursive function, then /if necessary/ try to
do it in a recursive class method.

HTH,
John


Sep 1 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dave Benjamin | last post by:
Here are some more ideas for how to implement a statement-friendly code block syntax in Python. Hopefully more "Pythonic" (that is, of or pertaining to those features noticably reminiscent of...
27
by: Ted Lilley | last post by:
What I want to do is pre-load functions with arguments by iterating through a list like so: >>>class myclass: .... pass >>>def func(self, arg): .... print arg >>>mylist = >>>for item...
1
by: Victor Ng | last post by:
Is there a way to preserve the argspec of a function after wrapping it in a closure? I'm looking for a general way to say "wrap function F in a closure", such that inspect.getargspec on the...
9
by: Mikito Harakiri | last post by:
Transitive closure (TC) of a graph is with TransClosedEdges (tail, head) as ( select tail, head from Edges union all select e.tail, ee.head from Edges e, TransClosedEdges ee where e.head =...
7
by: Csaba Gabor | last post by:
I feel like it's the twilight zone here as several seemingly trivial questions are bugging me. The first of the following three lines is a syntax error, while the last one is the only one that...
9
by: User1014 | last post by:
I'm a javascript noob and have a question concerning some code I've come across that I'm trying to understand. Here is a snippet of the relevant section: (snip) var closure = this; var xhr =...
0
by: Gerard Brunick | last post by:
Consider: ### Function closure example def outer(s): .... def inner(): .... print s .... return inner .... 5
11
by: Huayang Xia | last post by:
What will the following piece of code print? (10 or 15) def testClosure(maxIndex) : def closureTest(): return maxIndex maxIndex += 5 return closureTest()
4
by: LAN MIND | last post by:
?
4
by: JavascriptProgrammer | last post by:
In the following code: ----------------------- function get() { return function() { alert(x); } }; function foo(s) { var x = s; this.getX = get();
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.