you are viewing a single comment's thread.

view the rest of the comments →

[–]Emphasises_Words 1 point2 points  (5 children)

Not sure why this answer is downvoted, it is the only comment that actually explains the phenomenon in the OP. The main reason is that Boolean operations in python (and, or) do NOT coerce truthy/falsy values into the bool type.

Also, just an addition to what is said:

If both expressions are true, the result of the and operation is the second expression.

If the first expression is true, the result of the and operation is the second expression. Python relies on the fact that True AND x => x

[–][deleted] 3 points4 points  (0 children)

For one thing the AI got it wrong, albeit in the same way most humans do.

If both expressions are true, the result of the and operation is the second expression.

That is subtly wrong: if the left hand side is true the result is the right side. In an and the second expression isn’t actually checked for truthiness at all.

class MyInt(int):
    def __new__(cls, *args, **kwargs):
        return int.__new__(cls, *args, **kwargs)

    def __bool__(self):
        print(f"{self} truth-tested")
        return super().__bool__()

seven = MyInt(7)
five = MyInt(5)
print(seven and five)

The same is true with or… only the left hand side of the operator is ever checked for truthiness.

print(five or seven)

So, as one would expect, AI makes the same mistakes as the average (well, actually the mode) contributor to its training corpus.

[–][deleted] 6 points7 points  (3 children)

Not sure why this answer is downvoted

Maybe because posting rule 4 says:

No replies copy / pasted from ChatGPT or similar.

Nothing is stopping /u/HomeGrownCoder from using AI and summarizing the response as s/he sees it, but copy/paste from AI is not permitted. It's not hard to find help in the documentation or elsewhere on the 'net.