Armin Rigo wrote: > Hello Jim, > > On Wed, Jun 30, 2004 at 10:24:26AM -0400, Jim Fulton wrote: > >>Here is a demonstration Python implementation: >> >> from threading import currentThread >> >> class local(object): >> >> ... > > > The instance should clear the entry from all the per-thread dictionaries when > it is deallocated, Ugh. Now you want me to work hard. ;) ... > > Descriptor fun! I think there is a way to write this class so that "regular" > attributes like __class__ still work, and without having to special-case > __dict__. Something along the lines of: > > def __patch(self): > key = "thread-local-%x" % id(self) > d = currentThread().__dict__.setdefault(key, {}) > object.__setattr__(self, '__dict__', d) > > def __getattribute__(self, name): > self.__patch() > return super(local, self).__getattribute__(name) > > def __setattr__(self, name, value): > self.__patch() > super(local, self).__setattr__(name, value) > > I am unclear about how C types should do the equivalent of the 'super' call, > though. But I'm ok with declaring that we don't care and use > PyObject_Generic[GS]etAttr(). Note that this makes it go somewhat slower, since we now have to search the class dictionary as well as the instance dictonary. I don't think it's worth it to avoid special handling for __dict__ and __class__. This approach *does* provide the benefit that you can subclass thead.local to ad default values, methods, and descriptors, which I think could be very useful. I'll investigate this. The easiest way to do this in C will be to have different logic for the base class and for subclasses. For the base class, I'll do what I'm doing now (adding handling for __class__). For subclasses, I'll do what you suggest. This mans that simple usages of the base class don't get the extra performance hit. > > Finally, don't forget __delattr__. Yup. Thanks. Jim -- Jim Fulton mailto:jim at zope.com Python Powered! CTO (540) 361-1714 http://www.python.org Zope Corporation http://www.zope.com http://www.zope.org
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4