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


Groups > comp.lang.python > #104655

Re: hasattr() or "x in y"?

Path csiph.com!weretis.net!feeder6.news.weretis.net!nntp.club.cc.cmu.edu!micro-heart-of-gold.mit.edu!bloom-beacon.mit.edu!bloom-beacon.mit.edu!panix!not-for-mail
From Grant Edwards <invalid@invalid.invalid>
Newsgroups comp.lang.python
Subject Re: hasattr() or "x in y"?
Date Fri, 11 Mar 2016 22:30:46 +0000 (UTC)
Organization PANIX Public Access Internet and UNIX, NYC
Lines 36
Message-ID <nbvgum$5v6$1@reader1.panix.com> (permalink)
References <nbve7c$nbo$1@dont-email.me> <nbvf69$105$1@reader1.panix.com> <nbvg8a$vcv$1@dont-email.me>
NNTP-Posting-Host 67-130-15-94.dia.static.qwest.net
X-Trace reader1.panix.com 1457735446 6118 67.130.15.94 (11 Mar 2016 22:30:46 GMT)
X-Complaints-To abuse@panix.com
NNTP-Posting-Date Fri, 11 Mar 2016 22:30:46 +0000 (UTC)
User-Agent slrn/1.0.2 (Linux)
Xref csiph.com comp.lang.python:104655

Show key headers only | View raw


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?

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

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

csiph-web