Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.roellig-ltd.de!open-news-network.org!border2.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'classes,': 0.05; 'class,': 0.07; 'methods,': 0.09; 'subject:instance': 0.09; 'subject:method': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; "wouldn't": 0.14; 'class:': 0.16; 'cleaner': 0.16; 'factories': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'lambda': 0.16; 'rewritten': 0.16; 'subject:class': 0.16; 'subject:which': 0.16; 'types,': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'module,': 0.24; 'passes': 0.24; 'skip': 0.24; 'fine': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'sort': 0.25; "i've": 0.25; 'header:In-Reply- To:1': 0.27; 'function': 0.29; '[1]': 0.29; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'class': 0.32; 'skip:c 30': 0.32; 'themselves': 0.32; 'fri,': 0.33; 'actual': 0.34; 'problem': 0.35; 'classes': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'instances': 0.36; 'done': 0.36; 'subject:?': 0.36; 'should': 0.36; 'being': 0.38; 'pm,': 0.38; 'rather': 0.38; 'expect': 0.39; 'subject:" ': 0.39; 'ensure': 0.60; 'here:': 0.62; 'times': 0.62; 'between': 0.67; 'gathering': 0.68; 'fact,': 0.69; '2015': 0.84; 'hassle': 0.84; 'esteemed': 0.91; 'to:none': 0.92; 'before?': 0.93; 'wanting': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=YFWOCo42V+8TA6sKEjoVOpOi9Gjk8eAj++H4VscD5R4=; b=IdHd27oBPVwgEvFvTiQDxJTnQYlF5Qd9WZXESDh1PgHLcTri1TwNxCZGDqNGbtiT8o NVdtwOLqY3ZwbrCP2IoWkkQVvySREU/r/3jXln/9HRXGKivQGtbTiXyPkOyFx0LJnM+w 5vSFfGu+XsDc68w+wa6xzVT3tBkQfeL1Qk+WgLjzUCpjSFyVZU/Y5y/SBo9UgLdlCmRE GgQ4UoTYTG2XVsRPRQzlSuoKjhnjMUyria1hvGZpi0oYhVLCVmJPBnvxMpMV0DfA0mrs DR6vJtEo6Qhvv4eOhNNgnfHVZD/KQxE6Lgq3UeXVGPkuPJFm5RHq+7DijHStaFTpZNI+ ncLQ== MIME-Version: 1.0 X-Received: by 10.42.43.199 with SMTP id y7mr3929446ice.12.1431089422431; Fri, 08 May 2015 05:50:22 -0700 (PDT) In-Reply-To: References: Date: Fri, 8 May 2015 22:50:22 +1000 Subject: Re: A __call__ "method" which is itself another callable class instance? From: Chris Angelico Cc: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list 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: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1431089425 news.xs4all.nl 2876 [2001:888:2000:d::a6]:54196 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:90162 On Fri, May 8, 2015 at 10:24 PM, Skip Montanaro wrote: > The question for the esteemed gathering here: have you ever encountered the > need for this class-instance-as-a-dunder-call-method before? Perhaps in some > sort of code generation situation? Or cleaner functions-with-attributes? I've never actually done it, but there've been times when I've considered setting __call__ to be a class, rather than an actual function. (Though the time I wanted it, I was wanting to set __call__ on a module, and I ended up not bothering with the hassle that that entails.) Python classes are automatically factories of themselves [1], so there should be no difference between calling a class and calling a function that passes through to the class: foo = Bar @functools.wraps(Bar) def foo(*args, **kw): return Bar(*args, **kw) In fact, I would fully expect things like functools.wraps to work just fine on classes, types, and instances of classes with __call__ methods, just as they would with functions - in the same way that you'd expect a def function to work just as well as a lambda function. If your code _does_ get into an infinite loop, so would calling the function. I wouldn't see a problem with that being rewritten as "while True", just to ensure the equivalencies. ChrisA [1] https://xkcd.com/387/