Passing a callable object to Thread

J

Jeff Schwab

Paul said:
I don't think there's any Python language rule that says multiple uses
of the same numeric literal turn into the same object. It's just an
optimization (constant folding) that the CPython implementation
happens to perform. Other implementations might not do it, or CPython
might do it differently in some future version.

Thanks for clarifying.
 
C

castironpi


(a,) is not identical with a.

The tuple on the left is not identical with the tuple on the right, even
though they are equivalent.

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?

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...

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)- Hide quoted text -

- Show quoted text -

a= set( 'Vanilla', 'Chocolate', 'Strawberry' )
flavors= [ Flavors( i ) for i in a ]
b= list( a )
if len( b )> 1:
b[-1]= "and "+ b[-1]
return ", ".join( b )

string.join() works on sets, but the 'and', appears only before the
last element if there is more than one.

Why not "if b> 1", since len( integer ) always fails?

Could also use

b= ", ".join( a )
try:
return b[ :b.rfind( ',' ) ]+ ' and'+ b[ b.rfind( ',' ): ]
except IndexError:
return b

But -NOT-:

flavors= set( Flavors( 'Vanilla' ), Flavors( 'Chocolate' ),
Flavors( 'Strawberry' ) )
...

since set( Flavors( 'Vanilla' ), Flavors( 'Vanilla' ) ) has size two.

However:

flavors= set( Flavors.Get( 'Vanilla' ), Flavors.Get( 'Chocolate' ),
Flavors.Get( 'Strawberry' ) )

is a possibility:

class Flavors:
@classmethod
def Get( cls, flavor ):
return cls.flavors.setdefault( flavor, cls( flavor ) )
def __init__( self, flavor ):
self.flavor= flavor
flavors= {}

, since __init__ cannot return other objects, but classmethods can,
just like C++.

Fm. Pres. G.H.W. Bush endorses Sen. McCain. Who is Bush "41"?
 
C

castironpi

a= object()

(a,) is not identical with a.

The tuple on the left is not identical with the tuple on the right, even
though they are equivalent.

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?

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...

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)- Hide quoted text -

- Show quoted text -

while True:
"No, it's the element."
"No, it's a tuple with one element."
 
D

Dennis Lee Bieber

The usual CS meaning of "tuple" is more like the physics meaning than
like the Python meaning, I think.
I'd never encountered this physics definition until now...

My exposure to "tuple" came via relational database theory, as part
of the triad: relation, domain, tuple... Which are often loosely equated
as: table, column, row/record -- and once one has normalized the
relations, the tuples fit the concept of each "field" representing a
unique type of entity.

The "ice-cream" example given earlier does /not/ fit the idea of a
tuple to me; "Vanilla", "Chocolate", and "Strawberry" isn't a tuple --
it's a list...

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
C

castironpi

        The "ice-cream" example given earlier does /not/ fit the idea of a
tuple to me; "Vanilla", "Chocolate", and "Strawberry" isn't a tuple --
it's a list...

Flavor* flavors[]= { Vanilla, Chocolate, Strawberry };
flavorct= 3;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top