Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Dan Strohl Newsgroups: comp.lang.python Subject: RE: Question on keyword arguments Date: Thu, 18 Feb 2016 15:39:38 +0000 Lines: 116 Message-ID: References: <62136715E19142B6B318EA3A3DF04D40@OPTIPLEX990> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de Ihn+wKOs4OUu62U29kCrWwibNFqP6XI8AhfQ1fl/eqkA== 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; 'else:': 0.03; '"""': 0.05; 'subject:Question': 0.05; '__name__': 0.07; 'objects,': 0.07; 'cleaned': 0.09; 'collections': 0.09; 'iterate': 0.09; 'preferable': 0.09; 'thrown': 0.09; 'python': 0.10; 'thursday,': 0.13; 'times,': 0.13; 'def': 0.13; 'argument': 0.15; 'message-----': 0.15; 'result.': 0.15; 'variables': 0.15; "'__main__':": 0.16; '2016': 0.16; 'expects': 0.16; 'forth.': 0.16; 'function.)': 0.16; 'instead:': 0.16; 'loops': 0.16; 'programmer)': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'skip:[ 50': 0.16; 'switch.': 0.16; 'to:addr:web.de': 0.16; 'utility,': 0.16; 'whatever,': 0.16; 'wrote:': 0.16; 'case.': 0.18; 'developer': 0.20; 'to:name:python-list@python.org': 0.20; 'to:2**1': 0.21; 'arguments': 0.22; 'minor': 0.22; 'object.': 0.22; 'pass': 0.22; '(where': 0.23; 'passing': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'requests': 0.25; 'module': 0.25; '(which': 0.26; 'coding': 0.27; 'define': 0.27; 'mostly': 0.27; 'separate': 0.27; 'question': 0.27; 'converting': 0.27; 'data,': 0.27; 'object,': 0.27; 'correct': 0.28; 'function': 0.28; 'dan': 0.29; 'end,': 0.29; 'other,': 0.29; 'received:192.168.10': 0.29; 'convert': 0.29; 'objects': 0.29; 'url:mailman': 0.30; 'creating': 0.30; 'normally': 0.30; 'probably': 0.31; 'another': 0.32; 'skip:_ 10': 0.32; 'up.': 0.32; 'returned': 0.32; 'though,': 0.32; 'class': 0.33; 'url:python': 0.33; 'common': 0.33; 'usually': 0.33; 'jump': 0.33; 'skip:- 10': 0.34; 'url:listinfo': 0.34; "skip:' 20": 0.34; 'handle': 0.34; 'that,': 0.34; 'list': 0.34; 'so,': 0.35; 'sent:': 0.35; 'text': 0.35; 'done': 0.35; 'easiest': 0.35; 'returning': 0.35; 'something': 0.35; 'subject:': 0.35; 'but': 0.36; 'too': 0.36; 'list,': 0.36; 'should': 0.36; 'there': 0.36; 'url:org': 0.36; 'email addr:python.org': 0.36; 'keyword': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'being': 0.37; 'expect': 0.37; 'method': 0.37; 'setting': 0.37; 'desired': 0.37; 'charset:us-ascii': 0.37; 'list.': 0.37; 'skip:p 20': 0.38; 'test': 0.39; 'data': 0.39; 'does': 0.39; 'enough': 0.39; 'from:': 0.39; 'received:192': 0.39; 'url:mail': 0.40; 'to:addr:python.org': 0.40; 'some': 0.40; 'easy': 0.60; 'forget': 0.60; 'high': 0.60; 'your': 0.60; 'decision': 0.61; 'default': 0.61; 'back': 0.62; 'more': 0.63; 'different': 0.63; 'benefit': 0.66; 'response.': 0.66; 'python- list': 0.66; 'email name:python-list': 0.67; 'act': 0.67; 'treat': 0.72; 'hand': 0.82; 'back?': 0.84; 'hanging': 0.84; 'otten': 0.84; 'received:192.168.15': 0.84; 'good,': 0.93; 'approached': 0.95 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=f5.com; i=@f5.com; q=dns/txt; s=seattle; t=1455810050; x=1487346050; h=from:to:subject:date:message-id:references:in-reply-to: content-transfer-encoding:mime-version; bh=GmLj0GW7oJS0vnXQIhmpUWFZQWWu7udRuWCoiaKmr60=; b=CjoRA4kYjISIKcQbAwfaDqZmo05uDQcWDwsLDW+pFzMNa87d2qL1U2QL XfkEZH/2EDZZ8yWTut5OThD0TMWGbt2GaQ6ckeMxRP44ZvcX17XIXQcXV ayttk908mtlL8eYTZa12+t3p+bNNDZinyDszA5vKh8rIfGNxUJoU5hr6T s=; X-IronPort-AV: E=Sophos;i="5.22,465,1449532800"; d="scan'208";a="203204966" Thread-Topic: Question on keyword arguments Thread-Index: AQHRalZw8Vry/OGpuU6zQYVjWAdBQ58x33/kgAAJ84A= In-Reply-To: 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: [192.168.15.239] X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21rc2 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:103123 I have approached this in a few different ways, depending on the use case. I have done the "return_type=3Dlist" before, though I don't really like it.= .. it's too easy to forget or mistype the exact name used for the switch. = If you do this, I also recommend setting some class or module variables to = the text strings and pass those back and forth. I have also returned a different type depending on what is being requested.= .. so, if the function user requests data that has multiple objects, return= a list, if they request something that has just one object, return that ob= ject. This also has potential problems if the number of returned objects i= s not consistently predictable, you would have to always test your returned= object to see how to handle it. I like the other suggestions thrown out as well, (having multiple methods a= nd having one method and a converting function).. They are good, easy to re= ad and use methods as long as you know at design time what type of object y= ou want (which is probably the norm). Another approach that I like (where it makes sense) is to return another (c= ustom) object that can act like either... that way, you have the content in= hand and can use it either way. This approach works well when there is a = high overhead in creating the data, that you don't want to have to do again= , and you don't mind having a few more objects hanging around until cleaned= up. So, define a return object like: from collections import UserList class TestResponse(UserList): def __str__(self): return '\0xfe%s' % '\0xfe'.join(self.data) ...and return that object. that way, you can iterate over it, treat it as a list, or whatever, or if y= ou do print(str(TestResponse)), you get your r'0xfeONE\0exfeTWO\0xfeTHREE' In the end, your decision should be made based on: 1. What would the developer using this EXPECT to get back? (if the user no= rmally expects one or the other, do that, if this is for a "public module",= I would expect the list is more likely the common response. This list is = probably not the right place for that question though, unless this is a ver= y general utility, you want to ask the people coding in the domain that wou= ld mostly be using your function.) 2. What is the most likely response type? (if it is common to get it back = different ways, or use it multiple times, this is probably mostly the same = as #1, but can be different) 3. What will be the easiest to read and document? Returning a list is eas= y to read and expectable, but not if you have to then jump through some loo= ps to convert it to something else immediately. 4. Is there a large enough benefit in processing that you want to be able = to use it multiple ways without having to regenerate the list. (note that = this is the last criteria, unless performance is really critical, readabili= ty and predictability is normally much more important than what is probably= minor performance tuning.) Dan Strohl > -----Original Message----- > From: Python-list [mailto:python-list-bounces+d.strohl=3Df5.com@python.or= g] On > Behalf Of Peter Otten > Sent: Thursday, February 18, 2016 6:37 AM > To: python-list@python.org > Subject: Re: Question on keyword arguments >=20 > grsmith@atlanticbb.net wrote: >=20 > > Would this be the correct way to return a list as a default result. >=20 > If it does what you want it to do it is "correct" as in "complies with > specification". >=20 > > Also, would the list be the preferable result (to a python programmer) = ? >=20 > A list as a return value is OK as is any other object, however, >=20 > > def test(command, return_type=3D'LIST'): >=20 > passing the desired result type as an argument is usually not a good idea= . >=20 > > """ Go to database and return data""" > > if return_type =3D=3D 'LIST': > > result =3D ['ONE', 'TWO', 'THREE'] > > else: > > result =3D r'0xfeONE\0exfeTWO\0xfeTHREE' > > return result > > > > if __name__ =3D=3D '__main__': > > print(test('cmd')) > > print(test('cmd', 'LIST')) > > print(test('cmd', None)) > > print(test('cmd', 'string')) >=20 > Make it separate functions instead: >=20 > def test(command): > return ["ONE", "TWO", "THREE"] >=20 > def test_as_string(command, sep=3D","): > return sep.join(test(command)) >=20 > if __name__ =3D=3D '__main__': > print(test("cmd")) > print(test_as_string("cmd")) >=20 >=20 > -- > https://mail.python.org/mailman/listinfo/python-list