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


Groups > comp.lang.python > #95418

return types from library api wrappers

Path csiph.com!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <jcasale@activenetwerx.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; '[],': 0.07; 'api': 0.09; 'dict': 0.09; 'generators': 0.09; 'subject:library': 0.09; 'python': 0.10; 'def': 0.13; '(),': 0.16; 'fond': 0.16; 'iteration.': 0.16; 'properties,': 0.16; 'received:172.18.0': 0.16; 'subject:api': 0.16; 'of.': 0.18; 'load': 0.20; 'to:name :python-list@python.org': 0.20; 'doc': 0.22; 'latter': 0.22; 'struct': 0.22; 'tuples': 0.22; 'thanks,': 0.24; 'yield': 0.27; "skip:' 10": 0.28; 'classes': 0.30; "i'd": 0.31; 'option': 0.31; 'knows': 0.32; 'options': 0.33; 'instances': 0.33; 'languages': 0.34; 'so,': 0.35; 'returning': 0.35; 'something': 0.35; 'level': 0.35; 'skip:{ 10': 0.36; 'to:addr:python-list': 0.36; 'two': 0.37; 'charset:us-ascii': 0.37; 'guys': 0.38; 'someone': 0.38; 'subject:from': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'per': 0.62; 'potentially': 0.67; 'user,': 0.67
X-Authority-Analysis v=2.1 cv=T55GNa+Q c=1 sm=1 tr=0 a=g3mLq75WYuDrh3Lt0JSDww==:117 a=g3mLq75WYuDrh3Lt0JSDww==:17 a=QC7fh9NfAAAA:8 a=dmF8UoGEAYUA:10 a=kj9zAlcOel0A:10 a=uRRa74qj2VoA:10 a=WJ114GedgskGJSITHSoA:9 a=CjuIK1q_8ugA:10
X-Spam-Checker-Version SpamAssassin 3.4.0 (2014-02-07) on mail.activenetwerx.com
X-Spam-Level
X-Spam-Status No, score=-1.7 required=5.0 tests=ALL_TRUSTED,RP_MATCHES_RCVD autolearn=ham autolearn_force=no version=3.4.0
From "Joseph L. Casale" <jcasale@activenetwerx.com>
To "python-list@python.org" <python-list@python.org>
Subject return types from library api wrappers
Thread-Topic return types from library api wrappers
Thread-Index AdDYLMgMzjLZAaGWQlyK96o3U5/i5w==
Date Sun, 16 Aug 2015 23:31:30 +0000
Accept-Language en-US
Content-Language en-US
X-MS-Has-Attach
X-MS-TNEF-Correlator
x-ms-exchange-transport-fromentityheader Hosted
x-originating-ip [172.18.0.209]
Content-Type text/plain; charset="us-ascii"
Content-Transfer-Encoding quoted-printable
MIME-Version 1.0
X-CMAE-Envelope MS4wfHsbT/ZEFbih9CwEYEruwEM2MR5zD67LGD3STK+qHuuUwEMrAwWBtQkwIr3cIVq+cPSB31kVolGKglZ3Divprn80F7UuPiBm2XIXxmG0+AuXFl0GTJVyj4keLf2GgOglfq7SNjmuhzXGrx5XO36eXw3u21NBhM+oxxN/eF3y6jMVn43OYtf7HIvX1GR5r06NXg==
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.47.1439768058.4764.python-list@python.org> (permalink)
Lines 26
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1439768058 news.xs4all.nl 2860 [2001:888:2000:d::a6]:50954
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:95418

Show key headers only | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web