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


Groups > comp.lang.python > #104646 > unrolled thread

hasattr() or "x in y"?

Started by"Charles T. Smith" <cts.private.yahoo@gmail.com>
First post2016-03-11 21:44 +0000
Last post2016-03-11 22:30 +0000
Articles 8 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  hasattr() or "x in y"? "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-03-11 21:44 +0000
    Re: hasattr() or "x in y"? Chris Angelico <rosuav@gmail.com> - 2016-03-12 08:55 +1100
    Re: hasattr() or "x in y"? "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-03-11 21:53 +0000
      Re: hasattr() or "x in y"? Chris Angelico <rosuav@gmail.com> - 2016-03-12 09:04 +1100
      Re: hasattr() or "x in y"? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-11 22:05 +0000
    Re: hasattr() or "x in y"? Grant Edwards <invalid@invalid.invalid> - 2016-03-11 22:00 +0000
      Re: hasattr() or "x in y"? "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-03-11 22:18 +0000
        Re: hasattr() or "x in y"? Grant Edwards <invalid@invalid.invalid> - 2016-03-11 22:30 +0000

#104646 — hasattr() or "x in y"?

From"Charles T. Smith" <cts.private.yahoo@gmail.com>
Date2016-03-11 21:44 +0000
Subjecthasattr() or "x in y"?
Message-ID<nbve7c$nbo$1@dont-email.me>
From the performance point of view, which is better:
- hasattr()
- x in y

TIA
cts

[toc] | [next] | [standalone]


#104647

FromChris Angelico <rosuav@gmail.com>
Date2016-03-12 08:55 +1100
Message-ID<mailman.9.1457733330.12893.python-list@python.org>
In reply to#104646
On Sat, Mar 12, 2016 at 8:44 AM, Charles T. Smith
<cts.private.yahoo@gmail.com> wrote:
> From the performance point of view, which is better:
> - hasattr()
> - x in y
>

Mu. They do completely different things.

ChrisA

[toc] | [prev] | [next] | [standalone]


#104648

From"Charles T. Smith" <cts.private.yahoo@gmail.com>
Date2016-03-11 21:53 +0000
Message-ID<nbvep7$pb3$1@dont-email.me>
In reply to#104646
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...

[toc] | [prev] | [next] | [standalone]


#104651

FromChris Angelico <rosuav@gmail.com>
Date2016-03-12 09:04 +1100
Message-ID<mailman.11.1457733894.12893.python-list@python.org>
In reply to#104648
On Sat, Mar 12, 2016 at 8:53 AM, Charles T. Smith
<cts.private.yahoo@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#104652

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2016-03-11 22:05 +0000
Message-ID<mailman.12.1457733974.12893.python-list@python.org>
In reply to#104648
On 11/03/2016 21:53, 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...
>

Dict, don't you mean collection?  Or, to put it another way, what 
exactly were you originally asking?

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#104650

FromGrant Edwards <invalid@invalid.invalid>
Date2016-03-11 22:00 +0000
Message-ID<nbvf69$105$1@reader1.panix.com>
In reply to#104646
On 2016-03-11, Charles T. Smith <cts.private.yahoo@gmail.com> wrote:
> From the performance point of view, which is better:

> - hasattr()
> - x in y

Dunno, is red an even or odd color?  How many pancakes does it take to
cover a doghouse?

>>> "x" in "asdf"
False
>>> "x" in "xyz"
True

>>> "asdf".hasattr("x")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'hasattr'
>>> "xyz".hasattr("x")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'hasattr'

Since they behave differently, perhaps the question ought to be "which
does what you want to do?"

-- 
Grant Edwards               grant.b.edwards        Yow! I had pancake makeup
                                  at               for brunch!
                              gmail.com            

[toc] | [prev] | [next] | [standalone]


#104653

From"Charles T. Smith" <cts.private.yahoo@gmail.com>
Date2016-03-11 22:18 +0000
Message-ID<nbvg8a$vcv$1@dont-email.me>
In reply to#104650
On Fri, 11 Mar 2016 22:00:41 +0000, Grant Edwards wrote:

> Since they behave differently, perhaps the question ought to be "which
> does what you want to do?"

For parsed msgs, I had this:

              elif hasattr (msg.msgBody, 'request'):

It occurred to me that this was less abstruse:

              elif 'request' in msg.msgBody:

and by the way, how would you do that with duck-typing?
If I were doing this anew, I probably use a dictionary of functors,
but that's not an option anymore.
              

[toc] | [prev] | [next] | [standalone]


#104655

FromGrant Edwards <invalid@invalid.invalid>
Date2016-03-11 22:30 +0000
Message-ID<nbvgum$5v6$1@reader1.panix.com>
In reply to#104653
On 2016-03-11, Charles T. Smith <cts.private.yahoo@gmail.com> wrote:
> On Fri, 11 Mar 2016 22:00:41 +0000, Grant Edwards wrote:
>
>> Since they behave differently, perhaps the question ought to be "which
>> does what you want to do?"
>
> For parsed msgs, I had this:
>
>               elif hasattr (msg.msgBody, 'request'):
>
> It occurred to me that this was less abstruse:
>
>               elif 'request' in msg.msgBody:

If you want to know if msg.msgBody has an attribute named 'request'
then use hasattr().

If you want to know if msg.msgBody "contains"[1] the string 'request'
then use "in".

_They're_two_different_things_

[1] for some definition of "contains" that depends on the type of
msg.msgBody.

> and by the way, how would you do that with duck-typing?

Do WHAT?

> If I were doing this anew, I probably use a dictionary of functors,
> but that's not an option anymore.

-- 
Grant Edwards               grant.b.edwards        Yow! Were these parsnips
                                  at               CORRECTLY MARINATED in
                              gmail.com            TACO SAUCE?

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web