473,721 Members | 4,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[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('he llo')
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('he llo')
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 2604
On Fri, 18 Jun 2004 16:32:39 GMT, rumours say that Manlio Perillo
<NO************ ******@libero.i t> 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(obj 1, 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
implementati on 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(obj 1, 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
2349
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 Methods Version: $Revision: 1.34 $ Last-Modified: $Date: 2004/09/03 09:32:50 $ Author: Kevin D. Smith, Jim Jewett, Skip Montanaro, Anthony Baxter
49
2884
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 class). Any comments would be appreciated! Thanks! Steve ----------------------------------------------------------------------
18
3046
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 a single collections type, I should be proposing a new "namespaces" module instead. Some of my reasons: (1) Namespace is feeling less and less like a collection to me. Even though it's still intended as a data-only structure, the use cases...
0
1114
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 addition of nondeterminism as a spring board for future automatic parallelism. The first calls for core language changes, the second calls for a minor (and likely symbolic) change to list comprehension's semantics to better support future...
37
3304
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 http://ucsu.colorado.edu/~bethard/py/pep_create_statement.html PEP: XXX Title: The create statement
18
2717
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 http://ucsu.colorado.edu/~bethard/py/pep_create_statement.html In this post, I'm especially soliciting review of Carl Banks's point (now discussed under Open Issues) which asks if it would be better to have the create statement translated into:
28
2941
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! PEP: 359 Title: The "make" Statement Version: $Revision: 45366 $ Last-Modified: $Date: 2006-04-13 07:36:24 -0600 (Thu, 13 Apr 2006) $
7
2699
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 the goals -- the make statement is mostly syntactic sugar for:: class <name> <tuple>: __metaclass__ = <callable>
4
1668
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: http://mail.python.org/pipermail/python-dev/2007-February/070921.html http://mail.python.org/pipermail/python-dev/2007-February/071155.html http://mail.python.org/pipermail/python-dev/2007-February/071181.html I'd love to have feedback on this PEP: - from a user's perspective (would you want to write...
0
8851
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9227
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9143
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9077
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6676
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4761
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2588
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2137
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.