[PEP] auto keyword for automatic objects support in Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Manlio Perillo

    [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:

    [color=blue][color=green][color=darkred]
    >>> afile = file('foo.txt', 'w')[/color][/color][/color]
    [color=blue][color=green][color=darkred]
    >>> def use_file(afile) :[/color][/color][/color]
    .... afile.write('he llo')
    [color=blue][color=green][color=darkred]
    >>> use_file(afile)[/color][/color][/color]
    [color=blue][color=green][color=darkred]
    >>> afile.write(' word')[/color][/color][/color]



    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:
    [color=blue][color=green][color=darkred]
    >>> del afile[/color][/color][/color]


    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:
    [color=blue][color=green][color=darkred]
    >>> afile = file('foo.txt', 'w')
    >>> auto afile[/color][/color][/color]
    [color=blue][color=green][color=darkred]
    >>> def use_file(afile) :[/color][/color][/color]
    .... afile.write('he llo')
    [color=blue][color=green][color=darkred]
    >>> use_file(afile)[/color][/color][/color]
    [color=blue][color=green][color=darkred]
    >>> afile.write(' word') # ==> error, file closed. See above[/color][/color][/color]



    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:

    [color=blue][color=green][color=darkred]
    >>> if hasattr(afile, '__deinit__'): afile.__deinit_ _() # see above
    >>> del afile[/color][/color][/color]



    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:
    [color=blue][color=green][color=darkred]
    >>> class hold_file:[/color][/color][/color]
    .... def __init__(self, afile): self.file = afile

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




    Regards Manlio Perillo
  • Christos TZOTZIOY Georgiou

    #2
    Re: [PEP] auto keyword for automatic objects support in Python

    On Fri, 18 Jun 2004 16:32:39 GMT, rumours say that Manlio Perillo
    <NOmanlio_peril loSPAM@libero.i t> might have written:

    [snip]
    [color=blue]
    >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).[/color]

    [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

    Comment

    • Manlio Perillo

      #3
      Re: [PEP] auto keyword for automatic objects support in Python

      On Tue, 22 Jun 2004 12:16:05 +0300, Christos "TZOTZIOY" Georgiou
      <tzot@sil-tec.gr> wrote:
      [color=blue]
      >On Fri, 18 Jun 2004 16:32:39 GMT, rumours say that Manlio Perillo
      ><NOmanlio_peri lloSPAM@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.
      >
      >[/color]

      The problem is that one have to use finally consistently.
      The C++ RAII pattern is more simple.


      Thanks and regards Manlio Perillo

      Comment

      Working...