473,385 Members | 1,357 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,385 software developers and data experts.

suggestion: adding weakattr to stdlib

three-liner:
reposted from python-dev for more feedback. it suggests to add
the weakattr class to the standard weakref.py module.
comments are welcome.

[ http://article.gmane.org/gmane.comp.python.devel/81875 ]

From: tomer filiba <tomerfiliba <atgmail.com>
Subject: weakattr
Newsgroups: gmane.comp.python.devel
Date: 2006-07-01 13:49:46 GMT (2 days, 3 hours and 12 minutes ago)

weakattr (weak attributes) are attributes that are weakly referenced
by their containing object. they are very useful for cyclic references
--
an object that holds a reference to itself.

when a cyclic reference is found by the GC, the memory may be
freed, but __del__ is not called, because it's impossible to tell which
__del__ to call first. this is an awkward asymmetry with no clean
solution: most such objects provide a "close" or "dispose" method
that must be called explicitly.

weakattrs to solve this problem, by providing a "magical" attribute
that "disappears" when the attribute is no longer strongly-referenced.

you can find the code, as well as some examples, on this link
http://sebulba.wikispaces.com/recipe+weakattr

since the stdlib already comes with weakref.py, which provides
higher level concepts over the builtin _weakref module, i'd like to
make weakattr a part of it.

it's only ~20 lines of code, and imho saves the trouble of explicitly
releasing the resource of un__del__able objects.

i think it's useful. here's a snippet:
>>from weakref import weakattr

class blah(object):
.... yada = weakref()
....
>>o1 = blah()
o2 = blah()
o1.yada = o2
o2.yada = o1
o1.yada is a *weakref* to o2, so that when o2 is no longer
strongly-referenced...
>>del o2
o1.yada "magically" disappears as well.
>>o1.yada
.... AttributeError(...)

since the programmer explicitly defined "yada" as a weakatt, he/she
knows it might "disappear". it might look awkward at first, but that's
exactly the *desired* behavior (otherwise we'd just use the regular
strong attributes).

another thing to note is that weakattrs are likely to be gone only
when the object's __del__ is already invoked, so the only code that
needs to take such precautions is __del__ (which already has some
constraints)

for example:
>>class blah(object):
.... me = weakattr()
....
.... def __init__(self):
.... self.me = self
....
.... def something(self):
.... # we can rest assure me exists at this stage
.... print self.me
....
.... def __del__(self):
.... # by the time __del__ is called, "me" is removed
.... print "me exists?", hasattr(self, "me")
....
>>b = blah()
b.me
<__main__.blah object at 0x00C0EC10>
>>b.something()
<__main__.blah object at 0x00C0EC10>
>>del b
import gc
gc.collect()
me exists? False
0
>>>


-tomer

Jul 3 '06 #1
0 1029

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

Similar topics

4
by: exits funnel | last post by:
Hello, I'm trying to append the ascii representation of an integer to an stl string and I'm having some trouble. I've got the following bits of code: #include <string> #include <cstdlib> ...
2
by: PRadyut | last post by:
In this code i tried to add the elements in ascending order but the output is only 0 1 2 the rest of the elements are not shown. the code...
15
by: Jon Skeet | last post by:
I've been briefly musing on what is probably a pretty silly idea, but one which would no doubt benefit from being discussed and thoroughly shot down in flames rather than being allowed to fester in...
68
by: Gsec | last post by:
Hi, Can anybody let me know how to write a infinite loop, such that the program never crashes ? I, guess, buffer overflow method mite help!Not sure. Plz let me know. -thanks and regards,
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.