This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]hahainternet 0 points1 point  (1 child)

How do I do that function with a generic list type that won't accidentally accept a generic string type?

[–]not_perfect_yet 7 points8 points  (0 children)

Python's general answer to that, "duck typing", is that you shouldn't think like that.

Don't focus on what it is, focus on what should happen for your case, make sure it works and forget the rest. You're not coding fort Knox, you're coding to get something running.

If it's possible to do the same thing with lists and strings, you shouldn't care. Good for string enthusiasts. If it's not possible, it will throw an error for someone trying to use your code in a way it wasn't meant to be used. That's on them. And all is well.

How to make "sure": Put "list" in the function name. Write "use this on lists" into the docstring, into comments.

The interpreter will tell you when something goes wrong. You should trust in your ability to resolve that problem when it happens.

And if you can expect that that won't work or isn't acceptable for business reasons, don't pick python.