Path: csiph.com!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!bcyclone03.am1.xlned.com!bcyclone03.am1.xlned.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'api': 0.09; 'dict': 0.09; 'generators': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:library': 0.09; 'python': 0.10; 'jan': 0.11; 'fond': 0.16; 'iteration.': 0.16; 'properties,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject:api': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'of.': 0.18; '>>>': 0.20; '"",': 0.22; 'struct': 0.22; '(most': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'yield': 0.27; 'equivalent.': 0.29; 'code': 0.30; "i'd": 0.31; 'class': 0.33; 'traceback': 0.33; 'languages': 0.34; 'file': 0.34; 'gives': 0.35; 'level': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'guys': 0.38; 'skip:o 20': 0.38; 'subject:from': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'forget': 0.60; 'per': 0.62; 'believe': 0.66; 'potentially': 0.67; 'received:fios.verizon.net': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: return types from library api wrappers Date: Sun, 16 Aug 2015 22:23:39 -0400 References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-98-114-97-173.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1439778227 news.xs4all.nl 2955 [2001:888:2000:d::a6]:45591 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 4063 X-Received-Body-CRC: 4319765 Xref: csiph.com comp.lang.python:95420 On 8/16/2015 7:31 PM, Joseph L. Casale wrote: > 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. Current practice is a NamedTuple for python code or the C equivalent. I forget the C name, but I believe it is used by os.stat >>> os.stat('C:/programs') os.stat_result(st_mode=16895, st_ino=1970324837036820, st_dev=1816146727, st_nlink=1, st_uid=0, st_gid=0, st_size=4096, st_atime=1437490766, st_mtime=1437490766, st_ctime=1313612988) >>> s = os.stat('C:/programs') >>> s.st_atime 1437490766.6669185 >>> s.st_atime = 3 Traceback (most recent call last): File "", line 1, in s.st_atime = 3 AttributeError: readonly attribute > 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. I believe the above gives you both: custom class for type hinting and properties. -- Terry Jan Reedy