Passing a callable object to Thread

Jeff Schwab jeff at schwabcenter.com
Mon Feb 18 19:12:24 EST 2008


Paul Rubin wrote:
> Jeff Schwab <jeff at schwabcenter.com> writes:
>>>> In CS, a tuple is a kind of data structure that is specifically not
>>>> identical with any of its elements.  That's the sort of tuple used in
>>>> Python.
> 
> The usual CS meaning of "tuple" is more like the physics meaning than
> like the Python meaning, I think.  

That has not been my experience.

>>>>>> (a,) is (a,)
>>> False
>> The tuple on the left is not identical with the tuple on the right,
>> even though they are equivalent.
> 
> Implementation artifact.  It could be constant folded.
>    x = (a,)
>    y = x
>    x is y
> should print True.

I gave a similar example, but you snipped it.

>> An interesting thing about Python is that numbers of built-in types
>> are flyweights.  Unlike literals of non-flyweight types, distinct
>> instances of a given numeric literal actually refer to the same object:
>>
>>  >>> 5 is 5
>> True
>>  >>> 999999999999999999999999999999 is 999999999999999999999999999999
>> True
>>  >>> 3.5 is 3.5
>> True
> 
> Again an implementation artifact, not guaranteed by the language.  Try:
>   (5+1) is (5+1)

True

> then try
>   (999999999999999999999999999999+1) is (999999999999999999999999999999+1)

False

I think you're a little confused about the meaning of "numeric literal." 
  (5+1) is not a numeric literal.  Neither is 
(999999999999999999999999999999+1).

The flyweight pattern does not guarantee that all equivalent instances 
of an object type will be identical.  Maybe I should have said that 
*some* distinct instances of a given numeric literal actually refer to 
the same object, so as not to imply that *all* of them did.  I don't 
know whether 5 is always guaranteed to be the same object as any other 5 
in a given Python session.



More information about the Python-list mailing list