RE: Modifying func_closure

Discussion in 'Python' started by Robert Brewer, Jul 10, 2004.

  1. Talking about classes instead of closures, Terry Reedy wrote:
    > This is the Python mechanism for getting the effect of
    > multiple functions accessing shared persistent but rebindable
    > variables ('shared state' as Guido puts it). Guido added
    > read-only closures primarily, I believe, as a substitute
    > for the default argument hack. Notice that default args are
    > also read-only, and that the change required no syntax change,
    > merely either a legalization of previously illegal
    > (NameError-raising) code or a sematic change in having
    > intermediate scope vars mask global or builtin vars.
    > He had so far rejected sevaral proposals for adding syntax
    > to make closures mutable, noting that classes already do
    > mutable shared state (and perhaps also that one can instead
    > use a mutable enclosed object).


    Off-list, Jacek gave some reasons for preferring closures over classes.
    Jacek, can I post them so we can discuss them further? Perhaps we could
    revisit mutable closures.


    Robert Brewer
    MIS
    Amor Ministries
    Robert Brewer, Jul 10, 2004
    #1
    1. Advertising

  2. Robert Brewer

    Ville Vainio Guest

    >>>>> "Robert" == Robert Brewer <> writes:

    Robert> Talking about classes instead of closures, Terry Reedy wrote:

    [ref. to Guido]

    >> He had so far rejected sevaral proposals for adding syntax to
    >> make closures mutable, noting that classes already do mutable
    >> shared state (and perhaps also that one can instead use a
    >> mutable enclosed object).


    Robert> Off-list, Jacek gave some reasons for preferring closures
    Robert> over classes. Jacek, can I post them so we can discuss
    Robert> them further? Perhaps we could revisit mutable closures.

    Closures are indeed much handier than classes for many "in-place",
    lightweight operations. Implementing a local class is often too
    heavyweight, esp. if the creator has to implement __init__ as well as
    another method that does useful work. Closures are well in line with
    the "modern" way of Pythoning, up there with generators (that also
    preserve state in local variables).

    So yes, please, can we have our mutable closures now? The current way
    of using them (having to "box" the mutatee in a one-element array) has
    a hackish feel.

    --
    Ville Vainio http://tinyurl.com/2prnb
    Ville Vainio, Jul 10, 2004
    #2
    1. Advertising

  3. Re: Modifying func closure

    "Robert Brewer" <> wrote in message news:<>...
    >
    > Off-list, Jacek gave some reasons for preferring closures over classes.
    > Jacek, can I post them so we can discuss them further? Perhaps we could
    > revisit mutable closures.
    >


    I am sure Jacek's reasons are pretty good ones. He already wrote a lot
    in
    previous threads on c.l.p. One of the things that disturb me is that
    faking functions with callable objects sucks. You need to write
    something like

    class function(object):
    def __init__(self,func):
    self.__func__=func
    def __call__(self,*args,**kw):
    return self.__func__(*args,**kw)
    def __get__(self,obj,cls=None):
    return self.__func__.__get__(obj,cls)

    to have the callable objects respect the descriptor protocol as if
    they were functions. This is too verbose for my taste. So I prefer
    to return real function by using a closure instead of a class (but
    then read-write closures becomes important). Alternatively, I would
    need a
    way to subclass from FunctionType. More or this thread:

    http://groups.google.it/groups?hl=&rnum=1


    Michele Simionato
    Michele Simionato, Jul 11, 2004
    #3
  4. Ville Vainio <> writes:

    > Closures are indeed much handier than classes for many "in-place",
    > lightweight operations. Implementing a local class is often too
    > heavyweight, esp. if the creator has to implement __init__ as well as
    > another method that does useful work.


    .... which is pretty much guaranteed ... if you didn't have to
    implement __init__ then it wouldn't be a closure (unless I'm missing
    some subtle variation on the theme).

    > Closures are well in line with the "modern" way of Pythoning,


    Boy am I glad to hear you say that. I sometimes get the impression
    that I'm the only heretic bold enough to suggest publically that
    Classes are all you need in Python :)

    > So yes, please, can we have our mutable closures now?


    Wow, if I hear any more of these comments, I might just have to start
    thinking about considering to entertain the possibility of maybe
    writing a PEP. (Do you note any hesitation or reluctance on my part ?
    :)
    Jacek Generowicz, Jul 12, 2004
    #4
  5. Robert Brewer

    Ville Vainio Guest

    >>>>> "Jacek" == Jacek Generowicz <> writes:

    Jacek> Ville Vainio <> writes:

    >> Closures are indeed much handier than classes for many
    >> "in-place", lightweight operations. Implementing a local class
    >> is often too heavyweight, esp. if the creator has to implement
    >> __init__ as well as another method that does useful work.


    Jacek> ... which is pretty much guaranteed ... if you didn't have
    Jacek> to implement __init__ then it wouldn't be a closure (unless
    Jacek> I'm missing some subtle variation on the theme).

    Sorry, I meant if have_to_implement (__init__ AND other
    method). Sometimes just __init__ will do, if the state is manipulated
    directly via attributes.

    Jacek> Wow, if I hear any more of these comments, I might just
    Jacek> have to start thinking about considering to entertain the
    Jacek> possibility of maybe writing a PEP. (Do you note any
    Jacek> hesitation or reluctance on my part ? :)

    PEP won't hurt. At least it would be a document about why the feature
    was rejected, should such a thing happen ;-).

    --
    Ville Vainio http://tinyurl.com/2prnb
    Ville Vainio, Jul 12, 2004
    #5
  6. Ville Vainio <> writes:

    > PEP won't hurt.


    .... if you have the time to invest in writing a well-researched and
    thoughtful one.

    Unfortunately, I do not.
    Jacek Generowicz, Jul 13, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shawn Zabel

    Perl & XS -- Modifying a char*

    Shawn Zabel, Jul 19, 2004, in forum: Perl
    Replies:
    0
    Views:
    515
    Shawn Zabel
    Jul 19, 2004
  2. Jacek Generowicz

    Modifying func_closure

    Jacek Generowicz, Jul 8, 2004, in forum: Python
    Replies:
    0
    Views:
    282
    Jacek Generowicz
    Jul 8, 2004
  3. Robert Brewer

    RE: Modifying func_closure

    Robert Brewer, Jul 8, 2004, in forum: Python
    Replies:
    4
    Views:
    781
    Jacek Generowicz
    Jul 12, 2004
  4. Jacek Generowicz

    Re: Modifying func_closure

    Jacek Generowicz, Jul 11, 2004, in forum: Python
    Replies:
    7
    Views:
    288
    Bengt Richter
    Jul 12, 2004
  5. Fernando Perez

    Writing func_closure?

    Fernando Perez, Jun 8, 2005, in forum: Python
    Replies:
    5
    Views:
    838
    Michael Hoffman
    Jun 9, 2005
Loading...

Share This Page