• Re: What sort of exception when a class can't find something?

    From Peter J. Holzer@21:1/5 to Chris Green via Python-list on Thu Aug 31 22:53:28 2023
    On 2023-08-31 21:32:04 +0100, Chris Green via Python-list wrote:
    What sort of exception should a class raise in __init__() when it
    can't find an appropriate set of data for the parameter passed in to
    the class instantiation?

    E.g. I have a database with some names and address in and have a
    class Person that gets all the details for a person given their
    name.

    ....
    ....
    person.Person('Fred')
    ...
    ...


    If Fred doesn't exist in the database what sort of exception should
    there be? Is it maybe a ValueError?

    It you are going for a builtin exception, I think KeyError is the most appropriate: It should be a LookupError, since the lookup failed and a
    database is more like a mapping than a sequence.

    But it would probably be best to define your own exception for that.

    hp

    --
    _ | Peter J. Holzer | Story must make more sense than reality.
    |_|_) | |
    | | | [email protected] | -- Charles Stross, "Creative writing
    __/ | http://www.hjp.at/ | challenge!"

    -----BEGIN PGP SIGNATURE-----

    iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmTw/cMACgkQ8g5IURL+ KF2GcQ/9H+cJU0sSptuMsHdoA8CmnibAdXAmTKBdoVwaha2mheDSrcFa/+mDDEqY LskOZjimOeUcNrsYTKkPYxz0Ud6AeYPvqgMReXStavBvZ0HV9qYAadDJStiJt+lx uKTwZgyrWCGf6CpHhurDfr+hmDVDjQ82AvEYRn8GXM0RxaqfMNFQ25hGDen6uyN/ +nV8IdVx/GdmEknFWsBaKWwz+G++b/x6bm0wQSZM6cfskqNLoZYBsuKvPYz4DyMS +LI5E21IKq1AeJH/0laE3H7UOynqHoCvuqx6NNHOHwPFbNklA5TZk+8uvK8xxQEq +54QvCcaa0wMjl37qPPAtIFdWA46ZXrAZMXT0uFbY0gorLuwe34X1L0LMpGc9wAV 8eKUv4C+dzQgZwXicjVO4fnjZgt74e5w8LdS4qzokiXaQQmf6Oqy/hdpyp9cMjpz AfKT9WSUFRjeEmXXNXCi6Ov8C06UlrETRw2JxPQ+9/ngmXBGKk4XsOl0fudWgIwd iEvJB8gsRkPcIdwJ7m7VA0swk5FP0izUPj9GZXBL855PxBftWPz9S7AoEMGVzhJa 6Xfcf4jAdeq/QNVolvos6zJw2V+4N/HlBXMJUmS00ieyMUKk+7UuubmZnSIcULj1 GPxE88936OYfc6LMfIf+lcNVal2EzaF/HnsaYqV
  • From Chris Angelico@21:1/5 to [email protected] on Fri Sep 1 07:01:39 2023
    On Fri, 1 Sept 2023 at 06:39, Chris Green via Python-list <[email protected]> wrote:

    What sort of exception should a class raise in __init__() when it
    can't find an appropriate set of data for the parameter passed in to
    the class instantiation?

    E.g. I have a database with some names and address in and have a
    class Person that gets all the details for a person given their
    name.

    ....
    ....
    person.Person('Fred')
    ...
    ...


    If Fred doesn't exist in the database what sort of exception should
    there be? Is it maybe a ValueError?


    There's no clear answer to this, because you aren't really
    constructing a Person here. So there are a few options that seem
    pretty reasonable:

    1) As you say, raise ValueError. The problem is the value passed in
    (it's the right type, but the value wasn't found), so, ValueError.
    2) KeyError. This emphasizes the fact that you're effectively looking
    up in a mapping. Quite odd for a constructor though.
    3) A custom RecordNotFound exception. You're doing something unusual,
    so make it your own exception.

    TBH I would suggest making a slightly different API:

    person.Person.from_name('Fred')

    ie a classmethod alternate constructor. These can most definitely
    raise ValueError when the value given isn't appropriate:

    datetime.datetime.fromordinal(-1)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ValueError: ordinal must be >= 1

    and it makes good sense for a method like this to be doing lookups,
    rather than construction per se. (At a technical level, it's
    presumably constructing new objects.)

    To help with making that decision, what happens if you construct two
    Person objects for the same actual person? Would you return the same
    object (ie maintain a cache and deduplicate)? Or does each one take a
    snapshot of the data at the instant of construction, and thus you can
    observe changes through time by constructing more? Both are reasonable
    and make sense, but they lend themselves to slightly different
    approaches.

    ChrisA

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Green@21:1/5 to All on Thu Aug 31 21:32:04 2023
    What sort of exception should a class raise in __init__() when it
    can't find an appropriate set of data for the parameter passed in to
    the class instantiation?

    E.g. I have a database with some names and address in and have a
    class Person that gets all the details for a person given their
    name.

    ....
    ....
    person.Person('Fred')
    ...
    ...


    If Fred doesn't exist in the database what sort of exception should
    there be? Is it maybe a ValueError?


    --
    Chris Green
    ·

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Green@21:1/5 to All on Thu Aug 31 22:08:58 2023
    Several helpful replies, thank you all.

    --
    Chris Green
    ·

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Chris Green on Fri Sep 1 12:14:41 2023
    Chris Green <[email protected]> writes:
    What sort of exception should a class raise in __init__() when it
    can't find an appropriate set of data for the parameter passed in to
    the class instantiation?

    Possibilities:

    'EnvironmentError',
    'FileNotFoundError',
    'NotImplemented',
    'NotImplementedError', or
    'ValueError'.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)