473,408 Members | 2,477 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,408 software developers and data experts.

Closure/binding problem or misunderstanding

If I run the following code:

class path(object):
def __init__(self, **subdirs):
for name, path in subdirs.iteritems():
def getpath():
return path
setattr(self, name, getpath)

export = path(
one = 'this is one',
two = 'this is two',
)

print "invoking", export.one, export.one()
print "invoking", export.two, export.two()

I get this output:

invoking <function getpath at 0x400ded14this is one
invoking <function getpath at 0x400ded84this is one

So there apparently are two definitions of the function "getpath" (the
addresses are different, anyway), but they seem to have the same value
for the binding of "path". It's not clear to me, after reading what I
can find about python name binding, whether this is the expected
behavior, or not (although I was surprised).

Can anyone enlighten me?

Thanks,
Bob Sidebotham

Nov 9 '07 #1
2 1204
On Nov 9, 2:32 pm, "Bob.Sidebot...@gmail.com"
<Bob.Sidebot...@gmail.comwrote:
If I run the following code:

class path(object):
def __init__(self, **subdirs):
for name, path in subdirs.iteritems():
def getpath():
return path
setattr(self, name, getpath)

export = path(
one = 'this is one',
two = 'this is two',
)

print "invoking", export.one, export.one()
print "invoking", export.two, export.two()

I get this output:

invoking <function getpath at 0x400ded14this is one
invoking <function getpath at 0x400ded84this is one

So there apparently are two definitions of the function "getpath" (the
addresses are different, anyway), but they seem to have the same value
for the binding of "path". It's not clear to me, after reading what I
can find about python name binding, whether this is the expected
behavior, or not (although I was surprised).
Clearly it wasn't the expected behaviour, or you wouldn't be
surprised :)

It's behaving as defined though, and the usual work-around is to add a
variable with a default value.

class path(object):
def __init__(self, **subdirs):
for name, path in subdirs.iteritems():
def getpath(path=path):
return path
setattr(self, name, getpath)

--
Paul Hankin

Nov 9 '07 #2
On Nov 9, 9:49 am, Paul Hankin <paul.han...@gmail.comwrote:
It's behaving as defined though, and the usual work-around is to add a
variable with a default value.

class path(object):
def __init__(self, **subdirs):
for name, path in subdirs.iteritems():
def getpath(path=path):
return path
setattr(self, name, getpath)
Thanks, Paul. That's helpful. I will re-read the reference manual, and
see if I can find out where this behavior is defined. It looks like
it's binding both locals and globals, but not actually taking a
snapshot in time, as would, say Perl (I think). From an efficiency POV
this makes great sense, and I can see that you get everything you need
by essentially creating the closure yourself (by putting everything in
the local space for the function).

Bob

Nov 9 '07 #3

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

Similar topics

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 =...
4
by: Chris | last post by:
I have an ASCX (I think thats called a user control), with a public property of "TextStatic". I want to put this ASCX into a datagrid's template item and bind a data sources to the TextStatic...
19
by: Larry Lard | last post by:
In the old days (VB3 era), there was a thing called the Data Control, and you could use it to databind controls on forms to datasources, and so (as the marketing speak goes), 'create database...
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()
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.