473,569 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1084

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

Similar topics

6
1540
by: Csaba Gabor | last post by:
I'd like to be able to pass a key1/value1 pair into a function and have that function have a local variable by the name of key1 to which value1 is assigned. for example, I'd like to call function sample(oOptions) { var key1 = "key1 default"; var key2 = "key2 default"; var idx; if (arguments.length>0)
6
1423
by: Kent Johnson | last post by:
Is there a way to persist a class definition (not a class instance, the actual class) so it can be restored later? A naive approach using pickle doesn't work: >>> import pickle >>> class Foo(object): ... def show(self): ... print "I'm a Foo" ... >>> p = pickle.dumps(Foo) >>> p 'c__main__\nFoo\np0\n.'
7
1854
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 shows the alert. What is the essential reason? function () { alert('hi mom'); }(); function () { alert('hi dad'); }(8); var x=function () {...
9
1854
by: sashang | last post by:
Hi I'd like to use metaclasses to dynamically generate a class based on a parameter to the objects init function. For example: class MetaThing(type): def __init__(cls, name, bases, dict, extra_information): super(MetaThing, cls).__init__(name, bases, dict)
11
3012
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()
162
10113
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you prefix them with 2 underscores, but I hate prefixing my vars, I'd rather add a keyword before it. Python advertises himself as a full OOP language,...
10
1783
by: Ugo | last post by:
Hi guys, until now I make so: <div id="my_div"><!-- content --></div> MyClass.prototype.doSomething = function( ) {}; MyClass.prototype.assignFun = function( )
3
5475
by: Johnson | last post by:
I just converted an ASP.NET Web Site to be an ASP.NET Web Application project type in Visual Studio 2005. After the conversion, the whole thing still works - it builds and the Web app runs just fine. However, none of the classes are placed explicitly in any namespace. The ASP.NET Web Site project type apparentlyl infers namespace from the...
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7678
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7982
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5222
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
944
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.