1

The following piece of code is showing an error:

if ((type(varA) or type(varB) ) == type('t')):
    print "string involved"
elif varA<varB:
    print "RANDOM"

the error is for this case:

Test Values: varA = 0, varB = adios

output:

RANDOM

while this other piece of code

if ((type(varA) == type('t')) or (type(varB)== type('t'))):
    print "string involved"
elif varA<varB:
    print "RANDOM"`

For the following test values:

Test Values: varA = 6, varB = adios

ouput is as follows:

string involved

What is the difference between these two "if" conditions? I am finding them to be of the same logic!

2
  • I don't know python, but I doubt you can do that. In most languages you can't compare (a || b) == something, when you mean (a == something) || (b == something). Commented Oct 21, 2013 at 1:51
  • Why are you checking the type? The Python idiom is to seek forgiveness rather than permission. Commented Oct 21, 2013 at 2:00

3 Answers 3

5

This is wrong:

if ((type(varA) or type(varB) ) == type('t')):

It should be:

if type(varA) == str or type(varB) == str:

Equivalently:

if isinstance(varA, str) or isinstance(varB, str):

Or a bit shorter:

if str in ((type(varA), type(varB)):
Sign up to request clarification or add additional context in comments.

13 Comments

Does it really make semantic sense to use a tuple there? I would use a list.
or even isinstance(varA, str) or isinstance(varB, str)
@WaleedKhan it's the same, but for make it makes more sense to use a tuple, because the elements are not going to be modified
In fact, you can even look at the documentation for isinstance: isinstance(object, class-or-type-or-tuple) -> bool. You can write isinstance(a, (str, list)), but you can't write isinstance(a, [str, list]); try it and see.
@WaleedKhan import this says "Although practicality beats purity." . In this case we know we're only ever going to call __contains__ on this object, so we might as well use the most efficient type available for the purpose.
|
1

The problem is this expression:

if ((type(varA) or type(varB) ) == type('t')):

Programming languages don't work like English. The above first evaluates type(varA) or type(varB), which will yield the type of varA - because or returns the first truthy value and any type is truthy.

Then it will check to see if that is the same as type('t') - that is, str. Which means that it will only be true when varA is a string, and the type of varB is completely ignored.

What you want is probably this:

if type(varA) == type('t') or type(varB) == type('t'):

But there are more idiomatic/Pythonic ways of doing that; see Óscar López's answer for some examples.

Comments

1

If you want the test to work well for subclasses of str as well it's a good idea to use isintance instead of type

if any(isinstance(x, str) for x in (varA, varB)):
    ...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.