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


Groups > comp.lang.python > #8672

Re: Why won't this decorator work?

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!feeder.news-service.com!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'string.': 0.04; 'raised': 0.07; 'roll': 0.07; 'python': 0.08; 'callable': 0.09; 'callable.': 0.09; 'decorator': 0.09; 'from:addr:python': 0.09; 'wrote:': 0.15; 'callable,': 0.16; 'docs,': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'reply-to:addr:python-list': 0.16; 'method.': 0.16; 'this:': 0.16; 'def': 0.16; 'command': 0.19; 'work,': 0.20; "aren't": 0.22; "doesn't": 0.22; 'header:In-Reply- To:1': 0.22; 'code.': 0.22; 'says': 0.25; 'function': 0.26; 'thanks.': 0.26; 'tried': 0.27; "i'm": 0.27; 'random': 0.28; 'received:84': 0.28; 'import': 0.29; 'equivalent': 0.31; 'subject:?': 0.31; 'lines': 0.31; 'error': 0.31; 'to:addr:python- list': 0.34; 'header:User-Agent:1': 0.34; 'things': 0.34; 'defining': 0.35; 'reply-to:addr:python.org': 0.35; 'running': 0.35; 'message.': 0.36; 'but': 0.37; 'getting': 0.38; 'subject:: ': 0.38; 'returning': 0.38; 'should': 0.39; 'to:addr:python.org': 0.39; 'john': 0.62; 'here': 0.66; "'you": 0.67; 'header:Reply- To:1': 0.71; 'reply-to:no real name:2**0': 0.72; 'subject:this': 0.74; 'subject:won': 0.84
X-IronPort-Anti-Spam-Filtered true
X-IronPort-Anti-Spam-Result AsQJADpVD07Unw4S/2dsb2JhbABSmQ+OcneIfMEJhjYEly6LPw
Date Sat, 02 Jul 2011 18:33:33 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.18) Gecko/20110616 Thunderbird/3.1.11
MIME-Version 1.0
To python-list@python.org
Subject Re: Why won't this decorator work?
References <8b8c3ca2-ae36-4009-842e-1dc90fb01b65@ct4g2000vbb.googlegroups.com>
In-Reply-To <8b8c3ca2-ae36-4009-842e-1dc90fb01b65@ct4g2000vbb.googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To python-list@python.org
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.561.1309628013.1164.python-list@python.org> (permalink)
Lines 50
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1309628013 news.xs4all.nl 21844 [2001:888:2000:d::a6]:39651
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:8672

Show key headers only | View raw


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