Type checking: an iterable type that is not a string

后端 未结 2 401
庸人自扰
庸人自扰 2020-12-16 14:08

To explain better, consider this simple type checker function:

from collections import Iterable
def typecheck(obj):
    return not isinstance(obj, str) and i         


        
2条回答
  •  粉色の甜心
    2020-12-16 14:30

    I check it with this code and it work fine with Python 2 and 3

    from __future__ import unicode_literals
    import types
    import collections
    
    var = ["a", "b", "c"]
    if isinstance(var, collections.Iterable) and \
            not isinstance(var, types.StringTypes):
        return var
    

提交回复
热议问题