Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: Subclassing tuple and introspection Date: Wed, 2 Dec 2015 16:46:57 -0700 Lines: 27 Message-ID: References: <177ebe2b7c6b4011b74e3f2364223df8@activenetwerx.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de oQOsrOUZeUPbfDW/b7ex/Aaw2HbYoEeWFKoDVf9YUiug== 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; 'received:209.85.223': 0.03; 'heavily': 0.04; 'resulting': 0.04; 'desired.': 0.07; 'ide': 0.09; 'subclass': 0.09; 'tuple': 0.09; 'example:': 0.10; 'python': 0.10; 'wed,': 0.15; 'col': 0.16; 'naming': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subclassing': 0.16; 'terse': 0.16; 'wrote:': 0.16; 'element': 0.18; '2015': 0.20; 'extension': 0.20; 'accommodate': 0.22; 'class,': 0.22; 'subject:skip:i 10': 0.22; 'seems': 0.23; 'dec': 0.23; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'used,': 0.27; 'least': 0.27; 'message- id:@mail.gmail.com': 0.27; 'this.': 0.28; 'pep': 0.29; 'facility': 0.33; 'this?': 0.34; 'add': 0.34; 'received:google.com': 0.35; 'instance': 0.35; 'item': 0.35; 'supports': 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'method': 0.37; 'received:209': 0.38; 'to:addr:python.org': 0.40; 'collection': 0.60; 'further': 0.62; 'providing': 0.62; 'sounds': 0.76; 'collection,': 0.84; 'to:name:python': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=ls7eFWN1pXn55J2KImodNy+Bkzn1+GujjBYbVoO7Rcc=; b=TsDRw/Rv9grErFoNJ8JQsiAUXlNh4Ou2u/wPclcfoYJm5JJohuBBIbfstzBif6KBuV opwoVKC1fPYOGE0jtUkFrG1ZyCEBRQKpsvbaLtm5fimSiHfv9TpGf4OJzCNXZMy6Vm5p k+hv0rjjAiPer062aDhl0rHLh7wHkR82ZEpbM6IF8yyRr8luc6O6aCnZ1bt5P6BMODzt oOKSiKKuKryONO8B0Vx7opD7tb1H32KtZKNvBUdDuFdwTHsg3Yo5hHGvaHIwTNVhm6e/ cKZ4IPCgA9AtH33sekhP8Lyo8wgnN9jJvJ2ATIGxSc2JHo8Y6ZTTLl7yg8ZASwMLVh3Y 5n9A== X-Received: by 10.107.137.226 with SMTP id t95mr6049249ioi.188.1449100056941; Wed, 02 Dec 2015 15:47:36 -0800 (PST) In-Reply-To: <177ebe2b7c6b4011b74e3f2364223df8@activenetwerx.com> 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: , Xref: csiph.com comp.lang.python:99920 On Wed, Dec 2, 2015 at 4:32 PM, Joseph L. Casale wrote: > I need to return a collection of various types, since python doesn't > have the terse facility of extension methods like C#, subclassing tuple > and adding a method seems like a terse way to accommodate this. If you're not already familiar with collections.namedtuple, have a look at it, as it sounds like just naming the fields may be all that you need. You can also subclass it further to add methods if desired. > However, if the method returns one element of the collection, how can > one enable introspection for users of IDE's that the resulting reference > is of type A, and therefor has A's fields? > > For example: > col = (Class(..), Class(...)) > item = col[0] > > Introspection will now enumerate item as an instance of Class, providing > its fields. The subclass of tuple breaks this. > > Is there a better way to do this? I think that's going to depend heavily on the specific IDE being used, but for general usage you might consider using PEP 484 type annotations; I know that at least PyCharm supports their use for type hinting.