Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'value,': 0.04; 'argument': 0.05; 'subject:Python': 0.06; '(so': 0.07; 'args': 0.07; 'compiler': 0.07; 'odd': 0.07; 'plenty': 0.07; 'arguments': 0.09; 'claimed': 0.09; 'inserted': 0.09; 'properly.': 0.09; 'violates': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'bug': 0.12; 'stored': 0.12; '*args': 0.16; '*args):': 0.16; 'bug,': 0.16; 'inspects': 0.16; 'marco': 0.16; 'obj,': 0.16; 'tuple': 0.16; 'tuple,': 0.16; 'write,': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'discussion': 0.18; 'bit': 0.19; 'stack': 0.19; '>>>': 0.22; 'import': 0.22; 'hack': 0.22; 'cc:addr:python.org': 0.22; 'header:User-Agent:1': 0.23; "haven't": 0.24; 'environment': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'first,': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; "doesn't": 0.30; 'code': 0.31; '"",': 0.31; 'inspect': 0.31; 'work:': 0.31; 'file': 0.32; 'class': 0.32; 'another': 0.32; '(most': 0.33; 'cases': 0.33; 'guess': 0.33; 'totally': 0.33; "i'd": 0.34; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'consistent': 0.36; 'edge': 0.36; 'entry': 0.36; 'wrong': 0.37; 'so,': 0.37; 'expected': 0.38; 'handle': 0.38; 'pm,': 0.38; 'recent': 0.39; 'does': 0.39; 'reported': 0.39; 'how': 0.40; 'even': 0.60; 'simple': 0.61; 'first': 0.61; 'act': 0.63; 'name': 0.63; 'kind': 0.63; 'decided': 0.64; 'to:addr:gmail.com': 0.65; 'determine': 0.67; 'special': 0.74; 'behavior': 0.77; 'batchelder': 0.84; 'beats': 0.84; 'complexity': 0.84; 'execution.': 0.84; 'zen': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=o98aQqzulegqBajtNMt2G+UEiANj5A9pz3yFiRLQbSw=; b=oxf03VFyQWZCoCsWat8Lp7h9HaCO2PMvG5OrOoE44koO8RqpWEUNPmyZJiTyTfBW0F em8bmAkYxD2J3m8GSBTN8JMW22HL3PZ3YPEOLtT20j1Yx5Odwg7sMjEY+/xIXHF9bfY5 CIta4UWw9R3cXnOK0NekQpncY1Ngi0e6Tu5lYiTlWJ6t/4mpe4UEJwKv2WMPs3N5sYQ6 fHP7E8vbu38dLPlE1IPToxcVPnNpXVMqfLH7Wq7W7wI4g7g57tHEzrJWtTNrnUhShTSb 8uhnsfZFe69BEuuULINgpE4SwCp2f9Sp+WusoprtJdsq+6su5OLRKq/uvdJpmevHkPBH WBow== X-Received: by 10.229.130.135 with SMTP id t7mr8941202qcs.18.1381403078320; Thu, 10 Oct 2013 04:04:38 -0700 (PDT) Sender: Ned Batchelder Date: Thu, 10 Oct 2013 07:04:38 -0400 From: Ned Batchelder User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/20130801 Thunderbird/17.0.8 MIME-Version: 1.0 To: Marco Buttu Subject: Re: super in Python 3 and variadic arguments References: <52565598.6000709@gmail.com> In-Reply-To: <52565598.6000709@gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 66 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1381403081 news.xs4all.nl 15915 [2001:888:2000:d::a6]:47082 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:56558 On 10/10/13 3:22 AM, Marco Buttu wrote: > On 10/09/2013 06:47 PM, Ned Batchelder wrote: > >>> >>> class B(A): >>> ... def bfoo(*args): >>> ... super().afoo(*args[1:]) >>> ... >>> >>> B().bfoo(1, 2, 3) >>> Traceback (most recent call last): >>> File "", line 1, in >>> File "", line 3, in bfoo >>> RuntimeError: super(): no arguments >>> >>> How come? >> >> The no-args super() call inspects the calling environment to determine >> the class and self. "self" is the first local name stored in >> frame.f_code.co_localsplus, but *args doesn't put "args" into that entry >> of the code object > > But is it a bug or the behavior we want? The first (implicit) argument > is stored as expected as the first one in the args tuple, and the args > tuple is inserted as expected in frame.f_locals: > > >>> import inspect > >>> class B(A): > ... def bfoo(*args): > ... frame = inspect.currentframe() > ... for obj, value in frame.f_locals.items(): > ... print(obj, value, sep=' --> ') > ... # super().afoo(*args[1:]) > ... > >>> B().bfoo(1, 2, 3) > args --> (<__main__.B object at 0x7f28c960a590>, 1, 2, 3) > frame --> > > So, why does not super use it? > I haven't seen the discussion that decided the behavior of super(), but I'd guess that if you reported this as a bug, it would be closed as wontfix, because: 1) the use case you describe isn't something people actually write, 2) it would add to the complexity of super() to support it, and 3) there's a simple way to write your code that does work: class B(A): def bfoo(self, *args): super().afoo(*args) (though it's a bit odd to call afoo from bfoo.) Python has never claimed the kind of purity that makes everything work in a totally simple consistent way. super() with no args is a kind of hack to begin with. It involves a special case in the compiler (so that using the name "super" as a function call will act as if you had accessed the name "__class__" so that super can find it later), and inspecting the stack frame during execution. It's an interesting case of the Zen of Python. It violates one ("explicit is better than implicit"), but only because of another one ("practicality beats purity"). super(MyClass, self) in Python 2 is the kind of brain-bender that so many people get wrong at first, that it's helped plenty of people to do the arguments implicitly, even if there are oddball edge cases that it doesn't seem to handle properly. --Ned.