Skip to content

Instantly share code, notes, and snippets.

@garethr
Created January 8, 2019 14:43
Show Gist options
  • Save garethr/d197a091d8816d2833fc014db7a6d0d2 to your computer and use it in GitHub Desktop.
Save garethr/d197a091d8816d2833fc014db7a6d0d2 to your computer and use it in GitHub Desktop.
Feedback on Python API

I'm writing a Python library, and trying to decide on a specific, top level, API. Basically is it nicer to pass a list of objects or just pass a dict?

Option 1:

method(credentials=[
    Credential(name="something", value="something else")  
])

Option 2

method(credentials={
  something: "something else"
})

Option 1 is more verbose, but also more explicit. It's harder to make errors with, and makes those errors easier to catch. Option 2 is just data, which is both less verbose and also easier to string together from external sources. Thoughts?

@tonnydourado
Copy link

tonnydourado commented Jan 8, 2019

I'd say that it depends on how complex will credentials be. If it's just key: value, where value is scalar (or close enough), yeah, sure, dict is fine. Maybe even **kwargs. If the values might be very complex, or there might be lots of different keys with different meanings, than I'd say objects are better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment