return types from library api wrappers

Joseph L. Casale jcasale at activenetwerx.com
Mon Aug 17 01:31:30 CEST 2015


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


More information about the Python-list mailing list