What other languages use the same data model as Python?
John Nagle
nagle at animats.com
Thu May 5 11:58:33 EDT 2011
On 5/5/2011 6:59 AM, Steven D'Aprano wrote:
> On Thu, 05 May 2011 21:48:20 +1000, Chris Angelico wrote:
>
>> On Thu, May 5, 2011 at 9:44 PM, Mel<mwilson at the-wire.com> wrote:
>>> John Nagle wrote:
>>>> On 5/4/2011 5:46 PM, harrismh777 wrote:
>>>>> Or, as stated earlier, Python should not allow 'is' on immutable
>>>>> objects.
>>>>
>>>> A reasonable compromise would be that "is" is treated as "==" on
>>>> immutable objects.
>>>
>>> I foresee trouble testing among float(5), int(5), Decimal(5) ...
>>
>> Define 'x is y' as 'type(x)==type(y) and
>> isinstance(x,(int,float,tuple,etc,etc,etc)) and x==y' then.
That's close to the right answer.
>
> `is` is supposed to be a *fast* operator, not even slower than equality
> testing.
That's an implementation problem. Those are cheap tests at the
machine code level. An efficient test looks like this:
def istest(a, b) :
if id(a) == id(b) : # the cheap address test
return(True)
if type(x) != type(y) : # cheap binary comparison
return(False)
if mutable(x) : # the interpreter knows this
return(False)
return(x == y) # equality test for mutables
Probably about 12 machine instructions, and the full "==" test
is only reached for cases in which "is" now produces wrong answers.
It's encouraging that a Google code search finds no matches of
if .* is \"
or
if .* is 1
in Python code.
John Nagle
More information about the Python-list
mailing list