typechecking

Check if input is a list/tuple of strings or a single string

Check if input is a list/tuple of strings or a single string Question: I’ve a method that I want to be able to accept either a single string (a path, but not necessarily one that exists on the machine running the code) or a list/tuple of strings. Given that strings act as lists of characters, …

Total answers: 9

Type checking of arguments Python

Type checking of arguments Python Question: Sometimes checking of arguments in Python is necessary. e.g. I have a function which accepts either the address of other node in the network as the raw string address or class Node which encapsulates the other node’s information. I use type() function as in: if type(n) == type(Node): do …

Total answers: 5

What is the best (idiomatic) way to check the type of a Python variable?

What is the best (idiomatic) way to check the type of a Python variable? Question: I need to know if a variable in Python is a string or a dict. Is there anything wrong with the following code? if type(x) == type(str()): do_something_with_a_string(x) elif type(x) == type(dict()): do_somethting_with_a_dict(x) else: raise ValueError Update: I accepted avisser’s …

Total answers: 10