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: 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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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.