0

What can be the best to check if var is having str, unicode or None; but not anything else?

I tried with

if not isinstance(ads['number'], (str, unicode, None)):
    ....

but got below exception:

TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types

`

4 Answers 4

3

You have the right idea, but None is not a class. It's type is NoneType, or alternatively type(None). The latter works out of the box in both Python 2 and 3. The former requires an import: from types import NoneType and only works in Python 2.

Sign up to request clarification or add additional context in comments.

2 Comments

NameError: global name 'NoneType' is not defined ... This is new error when I use NoneType ... but worked with type(None) .... accepting ans.
@Bhuro. You have to do from types import NoneType. I'll mention that.
3

you can use

if not isinstance(a, (str, unicode, type(None))):
    ....

and in python 2 (which you seem to be using) this also works:

from types import NoneType
if not isinstance(a, (str, unicode, NoneType)):
    ....

4 Comments

What about Python 3?
@MadPhysicist the first version works in python 3. but unicode is not python 3, that's why i did not mention it explicitely.
If the first option is working in both python 2 & 3, why we should use an extra import for just python 2? Is there any specific advantage of option 2 over option 1 for python 2 only? just for curiosity...
@Bhuro NoneType just avoids a call to type which i prefer (if you mind the import you could also assign NoneType = type(None) to avoid the function call.
1

You can use type to find the method and check that in a list

Ex:

if type(None) in [str, unicode, type(None)]:
    print "ok"

5 Comments

None is not a type
Worked ... Code consistency will be changed ..as I have used most of places with isinstance ... so any more possible solutions..
Updated snippet to check None
if type(None)...? You sure?
He might be intended to use var and used type(None) instead in if condition.....
1

In complement to the other answers (which are correct), I would add a comment on the typology of cases where one would need such a test.

Distinguishing between a string and a scalar type

If the question is distinguishing between a string and a number, then asking for forgiveness instead of permission works just as well:

a = 1
try:
    b = a.upper()
except AttributeError:
    ...

Since that's the mindset of Python, it often makes things simpler, and it also takes care of the case where a is None. If one is trying to prevent such an error, then it might be easier to let it instead happen and then catch it. It could also be more reliable, because it could also take care of cases one had not thought of.

Distinguishing between a string and other iterables

One case where it won't work, however, is when one wants to distinguish between a string and a list (several libraries do that with function arguments). The problem is that both a string and a list are iterables, so the string might happily run through the code for the list without any error... except that now you have your string cut into pieces of one character.

>>> for el in a: print(el.upper())
...
H
E
L
L
O

(And if the code fails, the error could be confusing.) To avoid that effect:

a = "hello"
if isinstance(a, str):
    ...
else:
    ...

In my experience, it's the only case where it's really advisable to use a test with isinstance(x, (str,...)). I am curious to know whether someone knows others?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.