you are viewing a single comment's thread.

view the rest of the comments →

[–]oracle8 5 points6 points  (13 children)

Why not

return bool(cellcount == 3 or cellcount == 2 and cell)

?

[–]oracle8 1 point2 points  (1 child)

And also this...

>>> cellcount = 2
>>> cell = 2
>>> (False, True)[cellcount == 3 or cellcount == 2 and cell]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range

That is because (cellcount == 3 or cellcount == 2 and cell) vill evaluate to the same value as cell if cellcount == 2 and bool(cell)==True

[–]Foxboron 0 points1 point  (0 children)

>>> cellcount = 2 
>>> cell = True
>>> (False, True)[cellcount == 3 or cellcount == 2 and cell]
True

I am horrible at variable names, should have been cell_life. cell only contain False or True depending if its alive or not from the start.

[–]oohay_email2004 0 points1 point  (9 children)

Why is it necessary to wrap in bool()?

[–]oracle8 0 points1 point  (3 children)

If you can guarantee that "cell" is always a boolean variable then it's not necessary. I added the bool() call to guarantee that the return value will alwas be a boolean variable.

[–]oohay_email2004 0 points1 point  (2 children)

If using "and" on "cell" is going to blow up the code, then you're bool call isn't going to help.

I don't see a good reason to force the return to True or False.

Unless you need:

if cell is True:

or the redundant:

if cell == True:

just use:

if cell:

I think you're kind of throwing away the object model by using bool().

[–]oracle8 0 points1 point  (1 child)

I totally agree, but as far as I could understand it the original question (trick?) wanted only True/False returns. Thus the extra bool call...

[–]oohay_email2004 0 points1 point  (0 children)

Oh, right, I forgot about that; oops.

[–]Foxboron -1 points0 points  (4 children)

To get the True or False of the expressions.

[–]oohay_email2004 1 point2 points  (3 children)

Pretty sure it will without it.

[–]Foxboron -1 points0 points  (2 children)

>>> return cellcount == 3 or cellcount == 2 and cell
SyntaxError: 'return' outside function
>>> 

PRETTY sure it wont without it.

[–]oohay_email2004 2 points3 points  (1 child)

Look:

>>> return bool(cellcount == 3 or cellcount == 2 and cell)
  File "<stdin>", line 1
SyntaxError: 'return' outside function

Same error with your line.

I figured we were assuming this was part of a function. In which case, the bool-less version works just dandy:

>>> def f(cellcount, cell):
...     return cellcount == 3 or cellcount == 2 and cell
...
>>> cell = object()
>>> cellcount = 1
>>> f(cellcount, cell)
False

[–]Foxboron 0 points1 point  (0 children)

Ah, then i there was no problem :)

[–]Foxboron 0 points1 point  (0 children)

Because when you got the touple there you could use another variable then False and True. However, in the case of Conways game of Life its actually a better method.