The error condition they support is only missing data, not malformedĪnd it is hard, or inefficient, to “Look Before You Leap” and checkįor example, getattr(obj, name, default), dict.get(key, default), The other builtin functions that offer defaults tend to have these two People to handle errors without thought, when they really do need to It too much of an “attractive nuisance”, something that encourages Recommended design, baked into the int and other builtins. Go for it, it’s yourīut I would not want this raised up to an official supported and Some arbitrary default you plucked from thin air. Prefer, including not handling it at all but merely replacing it with That you shouldn’t be permitted to do whatever error handling you Or because I typoed “97w5” when I meant 9735.īut what you do in your code is up to you. I cannot imagine a scenario where I, as the user, would consider itĪcceptable to use 0 because I entered “635.1” when an int was expected, Replacement with a default value is surely going to lead to “Garbage In, (sometimes) a reasonable thing to do, but the empty string is not the We might agree that converting the empty string to some default value is If this is a reasonable suggestion, what about adding a default option to other builtin constructors that can raise a ValueError such as float() or complex()? In the case of CPython, it looks like this could by achieved by adding a single extra if statement around here which if reached and default was set would then return the default. What do people think of adding a keyword argument default= to int() which is used if the input would raise a ValueError? This would allow these examples to then be expressed as x = int(user_input, default=0) and y = int(oup(0), default=1). To handle edge cases in which int(x) raises a ValueError, this usually involves having to write code like: Many times I have found myself writing code to convert strings containing user input, regex matches etc.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |