I'm glad to hear it isn't a matter of whether it was useful or not. The way I implemented this feature in Slick-C is with indirection. In Python terms, this means that a separate data structure that isn't reference counted holds the method/function object data. The method/function object is changed to just contain a pointer to it. The data structure which holds all method/function data should probably be a non-reference counted dictionary. When a function is deleted, it's name remains in the dictionary but the entry needs to be changed to indicate that it is "null/invalid". When a deleted function is called, an exception should be raised. Adding a function/method means replacing the data in the dictionary. This type of implementation is simple. There's an insignificant amount of overhead on a function/method call (i.e. instead of "func->data" you have "func=*pfunc;if ( func->isInvalid() ) throw exception; else func->data" ). Technically this algorithm leaks memory since deleted functions/methods are never removed. My response is who cares. When the interpreter cleanup everything function is called, you simple deallocate everything in the hash table. Does anyone know what level of effort would be needed for something like this? Is my proposed implementation a good one for Python? ____________________________________________________________________________________ Yahoo! oneSearch: Finally, mobile search that gives answers, not web links. http://mobile.yahoo.com/mobileweb/onesearch?refer=1ONXIC
On 9/29/07, Joseph Maurer <clarkksv@yahoo.com> wrote: I'm glad to hear it isn't a matter of whether it was useful or not.
The way I implemented this feature in Slick-C is with indirection. In Python terms, this means that a separate data structure that isn't reference counted holds the method/function object data. The method/function object is changed to just contain a pointer to it. The data structure which holds all method/function data should probably be a non-reference counted dictionary. When a function is deleted, it's name remains in the dictionary but the entry needs to be changed to indicate that it is "null/invalid". When a deleted function is called, an exception should be raised. Adding a function/method means replacing the data in the dictionary. This type of implementation is simple. There's an insignificant amount of overhead on a function/method call (i.e. instead of "func->data" you have "func=*pfunc;if ( func->isInvalid() ) throw exception; else func->data" ).
Technically this algorithm leaks memory since deleted functions/methods are never removed. My response is who cares. When the interpreter cleanup everything function is called, you simple deallocate everything in the hash table.
Does anyone know what level of effort would be needed for something like this? Is my proposed implementation a good one for Python?
You may want to take a look at a relevant Cookbook recipe: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/160164 George
Joseph Maurer wrote:
The way I implemented this feature in Slick-C is with indirection...
Is my proposed implementation a good one for Python?
It's nowhere near detailed enough to be able to tell. When Steven said "figure out how to implement it", he meant working out the details, not just coming up with a high-level idea. What you suggest sounds like it ought to be possible, at first sight, since Python function objects are already containers with a reference to another object that holds the function's code. The problem will be figuring out *when* you're redefining a function, because the process of loading a module is a very dynamic one in Python. Defining functions and classes is done by executing code, not by statically analysing declarations as a C compiler does. -- Greg
participants (3)
-
George Sakkis
-
Greg Ewing
-
Joseph Maurer