Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #95418 > unrolled thread

return types from library api wrappers

Started by"Joseph L. Casale" <jcasale@activenetwerx.com>
First post2015-08-16 23:31 +0000
Last post2015-08-16 23:31 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  return types from library api wrappers "Joseph L. Casale" <jcasale@activenetwerx.com> - 2015-08-16 23:31 +0000

#95418 — return types from library api wrappers

From"Joseph L. Casale" <jcasale@activenetwerx.com>
Date2015-08-16 23:31 +0000
Subjectreturn types from library api wrappers
Message-ID<mailman.47.1439768058.4764.python-list@python.org>
What's the accepted practice for return types from a c based API
Python wrapper? I have many methods which return generators
which yield potentially many fields per iteration. In lower level
languages we would yield a struct with readonly fields.

The existing implementation returns a dict which I am not fond of.
I'd rather return an object with properties, however all the guys
who use this api use IDE's and want the type hinting. So, options I
can think of are to create a load of classes and returning instances
of them, return named tuples or even terser would be to return
something like:

def api_method(foo, bar):
    """produces two return values, x and y."""
    ...
    return type('MyResult', (), {'__slots__': [], 'some_field': x, 'another_field': y})()

The last option humorously returns readonly fields.

However, the latter two don't help much for type hinting unless
someone knows something I don't about doc strings? As a Python user,
what is preferred?

Thanks,
jlc

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web