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


Groups > comp.lang.python > #99920

Re: Subclassing tuple and introspection

From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: Subclassing tuple and introspection
Date 2015-12-02 16:46 -0700
Message-ID <mailman.150.1449100059.14615.python-list@python.org> (permalink)
References <177ebe2b7c6b4011b74e3f2364223df8@activenetwerx.com>

Show all headers | View raw


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.

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


Thread

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

csiph-web