473,320 Members | 2,004 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,320 software developers and data experts.

[PEP] auto keyword for automatic objects support in Python

Hi.
This post follows "does python have useless destructors".
I'm not an expert, so I hope what I will write is meaningfull and
clear.
Actually in Python there is no possibility to write code that follows
C++ RAII pattern.
Of course Python objects are not statics like in C++, but in C++ the
auto_ptr class is used for enforcing this pattern for dynamical
allocated objects, by using a 'destructive' pointer semantic.
Now, here is a simple example of Python code:

afile = file('foo.txt', 'w') def use_file(afile): .... afile.write('hello')
use_file(afile) afile.write(' word')
Internally (at least in CPython) when the function is called, a frame
object is created, with a dictionary of all local variables.
When the function terminates the frame is 'deleted' (where 'deleted'
means: 'its reference count is decremented').
The frame destructor then calls local variables destructors (again,
local variables references counter are decremented).

For our example this means that when use_file function exits, this
equivalent Python code is execuded:
del afile

Of course this not deletes afile, since there exists another reference
to it.
What I'm proposing is to add a new keyword 'auto'. Here its use:
afile = file('foo.txt', 'w')
auto afile def use_file(afile): .... afile.write('hello')
use_file(afile) afile.write(' word') # ==> error, file closed. See above
Simply, auto objects behaves like C++ auto_ptr but with important
differences and with the need of some support by the objects.

This equivalent code is now executed when the function exits:

if hasattr(afile, '__deinit__'): afile.__deinit__() # see above
del afile
With the use of auto there is the need(?) to think at object
destruction as a two fase operation (like object creation with __new__
and __init__).

In fact __del__ method is called when the object is being
deallocated/garbage collected (actually this is not 'deterministic').
The RAII pattern requires simply that as an object goes 'out of scope'
it should releases all the 'external resources' it has acquired.
So there is the need of a second special method (call it __deinit__)
that should be called for auto objects when they go 'out of scope'
(in a 'deterministic' way).
As an example, for a file object __deinit__ code is, of course, the
same as in the __del__ code.
More in detail:
as __init__ creates the class invariant, __deinit__ should be
'destroy' it; that is, it should be put the instance in a 'neutral'
state: all external resource released.

The difference between __deinit__ and __del__ is that after __del__ is
called the object is supposed to be deallocated/garbage collected;
after __deinit__ the object is still 'live' in a 'neutral' state.
So I think it is better to have two distinct methods.

Issues:
Q) Should be Frame objects be auto?
R) They should be auto if there are auto local variables in it.

Q) What about compound objects, ad example:
class hold_file:

.... def __init__(self, afile): self.file = afile

R) self.file should be auto if an hold_file instance is auto.


Regards Manlio Perillo
Jul 18 '05 #1
2 2570
On Fri, 18 Jun 2004 16:32:39 GMT, rumours say that Manlio Perillo
<NO******************@libero.it> might have written:

[snip]
With the use of auto there is the need(?) to think at object
destruction as a two fase operation (like object creation with __new__
and __init__).

In fact __del__ method is called when the object is being
deallocated/garbage collected (actually this is not 'deterministic').
The RAII pattern requires simply that as an object goes 'out of scope'
it should releases all the 'external resources' it has acquired.
So there is the need of a second special method (call it __deinit__)
that should be called for auto objects when they go 'out of scope'
(in a 'deterministic' way).


[snip]

I'm afraid your PEP's strongest adversary will be the "Explicit is
better than implicit". You suggest complications to the language
implementation that can be avoided just by user code.

For example, you could have a class Deallocator (untested improvised
code):

class Deallocator:
def __init__(self, *args):
self.args = args
def deallocate(self):
for obj in self.args:
obj.__deinit__()

then in your function start:
auto = Deallocator(obj1, obj2 ...)

and in your function end:
auto.deallocate()

If your function has multiple exit points, wrap its code in a try ...
finally sequence.
These are some of the obvious counter-arguments for your PEP, and
without looking I assume there have been already similar discussions in
the past. Good luck :)
--
TZOTZIOY, I speak England very best,
"I have a cunning plan, m'lord" --Sean Bean as Odysseus/Ulysses
Jul 18 '05 #2
On Tue, 22 Jun 2004 12:16:05 +0300, Christos "TZOTZIOY" Georgiou
<tz**@sil-tec.gr> wrote:
On Fri, 18 Jun 2004 16:32:39 GMT, rumours say that Manlio Perillo
<NO******************@libero.it> might have written:

[snip]
[snip]

I'm afraid your PEP's strongest adversary will be the "Explicit is
better than implicit". You suggest complications to the language
implementation that can be avoided just by user code.

For example, you could have a class Deallocator (untested improvised
code):

class Deallocator:
def __init__(self, *args):
self.args = args
def deallocate(self):
for obj in self.args:
obj.__deinit__()

then in your function start:
auto = Deallocator(obj1, obj2 ...)

and in your function end:
auto.deallocate()

If your function has multiple exit points, wrap its code in a try ...
finally sequence.


The problem is that one have to use finally consistently.
The C++ RAII pattern is more simple.
Thanks and regards Manlio Perillo
Jul 18 '05 #3

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

Similar topics

0
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. PEP: 318 Title: Decorators for Functions and...
49
by: Steven Bethard | last post by:
I promised I'd put together a PEP for a 'generic object' data type for Python 2.5 that allows one to replace __getitem__ style access with dotted-attribute style access (without declaring another...
18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
0
by: Adam DePrince | last post by:
I frequently find myself in a situation where I wish I could say "the execution order of these two lines just doesn't matter." Proposed here are two freshly drafted PEP's advocating for the...
37
by: Steven Bethard | last post by:
The PEP below should be mostly self explanatory. I'll try to keep the most updated versions available at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
18
by: Steven Bethard | last post by:
I've updated the PEP based on a number of comments on comp.lang.python. The most updated versions are still at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
28
by: Steven Bethard | last post by:
Ok, I finally have a PEP number. Here's the most updated version of the "make" statement PEP. I'll be posting it shortly to python-dev. Thanks again for the previous discussion and suggestions!...
7
by: Steven Bethard | last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The patch is available at: http://bugs.python.org/1472459 and I've pasted the full text below. I've tried to be more explicit about...
4
by: dustin | last post by:
I've been hacking away on this PEP for a while, and there has been some related discussion on python-dev that went into the PEP: ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.