Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: hasattr() or "x in y"? Date: Sat, 12 Mar 2016 09:04:46 +1100 Lines: 23 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de L+cGas4AVl2+gtZ7F1b3rgErXP9jBVA7ZQskmcZO05iQ== 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; 'operator': 0.03; 'attributes': 0.07; 'cc:addr:python-list': 0.09; "object's": 0.09; '"in"': 0.16; '2016': 0.16; 'attributes,': 0.16; 'better:': 0.16; 'dictionary,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'latter,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'looked': 0.16; '(in': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'am,': 0.23; 'sat,': 0.23; 'header:In-Reply-To:1': 0.24; 'fri,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'looks': 0.29; 'dictionary': 0.29; "we're": 0.30; 'point': 0.33; 'class': 0.33; 'view,': 0.33; 'members.': 0.34; 'received:google.com': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'subject:: ': 0.37; '12,': 0.37; 'received:209.85.213': 0.37; 'doing': 0.38; "won't": 0.38; 'received:209': 0.38; 'still': 0.40; 'back': 0.62; 'different': 0.63; 'between': 0.65; 'mar': 0.65; "they're": 0.66; 'smith': 0.76; 'chrisa': 0.84; 'to:none': 0.91 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; bh=23gND1jMUlaBtwgCcmOFqGy+zLu0+owGm9RHYDhz4uU=; b=VedUotk1NEY5kaulkN+SDNnWleML7/5Bkkq5vkCVq+DzuueO638XYZVvC+W3YyWMme 1YwNI46k2NRyNFy47lrGEVB5MntdIDdmWZgDGOvkjQ1YYDLvasiCBAa1ii86N41u+u1O Ib4ipZHKQ2X4nb5+xFB2ED5Lt1p6FBfNrdI2EGrvHE7h9OBS8AgCaCv6FGdYM8M9Umzs AwYe8dILaAFo2Nd0P/KrGc86Qam5GMRANKRVuovEflZeicVLtZAF2RpqUxG/8xDdqAAr Ujn7bBWjC2z4+jt/gs5DD9qZOayaatHCTMfbaNEieMCynaw8U0z4mprCR3KFXy0MrIES 0oXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=23gND1jMUlaBtwgCcmOFqGy+zLu0+owGm9RHYDhz4uU=; b=EKqjmH4IZMQrV4Awj69Y24iz8xNiHJk3GPDcQFUOKQiIV6nW9KWDJuojdImdHh9sFD oSYDKUbOS36NWpGyeadhHnso3VtwaxLk6gL0nl3X46sMSXtM+XinSAb9Vfx94m1D6lXC 0hK8FXgpk0VDBdS21NNHPuLabtiKJDpS0eel7p3gUS2V/hPIO1HzWKLppqc7yf1TdGyx HsdiHybqAOBwZlTL5+ZIuouFSJN92idiNOm4ZdGGpV/ScNSkPJz2NPeE8jIiSWxEVcSz KnJ3df0KyMVb/TJn+ACGQpexcS8IFHp+DrQCcQ/0BP4WkQF1cwrIwrigH4n57OTYrYM9 ZrAg== X-Gm-Message-State: AD7BkJL8Nj7vTfr8n+kWo10VTuO1iBz3aWzLYkaHjs6xEhCh5WtRU6O6BqOyMKCfxAqP1qNpcVRi3oj00f/q/Q== X-Received: by 10.50.43.226 with SMTP id z2mr6693661igl.94.1457733886563; Fri, 11 Mar 2016 14:04:46 -0800 (PST) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:104651 On Sat, Mar 12, 2016 at 8:53 AM, Charles T. Smith wrote: > On Fri, 11 Mar 2016 21:44:27 +0000, Charles T. Smith wrote: > >> From the performance point of view, which is better: - hasattr() >> - x in y >> >> TIA >> cts > > > I just realized that "in" won't look back through the class hierarchy... > that clearly makes them not interchangable, but given we're only > interested in the current dict... They're still completely different - hasattr looks at attributes, but the 'in' operator looks at the object's members (in the case of a dictionary, the keys). There is a broad similarity between "hasattr(obj, attrname)" and "attrname in obj.__dict__", but if you're doing the latter, you have other problems than performance; attributes should be looked up as attributes, not as dictionary members. ChrisA