#confused on python if command

1 messages · Page 1 of 1 (latest)

waxen kiln
#
if type(takeInt) is str:
#

if takeInt is str checks if takeInt is literally the same thing as the concept of a string

lime minnow
#

Use isinstance not type is

if isinstance(takeInt, str):
smoky root
#

Hello

#

What’s the issue here?

lime minnow
#

if type(x) is X:
That only checks if x is an X

if isinstance(x, X):
That checks is x is any kind of X

So basically if x is a Y and Y is a subtype of X the first example will fail, the second will correctly identify x as an X

#

In your case it doesn't really matter, you'll likely never see a subclass of str but because using type in an is check can have that edge case it's considered a bad practice and you should use isinstance instead