2

(Maybe b/c I'm from a C++ world) I want to verify some python variable is

list(string) or list(dict(int, string)) or SomethingIterable(string)

Is there a simple and unified way to do it? (Instead of writing customized code to iterate and verify each instance..)

I emphasize that I understand in Python list can have elements of different types, which is exactly the reason why I ask how to verify a list which are composed by just a certain type e.g. string.

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
Nan Hua
  • 3,414
  • 3
  • 17
  • 24
  • 2
    Have you tried `isinstance`? – OneCricketeer Sep 16 '16 at 23:32
  • `hasattr(var, "__iter__")`? – YOU Sep 16 '16 at 23:34
  • 1
    Possible duplicate of [Determine the type of a Python object](http://stackoverflow.com/questions/2225038/determine-the-type-of-a-python-object) – TemporalWolf Sep 16 '16 at 23:41
  • when you say list, do you actually need a list, or an iterable, or a subscriptable, or both? – njzk2 Sep 16 '16 at 23:45
  • 1
    So I think everyone is showing this is the [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem): Why do you want to do type verification? Python is an [EAFP](http://stackoverflow.com/questions/11360858/what-is-the-eafp-principle-in-python) language – TemporalWolf Sep 16 '16 at 23:46

3 Answers3

4

In Python lists can be composed of mixed types, there is no way to do something like setting the "type" of a list. Also, even if you could, this "type" is not enforced and could change at any time.

jmercouris
  • 348
  • 5
  • 17
  • 1
    This. `["Hi", 0, count()]` is a valid list in python. – TemporalWolf Sep 16 '16 at 23:44
  • 1
    Note: As a rule, `list`s _should_ typically be of homogeneous type. But yeah, it's completely unenforced/unenforceable; we're all adults, and if you want to do terrible things with `list`s, that's between you, your conscience, and the pitchfork wielding mobs of programmers who have to read/maintain your code later. `tuple` is either homogeneous (if being used logically as an immutable `list`), or heterogeneous (when being used logically as a "lightweight object"; e.g. any case where `collections.namedtuple` might be appropriate). – ShadowRanger Sep 17 '16 at 01:15
1

It seems that you are looking for an array (array.array), not a list:

>>> l = [1]
>>> l.append('a')
>>> import array
>>> a = array.array('l')
>>> a.append(3)
>>> a.append('a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required (got type str)
>>> a
array('l', [3])

As you get more and more comfortable with Python, though, you will gradually learn how to structure your code in such a way that type-checking becomes unnecessary. Personally, I've never had to use an array.array in Python (except in cases like this, where I'm specifically working with that module).

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
-3

Use the typing module

Typically:

from typing import List

def method(value: List[str]):
    pass
njzk2
  • 38,969
  • 7
  • 69
  • 107