Passing a callable object to Thread
Jeff Schwab
jeff at schwabcenter.com
Tue Feb 19 00:23:38 CET 2008
castironpi at gmail.com wrote:
> On Feb 18, 4:26 pm, Jeff Schwab <j... at schwabcenter.com> wrote:
>> Lie wrote:
>>> On Feb 16, 12:29 pm, Jeff Schwab <j... at schwabcenter.com> wrote:
>>>> Paul Rubin wrote:
>>>>> Jeff Schwab <j... at schwabcenter.com> writes:
>>>>>> Why not? They seem intuitive to me. I would find it weird if you
>>>>>> couldn't have 0-tuple, and even weirder if you couldn't have a
>>>>>> 1-tuple. Maybe my brain has been warped by too much C++ code.
>>>>> The idea is that a 2-tuple (of numbers, say) is a pair of numbers, a
>>>>> 3-tuple is three numbers, and a 1-tuple is one number. That would
>>>>> mean a number and a 1-tuple of numbers are the same thing, not
>>>>> separate types.
>>>> No, that doesn't follow. A set with one element is not the same thing
>>>> as that element, a sequence of one element is not the same thing as that
>>>> element, and a tuple with one element is not the same thing as that element.
>>> Probably the analogue of tuples in human language would be like this:
>>> A: What ice-cream flavour do you have?
>>> B: "Vanilla", "Chocolate", and "Strawberry"
>>> If, for example, he only have Vanilla:
>>> A: What ice-cream flavour do you have?
>>> B: "Vanilla"
>>> This way of thinking makes 1-tuple the same as the element itself.
>> Yes. I first heard the term "tuple" in a physics class, where it was
>> used to mean that a mathematical function took an arbitrary number of
>> objects. It was by analog with "triple, quadruple, quintuple...
>> n-tuple." That's a different context than computer science, though,
>> which is a specific branch of mathematics with its own terminology. 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.- Hide quoted text -
>
>>>> a= object()
>>>> (a,) is a
> False
(a,) is not identical with a.
>>>> (a,) is (a,)
> False
The tuple on the left is not identical with the tuple on the right, even
though they are equivalent.
>>>> a is a
> True
The variable on the left is identical with the one on the right. This
is not the same comparison as "(a,) is (a,)", which actually contains
the construction of two distinct objects. The moral equivalent of "a is
a" would be:
>>> b = (a,)
>>> b is b
True
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
I wonder, will this be true of the upcoming Fraction class?
>>>> (a,) == (a,)
> True
The two tuples are equivalent (though not identical).
>>>> a= []
>>>> a.append( a )
>>>> a
> [[...]]
That's cool. I don't think would have known off the top of my head how
the interactive interpreter would display something like that. Talk
about a cyclic reference...
>>>> tuple(a) is tuple(a)
> False
The tuple on the left is not identical with the tuple on the right, even
though they are equivalent. This is the sample as one of your earlier
examples, just with slightly different syntax.
> hasVanilla= True
> hasStrawberry= True
> hasChocolate= True
> if hasVanilla:
> print "Vanilla"
> if hasVanilla and not hasChocolate:
> print "and"
> if hasStrawberry:
> print "Strawberry"
> if hasVanilla or hasStrawberry and hasChocolate:
> print "and"
> if hasChocolate:
> print "Chocolate."
You've tried to implement a set using a set of flags to indicate whether
various items have membership in that set. See how an object
representing a given flavor would have to be distinct from the object
(boolean flag) indicating its set membership? Btw, your formatting
could use some work. :) Some flavor combinations cause extra "ands" to
be printed. Here's a little test harness, with PEP-friendly variable
names, and showing how your booleans corresponding directly with
traditional bit-bucket flag sets:
def print_flavors(flags):
print flags
vanilla = flags & 1
strawberry = flags & 2
chocolate = flags & 4
if vanilla:
print "Vanilla"
if vanilla and not chocolate:
print "and"
if strawberry:
print "Strawberry"
if vanilla or strawberry and chocolate:
print "and"
if chocolate:
print "Chocolate."
if __name__ == '__main__':
for flavor_flags in range(8):
print_flavors(flavor_flags)
More information about the Python-list
mailing list