Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8689

Re: Why won't this decorator work?

From Ben Finney <ben+python@benfinney.id.au>
Newsgroups comp.lang.python
Subject Re: Why won't this decorator work?
References <8b8c3ca2-ae36-4009-842e-1dc90fb01b65@ct4g2000vbb.googlegroups.com> <mailman.561.1309628013.1164.python-list@python.org> <98cca0c2-8d4f-4313-abed-3f34fa165c6d@u28g2000yqf.googlegroups.com> <mailman.562.1309632366.1164.python-list@python.org> <a61a8a98-609a-4fb2-ba1a-45c9c1a374db@w4g2000yqm.googlegroups.com>
Date 2011-07-03 08:36 +1000
Message-ID <87y60gjon4.fsf@benfinney.id.au> (permalink)
Organization Unlimited download news at news.astraweb.com

Show all headers | View raw


John Salerno <johnjsal@gmail.com> writes:

> Basically what I want to do is this: I first to need to roll a die to
> get a random number, then pass that number to the move method of a
> Player class. Can this be done with a decorator, or is it better just
> to do it like move(roll_die()) and be done with it?

If what you want can be expressed with ‘player.move(roll_die())’, I'm
confused as to why you think decorators would help.


A decorator is syntactic sugar for something quite different::

    @foo(spam, eggs)
    @bar
    def baz(beans):
        """ … """

is equivalent to::

    def baz(beans):
        """ … """

    baz = foo(spam, eggs)(bar(baz))

except in the first instance, the function defined never gets bound to
the name ‘baz’ even temporarily.

* The function ‘baz’ is defined, resulting in a function object. (In the
  first example, it is never bound to the name ‘baz’.)

* The expression ‘bar’ is evaluated, and the result (probably a function
  named ‘bar’) is called with the function object defined as ‘baz’; the
  return value should be a new function.

* The expression ‘foo(spam, eggs)’ is evaluated, and the result (the
  return value from that function call) is itself called, passing the
  return value from the call to ‘bar’.

* Finally, the return value from all of that is bound to the name ‘baz’.

Note that, in both ways of writing that, ‘baz’ is never called, but
instead the function object ‘baz’ is used as a parameter. Also, the
original function defined as ‘baz’ is no longer accessible.


That seems quite unrelated to your stated requirements. If you're not
accustomed to passing function objects around as data, that can be
difficult to wrap your head around; if you didn't know you needed it,
you probably don't in this case.

-- 
 \     “Jealousy: The theory that some other fellow has just as little |
  `\                                         taste.” —Henry L. Mencken |
_o__)                                                                  |
Ben Finney

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Why won't this decorator work? John Salerno <johnjsal@gmail.com> - 2011-07-02 09:56 -0700
  Re: Why won't this decorator work? MRAB <python@mrabarnett.plus.com> - 2011-07-02 18:33 +0100
    Re: Why won't this decorator work? John Salerno <johnjsal@gmail.com> - 2011-07-02 11:08 -0700
      Re: Why won't this decorator work? Tim Chase <python.list@tim.thechases.com> - 2011-07-02 13:45 -0500
        Re: Why won't this decorator work? John Salerno <johnjsal@gmail.com> - 2011-07-02 13:39 -0700
          Re: Why won't this decorator work? Ben Finney <ben+python@benfinney.id.au> - 2011-07-03 08:36 +1000
          Re: Why won't this decorator work? "OKB (not okblacke)" <brenNOSPAMbarn@NObrenSPAMbarn.net> - 2011-07-03 18:01 +0000
            Re: Why won't this decorator work? John Salerno <johnjsal@gmail.com> - 2011-07-03 20:24 -0700
            Re: Why won't this decorator work? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-07-04 20:22 +1200
      Re: Why won't this decorator work? Ian Kelly <ian.g.kelly@gmail.com> - 2011-07-02 12:49 -0600
        Re: Why won't this decorator work? Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2011-07-03 16:14 +1200
      Re: Why won't this decorator work? ChasBrown <cbrown@cbrownsystems.com> - 2011-07-02 12:16 -0700
      Re: Why won't this decorator work? ChasBrown <cbrown@cbrownsystems.com> - 2011-07-02 12:17 -0700
      Re: Why won't this decorator work? ChasBrown <cbrown@cbrownsystems.com> - 2011-07-02 12:16 -0700
      Re: Why won't this decorator work? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-03 12:11 +1000
        Re: Why won't this decorator work? John Salerno <johnjsal@gmail.com> - 2011-07-02 19:14 -0700

csiph-web