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


Groups > comp.lang.python > #99920 > unrolled thread

Re: Subclassing tuple and introspection

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2015-12-02 16:46 -0700
Last post2015-12-02 16:46 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Subclassing tuple and introspection Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-02 16:46 -0700

#99920 — Re: Subclassing tuple and introspection

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-12-02 16:46 -0700
SubjectRe: Subclassing tuple and introspection
Message-ID<mailman.150.1449100059.14615.python-list@python.org>
On Wed, Dec 2, 2015 at 4:32 PM, Joseph L. Casale
<jcasale@activenetwerx.com> 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.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web