Issue 6751: Default return value in ConfigParser (original) (raw)
I think it is useful, at least for me, to add an argument, default, to [Safe,Raw]ConfigParser.get that, if present, will be returned if the methid fails to return the value.
That is, instead of rasing an exception, return default, if present.
It could be done overriding the get method in SafeConfigParser, something like this:
class SafeConfigParser(ConfigParser): def get(self, section, option, raw=False, vars=None, **kwds): try: return super().get(section, option, raw, vars) except Exception as exc: if "default" in kwds: return kwds["default"] raise exc
I would like the method to have the exact same behavior as before if the "default" argument is not present, and return the given default value when "deafult" argument is present.
If you simply add a "default" keyword, it will always be present and you wouldn't know if the user wants the exception thrown or the default value returned.
Do you know how to program this using a "default" keyword argument?