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.

5
  • 2
    Have you tried isinstance? Commented Sep 16, 2016 at 23:32
  • hasattr(var, "__iter__")? Commented Sep 16, 2016 at 23:34
  • 1
    Possible duplicate of Determine the type of a Python object Commented Sep 16, 2016 at 23:41
  • when you say list, do you actually need a list, or an iterable, or a subscriptable, or both? Commented Sep 16, 2016 at 23:45
  • 1
    So I think everyone is showing this is the XY Problem: Why do you want to do type verification? Python is an EAFP language Commented Sep 16, 2016 at 23:46

3 Answers 3

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.

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

2 Comments

This. ["Hi", 0, count()] is a valid list in python.
Note: As a rule, lists 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 lists, 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).
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).

Comments

-3

Use the typing module

Typically:

from typing import List

def method(value: List[str]):
    pass

2 Comments

That's type annotation, but it's purely for static analysis; it performs no checking at all either at compile or run time.
that's still a start, though.

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.