Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #8672
| Date | 2011-07-02 18:33 +0100 |
|---|---|
| From | MRAB <python@mrabarnett.plus.com> |
| Subject | Re: Why won't this decorator work? |
| References | <8b8c3ca2-ae36-4009-842e-1dc90fb01b65@ct4g2000vbb.googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.561.1309628013.1164.python-list@python.org> (permalink) |
On 02/07/2011 17:56, John Salerno wrote:
> I thought I had finally grasped decorators, but the error I'm getting
> ('str' type is not callable) is confusing me. Here is my code. Also,
> the commented sentence is from the Python docs, which says it doesn't
> even need to be callable, if that matters. I also commented out a few
> things in the move method. They were just to see if it would work, but
> those lines raised even more errors!
>
> import random
>
> #space = 0
>
> def move(roll):
> # global space
> # space += roll
> return 'You moved to space {0}.'.format(roll)
>
> @move
> def roll_die():
> return random.randint(1, 6)
>
> # The return value of the decorator need not be callable
> # roll_die = move(roll_die)
>
>
>
> I tried running the command "roll_die()" and I get the error message.
>
> Thanks.
A decorator should return a callable.
This:
@move
def roll_die():
return random.randint(1, 6)
is equivalent to this:
def roll_die():
return random.randint(1, 6)
roll_die = move(roll_die)
You should be defining a function (a callable) and then passing it to a
decorator which returns a callable.
As it is, you're defining a function and then passing it to a decorator
which is returning a string. Strings aren't callable.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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