hasattr() or "x in y"?

Mark Lawrence breamoreboy at yahoo.co.uk
Fri Mar 11 17:17:09 EST 2016


On 11/03/2016 22:00, Grant Edwards wrote:
> On 2016-03-11, Charles T. Smith <cts.private.yahoo at 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?"
>

The BartC answer would be that it doesn't matter provided it runs 
quickly.  The RUE answer would be that he can't get it to work anyway 
because of the Python3 FSR.  RR I'm not sure about.  Mark Janssen would 
state that Python shouldn't have an object called 'str', and Ilias 
Lazaridis would still be evaluating and hence wouldn't have made up his 
mind.  Who have I missed?  Ah, I remember, that Dutch idiot who created 
a programming language that was so useless it only lasted for 25 years. 
  Now what was his name, and what was the name of the language?

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

Mark Lawrence




More information about the Python-list mailing list