472,143 Members | 1,378 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Where does a class closure live?

Consider:

### Function closure example

def outer(s):
.... def inner():
.... print s
.... return inner
....
>>f = outer(5)
f()
5
>>dir(f)
['__call__', '__class__', '__delattr__', '__dict__', '__doc__',
'__get__', '__getattribute__', '__hash__', '__init__', '__module__',
'__name__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__str__', 'func_closure', 'func_code', 'func_defaults',
'func_dict', 'func_doc', 'func_globals', 'func_name']

### Class instance closure example
>>def outer2(s):
.... class Inner(object):
.... def __call__(self):
.... print s
.... return Inner()
....
>>f = outer2(10)
f()
10
>>dir(f)
['__call__', '__class__', '__delattr__', '__dict__', '__doc__',
'__getattribute__', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__',
'__weakref__']

##### Class closure example
>>def outer3(s):
.... class Inner(object):
.... def __call__(self):
.... print s
.... return Inner
....
>>F = outer3(15)
f = F()
f()
15
>>dir(F)
['__call__', '__class__', '__delattr__', '__dict__', '__doc__',
'__getattribute__', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__',
'__weakref__']
Now the closure for the function live in func_name. I've even done the
exercise where I build a dummy inner function that returns its closed
variable, so that I can use that thing to reach through "cells" and
check out the variables living in the closure object. Where are the
closure variables for the class instance, and the class? Can I get my
hands on them in Python?
-Gerard
Dec 6 '06 #1
0 1009

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Csaba Gabor | last post: by
6 posts views Thread by Kent Johnson | last post: by
7 posts views Thread by Csaba Gabor | last post: by
11 posts views Thread by Huayang Xia | last post: by
reply views Thread by leo001 | last post: by

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.