Question about idomatic Python. Suppose I have a function:
def a_function(list_of_things):
for item in list_of_things:
process_item(item)
Now suppose it's hard for me to be sure that the input argument is a list. Even now, I hear a sweet pythonista choir pleading with me: "Use duck print and exceptions!"
def a_function(list_of_things):
try:
for item in list_of_things:
process_item(item)
except:
pass
And it’s great if I don’t go to the map, not a line, or anything else that will repeat as I think is “wrong” for this particular application.
I don't write tons of Python, but I manage to run into this and related situations often enough to annoy me regularly. More generally, I want to use duck typing and exceptions, it seems to be a convention in Python, but getting in many cases the wrong input type that passes my exception; so many that exceptions seem to be the wrong answer as often as not (or at least quite often).
Even if exceptions seem to be the right answer, I’m still afraid when I use them, because if I just didn’t think about the corner where the bad type would go, leaving my code due to the lack of a better term, “evaded”.
, type isinstance. , Python, ? , Python, , .
, :
- , . .,.
- . , ( ?)
- . .
<python thing for this situation>? !
- ?
. SO, , , "" , .