Search
Code Directory
 ASP
 ASP.NET
 C/C++
 CFML
 CGI/PERL
 Delphi
 Development
 Flash
 HTML
 Java
 JavaScript
 Pascal
 PHP
 Python
 SQL
 Tools
 Visual Basic & VB.NET
 XML
New Code
Spire.PDF for Java 2.7.8
Database Workbench Pro 5.6.10.0
The C# PDF Library 2020.1.5
CAD-Viewer 3DVBVIEW V4
dotConnect for SQLite 5.15
LuaStudio 9.92
EntityDAC 2.3
Devart ODBC Driver for QuickBooks 2.0
phpEnter 5.1.
Udemy Clone script 2.01
VisualNEO Web 19.11.7
VisualNEO for Windows 19.4.29.0
SentiVeillance SDK Trial 7.3
Devart ODBC Driver for PostgreSQL 3.2
SecureBridge 9.2
Top Code
SecureBridge 8.0
dotConnect for SQLite 5.9
EntityDAC 1.6
Database Workbench Pro 5.6.10.0
ATN Site Builder 3.0
ATN Resume Finder 2.0
PHP Review Script 1.0
Devart ODBC Driver for PostgreSQL 3.2
ADO.NET Provider for ExactTarget 1.0
Deals and Discounts Website Script 1.0.2
ICPennyBid Penny Auction Script 4.0
Invoice Manager by PHPJabbers 3.0
The C# PDF Library 1.0
Aglowsoft SQL Query Tools 8.2
Classified Ad Lister 1.0
Top Rated
phpEnter 5.1.
Single Leg MLM 1.2.1
Azizi search engine script PHP 4.1.10
Paste phpSoftPro 1.4.1
Extreme Injector 3.7
Deals and Discounts Website Script 1.0.2
ADO.NET Provider for ExactTarget 1.0
Solid File System OS edition 5.1
Classified Ad Lister 1.0
Aglowsoft SQL Query Tools 8.2
Invoice Manager by PHPJabbers 3.0
ICPennyBid Penny Auction Script 4.0
PHP Review Script 1.0
ATN Resume Finder 2.0
ATN Site Builder 3.0
object finalization without __del__ and without hassles 
File ID: 65524






object finalization without __del__ and without hassles 
Download object finalization without __del__ and without hassles http://code.activestate.com/recipes/519621-object-finalization-without-__del__-and-without-ha/?in=lang-pythonReport Error Link
License: Freeware
Downloads: 1
Submit Rating:
object finalization without __del__ and without hassles  Description
Description: The well-known way to avoid __del__ (and its problems when the object to be deleted is involved in a reference cycle) is to use a finalization callback on a weakref.

This recipe simply makes it easier to write such "finalizable" objects by allowing to write the finalizer as a __finalize__ method (a bit similar to a __del__ method, but not exactly as we'll see).

The first step is to call the bind_finalizer(*attrs) method on your instance; you must give bind_finalizer a list of attribute names necessary for the finalizer to operate, and it will construct a "ghost object" holding those attributes (for example, if you have a "socket" attribute you want to close, you will include "socket" in the arguments to bind_finalizer, which in turn will bind a "socket" attribute on the ghost object). The ghost object has the same class as the original self, so you will be able to call instance methods and use class variables in the finalizer as well.

Please note: the ghost object is created and its attributes are bound as soon as bind_finalizer() is called, so their values must have been properly initialized. A practical idiom is probably to call bind_finalizer() at the end of your constructor. Creating the ghost ignores any __new__ or __init__ method you have defined, so you don't have to worry about constructor signature or unwanted side effects.

Your __finalize__ method must be written as a normal method, except that it can only access those instance attributes whose names you gave to bind_finalizer. This is because __finalize__ gets a "ghost instance" instead of the original instance.

Finally, there is a remove_finalizer() which does the obvious. Call it when the resources have been somehow freed manually and automatic finalization is not useful anymore.

With this recipe, you have an object-oriented finalization scheme which still works as required when your instance is part of a reference cycle to be broken by the Garbage Collector. Also, it doesn't use metaclasses which makes it easier to re-use if you have your own metaclasses.

You can find hereafter the code for the Finalizeable class, as well as an example TransactionBase class which helps writing objects with transaction-like behaviour and optional automatic rollback on object destruction.
(but the interesting stuff is really in Finalizable)

License: Freeware

Related: Oop

O/S:Windows

Downloads: 1



More Similar Code

an object which extends the xrange object to support slicing and indexing (simple)



Automates the creation of SQL INSERT statements for the "simple" attributes in a python object by creating a string of an object's attribute names and a corresponding string of that object's attribute values. Simple attributes are those...



ApexSQL Diff implements a state of the Art solution to the most complex database comparison scenarios including many unique features like Diff codes, RegEx object filters, Project Wizards and much more

Compare your SQL Databases in...



Easy, flexible, extensible, Java object persistence without the need for XML binding files or other configuration.

Contains a semantic model that facilitates going from Object to any representation (XML, JSON, Database, etc) and...



A methode to traverse a tree (or the rest of a tree starting from a node with unkown position) depth-first without recursion and without mandatorily keeping track of the position of the current node; requires each node to have reference acess to...



diapergluforth script helps you to access functions in shared object libraries without recompile them against the library header.



Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation [1]. This design pattern is also known as Cursor.

Motivation: An example of an aggregate object is an instance of...



Object package for representation and manipulation of block-circulant matrices.



Com++ is an object oriented network programming and system programming library. The framework facilitates client server programming in the TCP/IP-, UDP, Unix Domain offers Multithreading-, Semaphore -, Mutex ... Written in C++ and verified on Linux



This COM object can create, edit and join wireless bitmaps. Use it with ASP, Visual Basic or other COM tools. Convert to and from standard bitmaps. Draw shapes, lines, pixels and text to dynamically produce graphics for WAP enabled web pages....

User Review for object finalization without __del__ and without hassles
- required fields
     

Please enter text on the image