4 ms·
When I write a routine should I always "assert" that its parameter is a list and not a generator? Do I have to write the code to use a slower technique just in
by Goladus 17y ago
When I write a routine should I always "assert" that its parameter is a list and not a generator? Do I have to write the code to use a slower technique just in case the parameter is a generator? Do I "ungenerate" the generator out into a list?
That the caller can be trusted to use the proper type in arguments to a function a basic assumption in Python. The decision about how to handle type mismatch errors is up to you. You can handle generators any way you'd like, depending on your needs. There are other modern languages that don't make the same assumptions Python does.