Re: Code block function syntax, anonymous functions decorator

Discussion in 'Python' started by Jean-Paul Calderone, Feb 6, 2008.

  1. On Wed, 06 Feb 2008 23:59:27 +0100, "Diez B. Roggisch" <> wrote:
    > schrieb:
    >> def run3( block ):
    >> for _ in range( 3 ):
    >> block()
    >>
    >> run3():
    >> normal_suite()
    >>
    >> Introduces new syntax; arbitrary functions can follow 'colon'.
    >>
    >> Maintains readability, meaning is consistent.
    >>
    >> Equivalent to:
    >>
    >> def run3( block ):
    >> for _ in range( 3 ):
    >> block()
    >>
    >> @run3
    >> def anonfunc():
    >> normal_suite()
    >>
    >> Simplification in cases in which decorators are use often.

    >
    >This is non-sensical - how do you invoke anonfunc? They would all bind
    >to the same name, run3. Or to no name as all, as your "spec" lacks that.


    As he said, the decorator version is the _equivalent_ to the syntax he
    was proposing. The point isn't to decorate the function, so perhaps he
    shouldn't have used decorator syntax, but instead:

    def anonfunc():
    normal_suite()
    run3(anonfunc)
    del anonfunc

    So it's not non-sensical. It's a request for a piece of syntax.

    >
    >Besides, it's butt-ugly IMHO. But taste comes after proper definition...


    It's properly defined. Not that I'm endorsing this or anything. I'd
    rather not see half-assed syntax proposals at all, even if they're super
    great (and some of the syntax that's made it into Python is much worse
    than this).

    Jean-Paul
     
    Jean-Paul Calderone, Feb 6, 2008
    #1
    1. Advertising

  2. Jean-Paul Calderone

    Guest

    On Feb 6, 5:45 pm, Jean-Paul Calderone <> wrote:
    > On Wed, 06 Feb 2008 23:59:27 +0100, "Diez B. Roggisch" <> wrote:
    >
    >
    >
    >
    >
    > > schrieb:
    > >> def run3( block ):
    > >>    for _ in range( 3 ):
    > >>       block()

    >
    > >> run3():
    > >>    normal_suite()

    >
    > >> Introduces new syntax; arbitrary functions can follow 'colon'.

    >
    > >> Maintains readability, meaning is consistent.

    >
    > >> Equivalent to:

    >
    > >> def run3( block ):
    > >>    for _ in range( 3 ):
    > >>       block()

    >
    > >> @run3
    > >> def anonfunc():
    > >>    normal_suite()

    >
    > >> Simplification in cases in which decorators are use often.

    >
    > >This is non-sensical - how do you invoke anonfunc? They would all bind
    > >to the same name, run3. Or to no name as all, as your "spec" lacks that.

    >
    > As he said, the decorator version is the _equivalent_ to the syntax he
    > was proposing.  The point isn't to decorate the function, so perhaps he
    > shouldn't have used decorator syntax, but instead:
    >
    >     def anonfunc():
    >         normal_suite()
    >     run3(anonfunc)
    >     del anonfunc
    >
    > So it's not non-sensical.  It's a request for a piece of syntax.
    >
    >
    >
    > >Besides, it's butt-ugly IMHO. But taste comes after proper definition...

    >
    > It's properly defined.  Not that I'm endorsing this or anything.  I'd
    > rather not see half-assed syntax proposals at all, even if they're super
    > great (and some of the syntax that's made it into Python is much worse
    > than this).
    >
    > Jean-Paul- Hide quoted text -
    >
    > - Show quoted text -


    Yes. @run3( anonfunc ) runs -in-place-. Jean-Paul's was a closer
    equivalent.

    It's used for a piece of code that won't get called like with
    statements.
     
    , Feb 7, 2008
    #2
    1. Advertising

  3. Jean-Paul Calderone

    Guest

    On Feb 6, 8:10 pm, wrote:
    > On Feb 6, 5:45 pm, Jean-Paul Calderone <> wrote:
    >
    >
    >
    >
    >
    > > On Wed, 06 Feb 2008 23:59:27 +0100, "Diez B. Roggisch" <..de> wrote:

    >
    > > > schrieb:
    > > >> def run3( block ):
    > > >>    for _ in range( 3 ):
    > > >>       block()

    >
    > > >> run3():
    > > >>    normal_suite()

    >
    > > >> Introduces new syntax; arbitrary functions can follow 'colon'.

    >
    > > >> Maintains readability, meaning is consistent.

    >
    > > >> Equivalent to:

    >
    > > >> def run3( block ):
    > > >>    for _ in range( 3 ):
    > > >>       block()

    >
    > > >> @run3
    > > >> def anonfunc():
    > > >>    normal_suite()

    >
    > > >> Simplification in cases in which decorators are use often.

    >
    > > >This is non-sensical - how do you invoke anonfunc? They would all bind
    > > >to the same name, run3. Or to no name as all, as your "spec" lacks that..

    >
    > > As he said, the decorator version is the _equivalent_ to the syntax he
    > > was proposing.  The point isn't to decorate the function, so perhaps he
    > > shouldn't have used decorator syntax, but instead:

    >
    > >     def anonfunc():
    > >         normal_suite()
    > >     run3(anonfunc)
    > >     del anonfunc

    >
    > > So it's not non-sensical.  It's a request for a piece of syntax.

    >
    > > >Besides, it's butt-ugly IMHO. But taste comes after proper definition....

    >
    > > It's properly defined.  Not that I'm endorsing this or anything.  I'd
    > > rather not see half-assed syntax proposals at all, even if they're super
    > > great (and some of the syntax that's made it into Python is much worse
    > > than this).

    >
    > > Jean-Paul- Hide quoted text -

    >
    > > - Show quoted text -

    >
    > Yes.  @run3( anonfunc ) runs -in-place-.  Jean-Paul's was a closer
    > equivalent.
    >
    > It's used for a piece of code that won't get called like with
    > statements.- Hide quoted text -
    >
    > - Show quoted text -


    I know of two examples. Thread starting, and event binding in a
    framework GUI. Of course, not too much farther along, for X in A as
    C, storing the code block and/or for loop "object" as a object in "C",
    and likewise for if. These met with disfavor in another place.

    start_new_thread( codeblock as target, args= () ):
    normal_suite()

    bind( "<Key>", codeblock as A ):
    handle_event( event )

    But where does 'event' come from?

    mybind( "<Key>", codeblock as A )( event ):
    handle_event( event )

    ?
     
    , Feb 7, 2008
    #3
  4. Jean-Paul Calderone schrieb:
    > On Wed, 06 Feb 2008 23:59:27 +0100, "Diez B. Roggisch"
    > <> wrote:
    >> schrieb:
    >>> def run3( block ):
    >>> for _ in range( 3 ):
    >>> block()
    >>>
    >>> run3():
    >>> normal_suite()
    >>>
    >>> Introduces new syntax; arbitrary functions can follow 'colon'.
    >>>
    >>> Maintains readability, meaning is consistent.
    >>>
    >>> Equivalent to:
    >>>
    >>> def run3( block ):
    >>> for _ in range( 3 ):
    >>> block()
    >>>
    >>> @run3
    >>> def anonfunc():
    >>> normal_suite()
    >>>
    >>> Simplification in cases in which decorators are use often.

    >>
    >> This is non-sensical - how do you invoke anonfunc? They would all bind
    >> to the same name, run3. Or to no name as all, as your "spec" lacks that.

    >
    > As he said, the decorator version is the _equivalent_ to the syntax he
    > was proposing. The point isn't to decorate the function, so perhaps he
    > shouldn't have used decorator syntax, but instead:
    >
    > def anonfunc():
    > normal_suite()
    > run3(anonfunc)
    > del anonfunc
    >
    > So it's not non-sensical. It's a request for a piece of syntax.
    >
    >>
    >> Besides, it's butt-ugly IMHO. But taste comes after proper definition...

    >
    > It's properly defined. Not that I'm endorsing this or anything. I'd
    > rather not see half-assed syntax proposals at all, even if they're super
    > great (and some of the syntax that's made it into Python is much worse
    > than this).


    Yeah, I missed somehow that the decorator doesn't actually return anything.

    Diez
     
    Diez B. Roggisch, Feb 7, 2008
    #4
  5. Jean-Paul Calderone

    Guest

    On Feb 7, 2:48 am, "Diez B. Roggisch" <> wrote:
    > Jean-Paul Calderone schrieb:
    >
    >
    >
    >
    >
    > > On Wed, 06 Feb 2008 23:59:27 +0100, "Diez B. Roggisch"
    > > <> wrote:
    > >> schrieb:
    > >>> def run3( block ):
    > >>>    for _ in range( 3 ):
    > >>>       block()

    >
    > >>> run3():
    > >>>    normal_suite()

    >
    > >>> Introduces new syntax; arbitrary functions can follow 'colon'.

    >
    > >>> Maintains readability, meaning is consistent.

    >
    > >>> Equivalent to:

    >
    > >>> def run3( block ):
    > >>>    for _ in range( 3 ):
    > >>>       block()

    >
    > >>> @run3
    > >>> def anonfunc():
    > >>>    normal_suite()

    >
    > >>> Simplification in cases in which decorators are use often.

    >
    > >> This is non-sensical - how do you invoke anonfunc? They would all bind
    > >> to the same name, run3. Or to no name as all, as your "spec" lacks that..

    >
    > > As he said, the decorator version is the _equivalent_ to the syntax he
    > > was proposing.  The point isn't to decorate the function, so perhaps he
    > > shouldn't have used decorator syntax, but instead:

    >
    > >    def anonfunc():
    > >        normal_suite()
    > >    run3(anonfunc)
    > >    del anonfunc

    >
    > > So it's not non-sensical.  It's a request for a piece of syntax.

    >
    > >> Besides, it's butt-ugly IMHO. But taste comes after proper definition....

    >
    > > It's properly defined.  Not that I'm endorsing this or anything.  I'd
    > > rather not see half-assed syntax proposals at all, even if they're super
    > > great (and some of the syntax that's made it into Python is much worse
    > > than this).

    >
    > Yeah, I missed somehow that the decorator doesn't actually return anything..
    >
    > Diez- Hide quoted text -
    >
    > - Show quoted text -


    Even to myself, the idea is not sounding too bad. You could require
    that the function have a 'codeblock' argument, (but that goes against
    pythonic "self" style customs), or pass it as an implicit first
    parameter. Hybrid context manager and function.
     
    , Feb 8, 2008
    #5
  6. Jean-Paul Calderone

    Guest

    On Feb 7, 7:13 pm, wrote:
    > On Feb 7, 2:48 am, "Diez B. Roggisch" <> wrote:
    >
    >
    >
    >
    >
    > > Jean-Paul Calderone schrieb:

    >
    > > > On Wed, 06 Feb 2008 23:59:27 +0100, "Diez B. Roggisch"
    > > > <> wrote:
    > > >> schrieb:
    > > >>> def run3( block ):
    > > >>>    for _ in range( 3 ):
    > > >>>       block()

    >
    > > >>> run3():
    > > >>>    normal_suite()

    >
    > > >>> Introduces new syntax; arbitrary functions can follow 'colon'.

    >
    > > >>> Maintains readability, meaning is consistent.

    >
    > > >>> Equivalent to:

    >
    > > >>> def run3( block ):
    > > >>>    for _ in range( 3 ):
    > > >>>       block()

    >
    > > >>> @run3
    > > >>> def anonfunc():
    > > >>>    normal_suite()

    >
    > > >>> Simplification in cases in which decorators are use often.

    >
    > > >> This is non-sensical - how do you invoke anonfunc? They would all bind
    > > >> to the same name, run3. Or to no name as all, as your "spec" lacks that.

    >
    > > > As he said, the decorator version is the _equivalent_ to the syntax he
    > > > was proposing.  The point isn't to decorate the function, so perhaps he
    > > > shouldn't have used decorator syntax, but instead:

    >
    > > >    def anonfunc():
    > > >        normal_suite()
    > > >    run3(anonfunc)
    > > >    del anonfunc

    >
    > > > So it's not non-sensical.  It's a request for a piece of syntax.

    >
    > > >> Besides, it's butt-ugly IMHO. But taste comes after proper definition....

    >
    > > > It's properly defined.  Not that I'm endorsing this or anything.  I'd
    > > > rather not see half-assed syntax proposals at all, even if they're super
    > > > great (and some of the syntax that's made it into Python is much worse
    > > > than this).

    >
    > > Yeah, I missed somehow that the decorator doesn't actually return anything.

    >
    > > Diez- Hide quoted text -

    >
    > > - Show quoted text -

    >
    > Even to myself, the idea is not sounding too bad.  You could require
    > that the function have a 'codeblock' argument, (but that goes against
    > pythonic "self" style customs), or pass it as an implicit first
    > parameter.  Hybrid context manager and function.- Hide quoted text -
    >
    > - Show quoted text -



    Sometimes, it's more appropriate to write

    @call
    def f():
    normal_suite()

    than

    def f():
    normal_suite()
    f().

    It's clearer to the eye and reader, and truer to the meaning of the
    code. From reading the docs, it's pretty clear that it's not what the
    author meant for decorators. So, even though it's good and practical,
    as well as Pythonic, it doesn't get in. However, from the writing, he
    or she is most reasonable himself and practical. Or herself. How
    does one reconcile these observations? Not just anyone can invent a
    computer language, but we all know what happens later to emperors and
    the like. Will they listen to reason? Depends. Is Python someone's
    baby to raise, as court says, "any way he or she wants?" Does he or
    she want people to like it? Does he or she only want to please
    himself? Or herself? Anyone for a controlling share, hostile
    takeover, or court injunction or protection order? Gollum was a
    Hobbit too once....

    Maybe someone is getting back at "us", "the rest", "the world", for
    ideas he or she had early on that weren't accepted. Does he or she
    even read these things down here? What factors in to his or her
    decision? If someone "on top" doesn't like an idea, is it a strong
    indicator that it's bad? That it's good? If myself, I don't
    understand, it's one thing. If "they" are wrong, it's another. If he
    or she is just shy, and won't get behind something clean and cool,
    only without first making a friend, then maybe, after all, I can
    relate.

    If it's bad, they should be able to tell me why. Is it? Can they?
    Will they?

    Though don't forget: there's only so much that can fit in a language.
    There are only so many letters, so many punctuation marks (many of
    which are in use), and syntax is pricey-pricey. Clearly, extensions
    should be general.

    $ to execute, .. to download, ## to get the size of a list, etc., cf.
    Perl, are one side of this coin. Math and logic are the other: it is
    so general, in fact, that it's outside cause altogether.

    How well you speak English is well-defined. How well you speak Python
    is too. Your choice of custom-syntax languages, though, may not be.
    Once a syntax is defined in a language, is the rest of the program in
    it? Say it like this: There's only so many specifics that -ought- to
    go into any given line of code.

    If it's not clear by now, I agree with many of the constraints that
    "our someone" has imposed on Python-- or, more accurately, demanded of
    us, its speakers, requests they have made for the future of it. I've
    had some proposals rejected. They didn't fit in with-- what? The
    powers that be are not impartial; they are human, prejudiced emotions,
    battered childhoods, and all.

    I like to think that a good idea would get in to a language I create.
    Does it? No holds barred, no strings attached, no further
    prerequisites, only that its good? Perhaps "up there", priorities are
    different; and it turns out that whatever it is constitutes "good" is
    not, after all, objective. In other words, that there's no such
    thing, only favorites. I can't say that pride has always been
    entirely dissociated from my own evaluations of ideas I've even had
    myself.

    Anyway, what are the constraints on a language? If something is too
    big to fit in a person's mind, too broad, or too general; too small,
    too specific, or too distracting, it doesn't get in. Even if it
    should. Computer languages are spoken by people too. There is a
    scarcity of resources, "time" in the sense of perception and the human
    condition: memory and attention, available to any person, hence to any
    aim, and if they're already allocated, then the long answer is, "the
    people on top can't afford to think about it. Bring some, not give
    some, that is, time, to think about it." Time is -a- money, of course
    not money itself; it's a scarcity, and time on Earth is too. Short
    answer: "Sure. Buy it." And yes, this applies to politics and
    religion. But I digress.

    How did Python's creator get the idea for it? How long did it take to
    push it? And what through? What are the hurdles? What sponsors it,
    what are its interests, and can I donation-style-buy, the equivalent
    of a university building to it? How much does my idea cost? ... in
    time -or- money. As an aside, one can always trust Gollum to hold a
    Ring. It's only a riddle to get it back.

    Are the criteria for what is Pythonic, and what isn't, -com-plete-ly-
    objective? Are they written anywhere, or even spoken? Alternatively,
    do they go without saying? If they're not, that's a shame. If they
    are, does it take judgment to see how closely an idea fulfills them?

    Some of the criteria I've heard so far are, "it is in widespread use,"
    and, "Guy X likes it." I don't know Guy X, but Guy Y plays favorites
    and takes bribes. What about X? There's an objective time frame on
    the former: is it, how long has it been so, and will it ever be; is it
    ahead of its time, after its time, right on time, and does it have
    one? What's involved in the latter is, for the lack of a better word,
    personal. But then again, are those on top equipped to evaluate the
    former? I'm not, and where am I going with this. Time of course will
    tell; stick around.

    For my part, and for the idea of topic, the strongest argument I have
    follows. If we ever end up with poly-core PCs, multiple pipelines,
    decoupled perpipherals, or parallel, "many small" types of hardware,
    the ability to "hand around" ounce-sized blocks of code might be one
    way to avail of them, and here's at least one way to do it.

    If no one designs for a poly-core PC, and no one builds one, what? Do
    hardware manufacturers know where software manufacturers are taking
    this? Vice versa? Who has crosshairs on the future? Is leadership a
    responsibility? Who takes it? Who leads? Take me to him ...or her!
     
    , Feb 8, 2008
    #6
  7. On Feb 8, 6:50 am, wrote:
    > Sometimes, it's more appropriate to write
    >
    > @call
    > def f():
    >    normal_suite()
    >
    > than
    >
    > def f():
    >    normal_suite()
    > f().
    >
    > It's clearer to the eye and reader, and truer to the meaning of the
    > code.  From reading the docs, it's pretty clear that it's not what the
    > author meant for decorators.  So, even though it's good and practical,
    > as well as Pythonic, it doesn't get in.


    As I remarked in a recent post, it's already almost in, but it's
    called '@apply' (I don't know what your @call returns):

    @apply
    def the_answer(x=6):
    return x*(x+1)

    print the_answer

    :)

    --
    Arnaud
     
    Arnaud Delobelle, Feb 8, 2008
    #7
  8. Jean-Paul Calderone

    Guest

    On Feb 8, 1:08 am, Arnaud Delobelle <> wrote:
    > On Feb 8, 6:50 am, wrote:
    >
    >
    >
    >
    >
    > > Sometimes, it's more appropriate to write

    >
    > > @call
    > > def f():
    > >    normal_suite()

    >
    > > than

    >
    > > def f():
    > >    normal_suite()
    > > f().

    >
    > > It's clearer to the eye and reader, and truer to the meaning of the
    > > code.  From reading the docs, it's pretty clear that it's not what the
    > > author meant for decorators.  So, even though it's good and practical,
    > > as well as Pythonic, it doesn't get in.

    >
    > As I remarked in a recent post, it's already almost in, but it's
    > called '@apply' (I don't know what your @call returns):
    >
    > @apply
    > def the_answer(x=6):
    >     return x*(x+1)
    >
    > print the_answer
    >
    > :)
    >
    > --
    > Arnaud- Hide quoted text -
    >
    > - Show quoted text -


    Python 3.0a2 (r30a2:59405M, Dec 7 2007
    >>> apply

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    NameError: name 'apply' is not defined

    I understand it's "To be removed" [1]. These proposals are similar
    [2] [3].

    What is not in is a full-fledged anonymous function. That is a code
    block.

    I personally have been focussing on multi-threading, looking to the
    future, so forgive the one-sided example.

    @start_new_thread
    def anonA():
    normal_suite()


    Similar.
    start_new_thread():
    normal_suite()

    Or, if you're using other arguments, or the function does not take a
    function as its first parameter, rearrange parameters with a standard
    function.

    Makes it hard to join or call if you ever need it more than once. I
    don't believe that assigning the return to a name fits in with the
    grand scheme.

    Yes:

    def convenientstart( func ):
    Thread( target= func ).start()

    @convenientstart
    def anonA():
    normal_suite()

    Yes:

    def convenientstart( func ):
    th= Thread( target= func, args= ( th, ) )
    th.start()
    return th#, but where does it go?

    convenientstart():
    normal_suite()

    No:

    def convenientstart( func ):
    Thread( target= func ).start()

    convenientstart() as thA:
    normal_suite()

    I dream:

    def convenientstart( func, A ):
    th= Thread( target= func, args= ( th, A ) )
    th.start()
    return th#, but where does it go?

    th= convenientstart( A ):
    normal_suite()

    Cf. bound object instance calls: code block is the applicable "self".

    Another day, perhaps.

    I am extremely open to feedback. Asking to brainstorm. Thoughts
    welcome. Any?


    [1] http://www.python.org/dev/peps/pep-3100/ .
    [2] http://mail.python.org/pipermail/python-ideas/2007-October/001083.html
    [3] http://mail.python.org/pipermail/python-ideas/2007-October/001086.html
     
    , Feb 9, 2008
    #8
    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. morrell
    Replies:
    1
    Views:
    1,006
    roy axenov
    Oct 10, 2006
  2. glomde
    Replies:
    5
    Views:
    549
    glomde
    Mar 29, 2007
  3. Reporter
    Replies:
    3
    Views:
    498
    Mike Schilling
    May 12, 2007
  4. Replies:
    2
    Views:
    246
  5. Replies:
    1
    Views:
    241
Loading...

Share This Page