0

Let's say I have on object x. I want to check if x is of a type I support (let's say it's lists, sets, strings and None. And without types that inherit from them), so I figured I'll just check

type(x) in (list, set, str, NoneType)

However, NoneType cannot be referable. Is there a clean solution, or must I do something like this?

x is None or type(x) in (list, set, str)

Is there a standard way to access <type 'NoneType'>?

Thanks.

10
  • 1
    You mean type(None) ?
    – khelwood
    Apr 5, 2018 at 7:38
  • Thanks! Didn't think of that. Though I still think developers should have access to NoneType.
    – MattS
    Apr 5, 2018 at 7:45
  • 1
    IIRC, they were going to put NoneType to types along with other inaccessible types (FunctionType, FrameType, etc.) in 3.0, until Guido asked (a) whether anyone could imagine writing types.NoneType instead of type(None) (after all, digging up a frame object to call type on it isn't trivial, but None is always right there), and (b) whether there was a good use for isinstance checks on None except as part of some set you built up out of like (since there is only one NoneType value, and you're supposed to test it with is), and everyone agreed he was right.
    – abarnert
    Apr 5, 2018 at 7:49
  • I prefer this: isinstance(x, (str, list, set, type(None))). I'm also interested in figure out which approach could be considered more pythonic.
    – slackmart
    Apr 5, 2018 at 7:51
  • @slackmart this won't work If I want to exclude types that inherit from str \ list\ set
    – MattS
    Apr 5, 2018 at 7:55

2 Answers 2

5

How about type(None)? Just do:

type(x) in (list, set, str, type(None))

seems to work for me, and is quite clear to me at least. Since there is only one value of NoneType, using it as a literal is hardly "magic".

7
  • Wow, didn't think of that! I guess it's probably better than my idea of "type(x) is None or type(x) in (list, set, str)"
    – MattS
    Apr 5, 2018 at 7:40
  • 2
    @user2594596 You mean x is None not type(x) is None. Anyway that's not necessarliy a bad solution. A bit less readable. But it would be even better if you didn't allow x to have different types at all.
    – freakish
    Apr 5, 2018 at 7:46
  • @freakish thanks, fixed my typo. I need to filter a list from unsupported types so there's no escape.
    – MattS
    Apr 5, 2018 at 7:48
  • 2
    @user2594596 Right, by why would you allow different types in the list in the first place? I.e. instead of doing type(x) in (a,b,c) why not simply isinstance(x, MyClass) and only allow MyClass in the list (and get rid of None as well). IMO mixing unrelated types in a list is not a good practice.
    – freakish
    Apr 5, 2018 at 7:51
  • 2
    @user2594596 That's a weird thing to avoid to be honest. Usually in OOP you want to have objects with a common base class/interface in a list. Usually you want isinstance instead of directly checking types. Perhaps you have a case were it is ok. But I strongly suggest you rethink the architecture.
    – freakish
    Apr 5, 2018 at 8:00
2

If you want to access it without calling type() you can always refer to its __class__, i.e.

type(x) in (list, set, str, None.__class__)
1
  • Great solution!
    – MattS
    Apr 5, 2018 at 8:11

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.