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


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

A bug?

Started by"umatrp@gmail.com" <trsexgl@gmail.com>
First post2014-10-27 09:01 -0700
Last post2014-10-30 10:41 -0700
Articles 20 on this page of 28 — 9 participants

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


Contents

  A bug? "umatrp@gmail.com" <trsexgl@gmail.com> - 2014-10-27 09:01 -0700
    Re: A bug? Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2014-10-27 17:17 +0100
    Re: A bug? Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-27 10:26 -0600
    Re: A bug? Denis McMahon <denismfmcmahon@gmail.com> - 2014-10-28 00:36 +0000
      Re: A bug? Rustom Mody <rustompmody@gmail.com> - 2014-10-27 18:12 -0700
        Re: A bug? Chris Angelico <rosuav@gmail.com> - 2014-10-28 12:25 +1100
          Re: A bug? Rustom Mody <rustompmody@gmail.com> - 2014-10-27 19:13 -0700
      Re: A bug? Joshua Landau <joshua@landau.ws> - 2014-10-28 01:29 +0000
        Re: A bug? Denis McMahon <denismfmcmahon@gmail.com> - 2014-10-29 02:17 +0000
          What for -- for?  (was A bug?) Rustom Mody <rustompmody@gmail.com> - 2014-10-28 20:22 -0700
            Re: What for -- for? (was A bug?) Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-10-28 23:16 -0500
            Re: What for -- for? (was A bug?) Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-10-28 23:23 -0500
              Re: What for -- for? (was A bug?) Rustom Mody <rustompmody@gmail.com> - 2014-10-28 21:40 -0700
                Re: What for -- for? (was A bug?) Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-10-28 23:59 -0500
                  Re: What for -- for? (was A bug?) Rustom Mody <rustompmody@gmail.com> - 2014-10-28 22:15 -0700
                    Re: What for -- for? (was A bug?) Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-10-29 00:39 -0500
                      Re: What for -- for? (was A bug?) Rustom Mody <rustompmody@gmail.com> - 2014-10-28 23:11 -0700
                        Re: What for -- for? (was A bug?) Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-10-29 01:18 -0500
                          Finding way around ABCs (was What for -- for? (was A bug?)) Rustom Mody <rustompmody@gmail.com> - 2014-10-30 10:01 -0700
                            Re: Finding way around ABCs (was What for -- for? (was A bug?)) Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-30 11:09 -0600
                              Re: Finding way around ABCs (was What for -- for? (was A bug?)) Rustom Mody <rustompmody@gmail.com> - 2014-10-30 10:16 -0700
                            Re: Finding way around ABCs (was What for -- for? (was A bug?)) Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-30 11:11 -0600
            Re: What for -- for? (was A bug?) Ben Finney <ben+python@benfinney.id.au> - 2014-10-29 16:27 +1100
            Re: What for -- for? (was A bug?) Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-10-29 00:42 -0500
              Re: What for -- for? (was A bug?) Rustom Mody <rustompmody@gmail.com> - 2014-10-28 22:57 -0700
            Re: What for -- for? (was A bug?) Ben Finney <ben+python@benfinney.id.au> - 2014-10-29 17:02 +1100
            Re: What for -- for? (was A bug?) Joshua Landau <joshua@landau.ws> - 2014-10-30 17:21 +0000
              Re: What for -- for? (was A bug?) Rustom Mody <rustompmody@gmail.com> - 2014-10-30 10:41 -0700

Page 1 of 2  [1] 2  Next page →


#79794 — A bug?

From"umatrp@gmail.com" <trsexgl@gmail.com>
Date2014-10-27 09:01 -0700
SubjectA bug?
Message-ID<f491d969-7405-4017-b9c4-e1cb52a70280@googlegroups.com>
I use python 3.4.0 version. In the course of developing / running a python program, I have encountered a problem. I have reproduced below a simple program to bring it out.
 

>>> d = [[0]*3]*4
>>> dd = [1,2,3,4,5,6,7,8,9,10,11,12]
>>> for i in range(4):
...   for j in range(3): d[i][j] = dd[i*3+j]
... 
>>> d
[[10, 11, 12], [10, 11, 12], [10, 11, 12], [10, 11, 12]]
>>> 
d is not transferred to dd as expected?
Of course I can use 'append' & do my job (less elegantly though).

[toc] | [next] | [standalone]


#79795

FromWolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
Date2014-10-27 17:17 +0100
Message-ID<mailman.15238.1414426664.18130.python-list@python.org>
In reply to#79794
On 10/27/2014 05:01 PM, umatrp@gmail.com wrote:
> I use python 3.4.0 version. In the course of developing / running a python program, I have encountered a problem. I have reproduced below a simple program to bring it out.
>
>
>>>> d = [[0]*3]*4
>>>> dd = [1,2,3,4,5,6,7,8,9,10,11,12]
>>>> for i in range(4):
> ...   for j in range(3): d[i][j] = dd[i*3+j]
> ...
>>>> d
> [[10, 11, 12], [10, 11, 12], [10, 11, 12], [10, 11, 12]]
>>>>
> d is not transferred to dd as expected?
> Of course I can use 'append' & do my job (less elegantly though).
>

See 
https://docs.python.org/3/library/stdtypes.html?highlight=list#common-sequence-operations 
under Note 2 .

Also asked and answered multiple times at stackoverflow, e.g., 
http://stackoverflow.com/questions/6688223/


[toc] | [prev] | [next] | [standalone]


#79796

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-10-27 10:26 -0600
Message-ID<mailman.15239.1414427209.18130.python-list@python.org>
In reply to#79794
On Mon, Oct 27, 2014 at 10:17 AM, Wolfgang Maier
<wolfgang.maier@biologie.uni-freiburg.de> wrote:
> See
> https://docs.python.org/3/library/stdtypes.html?highlight=list#common-sequence-operations
> under Note 2 .
>
> Also asked and answered multiple times at stackoverflow, e.g.,
> http://stackoverflow.com/questions/6688223/

Also see https://docs.python.org/3/faq/programming.html#how-do-i-create-a-multidimensional-list
(probably a better resource than that footnote).

[toc] | [prev] | [next] | [standalone]


#79813

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-10-28 00:36 +0000
Message-ID<m2mof5$hvh$2@dont-email.me>
In reply to#79794
On Mon, 27 Oct 2014 09:01:57 -0700, umatrp@gmail.com wrote:

> I use python 3.4.0 version. In the course of developing / running a
> python program, I have encountered a problem. I have reproduced below a
> simple program to bring it out.
>  
>  
>>>> d = [[0]*3]*4 dd = [1,2,3,4,5,6,7,8,9,10,11,12]
>>>> for i in range(4):
> ...   for j in range(3): d[i][j] = dd[i*3+j]
> ...
>>>> d
> [[10, 11, 12], [10, 11, 12], [10, 11, 12], [10, 11, 12]]
>>>> 
> d is not transferred to dd as expected?
> Of course I can use 'append' & do my job (less elegantly though).

Not sure if this is elegant or not:

d = [[list(range(1,13))[i*3+j] for j in range(3)] for i in range(4)]

but it seems to be a one-line solution for what you're trying to do.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#79814

FromRustom Mody <rustompmody@gmail.com>
Date2014-10-27 18:12 -0700
Message-ID<b675d41f-b690-4bcc-a0b7-3310f61f39ce@googlegroups.com>
In reply to#79813
On Tuesday, October 28, 2014 6:07:14 AM UTC+5:30, Denis McMahon wrote:
> On Mon, 27 Oct 2014 09:01:57 -0700, umatrp wrote:
> > I use python 3.4.0 version. In the course of developing / running a
> > python program, I have encountered a problem. I have reproduced below a
> > simple program to bring it out.
> >>>> d = [[0]*3]*4 dd = [1,2,3,4,5,6,7,8,9,10,11,12]
> >>>> for i in range(4):
> > ...   for j in range(3): d[i][j] = dd[i*3+j]
> > ...
> >>>> d
> > [[10, 11, 12], [10, 11, 12], [10, 11, 12], [10, 11, 12]]
> > d is not transferred to dd as expected?
> > Of course I can use 'append' & do my job (less elegantly though).
> Not sure if this is elegant or not:
> d = [[list(range(1,13))[i*3+j] for j in range(3)] for i in range(4)]
> but it seems to be a one-line solution for what you're trying to do.

Neat

More generally for d being a 2-D reshape of dd (which may be anything
as long as the size matches)

>>> dd = [1,2,3,4,5,6,7,8,9,10,11,12]
>>> d=[[dd[i*3+j] for j in range(3)] for i in range(4)]
>>> d
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]

[toc] | [prev] | [next] | [standalone]


#79815

FromChris Angelico <rosuav@gmail.com>
Date2014-10-28 12:25 +1100
Message-ID<mailman.15247.1414459556.18130.python-list@python.org>
In reply to#79814
On Tue, Oct 28, 2014 at 12:12 PM, Rustom Mody <rustompmody@gmail.com> wrote:
> More generally for d being a 2-D reshape of dd (which may be anything
> as long as the size matches)
>
>>>> dd = [1,2,3,4,5,6,7,8,9,10,11,12]
>>>> d=[[dd[i*3+j] for j in range(3)] for i in range(4)]
>>>> d
> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]

The inner comprehension should surely be a slice:

>>> [dd[i*3:i*3+3] for i in range(4)]
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]

ChrisA

[toc] | [prev] | [next] | [standalone]


#79817

FromRustom Mody <rustompmody@gmail.com>
Date2014-10-27 19:13 -0700
Message-ID<90cc19d7-a888-49aa-8005-15903e031f4a@googlegroups.com>
In reply to#79815
On Tuesday, October 28, 2014 6:56:10 AM UTC+5:30, Chris Angelico wrote:
> On Tue, Oct 28, 2014 at 12:12 PM, Rustom Mody  wrote:
> > More generally for d being a 2-D reshape of dd (which may be anything
> > as long as the size matches)
> >
> >>>> dd = [1,2,3,4,5,6,7,8,9,10,11,12]
> >>>> d=[[dd[i*3+j] for j in range(3)] for i in range(4)]
> >>>> d
> > [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
> 
> The inner comprehension should surely be a slice:
> 
> >>> [dd[i*3:i*3+3] for i in range(4)]
> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]

Sweet!
[Something for my class today]

Looks even better this way:

>>> dd = range(1,13)
>>> [dd[i*3:i*3+3] for i in range(4)]
[range(1, 4), range(4, 7), range(7, 10), range(10, 13)]
>>> 

In the same vein a transpose:
>>> [list(dd[i::3]) for i in range(3)]
[[0, 3, 6, 9], [1, 4, 7, 10], [2, 5, 8, 11]]

[Not sure why both are 3; no 4's...]

[toc] | [prev] | [next] | [standalone]


#79816

FromJoshua Landau <joshua@landau.ws>
Date2014-10-28 01:29 +0000
Message-ID<mailman.15248.1414459811.18130.python-list@python.org>
In reply to#79813
On 28 October 2014 00:36, Denis McMahon <denismfmcmahon@gmail.com> wrote:
>
> d = [[list(range(1,13))[i*3+j] for j in range(3)] for i in range(4)]

A quick note. Ranges (even 2.7's xrange) are all indexable. The cast
to a list isn't needed.

[toc] | [prev] | [next] | [standalone]


#79869

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-10-29 02:17 +0000
Message-ID<m2pinn$hum$2@dont-email.me>
In reply to#79816
On Tue, 28 Oct 2014 01:29:28 +0000, Joshua Landau wrote:

> On 28 October 2014 00:36, Denis McMahon <denismfmcmahon@gmail.com>
> wrote:
>>
>> d = [[list(range(1,13))[i*3+j] for j in range(3)] for i in range(4)]
> 
> A quick note. Ranges (even 2.7's xrange) are all indexable. The cast to
> a list isn't needed.

Until you apply Chris' slicing trick, and then:

>>> [list(range(1,13))[i*3:i*3+3] for i in range(4)]
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
>>> [range(1,13)[i*3:i*3+3] for i in range(4)]
[range(1, 4), range(4, 7), range(7, 10), range(10, 13)]
>>> 

Depends how important it is that you get a list and not a range object ;)

(Thinking back to a recent discussion where someone was taking str(range
(x)) and not getting the expected results)

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#79873 — What for -- for? (was A bug?)

FromRustom Mody <rustompmody@gmail.com>
Date2014-10-28 20:22 -0700
SubjectWhat for -- for? (was A bug?)
Message-ID<94e3143a-c9ab-433d-8b3d-6ab296de14d9@googlegroups.com>
In reply to#79869
On Wednesday, October 29, 2014 7:47:47 AM UTC+5:30, Denis McMahon wrote:
> On Tue, 28 Oct 2014 01:29:28 +0000, Joshua Landau wrote:
> 
> > On 28 October 2014 00:36, Denis McMahon wrote:
> >>
> >> d = [[list(range(1,13))[i*3+j] for j in range(3)] for i in range(4)]
> > 
> > A quick note. Ranges (even 2.7's xrange) are all indexable. The cast to
> > a list isn't needed.
> 
> Until you apply Chris' slicing trick, and then:
> 
> >>> [list(range(1,13))[i*3:i*3+3] for i in range(4)]
> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
> >>> [range(1,13)[i*3:i*3+3] for i in range(4)]
> [range(1, 4), range(4, 7), range(7, 10), range(10, 13)]
> >>> 
> 
> Depends how important it is that you get a list and not a range object ;)

Heh!

Strange that you say this in this context!
Yesterday I was trying to introduce python to some senior computer scientists.

Tried showing a comprehension-based dir-walker vs a for-loop based one:

def dw(p):
   if isfile(p):
      return [p]
   else:
      return [p] + [c for f in listdir(p) for c in dw(p+'/'+f)]


def dw(p):
    if isfile(p):
        return [p]
    else:
        ls = [p]
        for f in listdir(p):
            ls = ls+[f]
            for c in dw(p+'/'+f):
                ls = ls+[c]
        return ls

Comment to me : "Well this is neat and compact, but it does not add 
anything fundamental (over usual index based for-loops)"

I tried to say that 'for' over general sequences is quite different
and significantly more powerful than C/Pascal for over indexes +
explicit indexing.

In particular the fact that in python-3:
>>> range(1,10)
range(1, 10)

and not like python2's

>>> range(1,10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]

seemed to convincingly show that python's range is just 
'notional' like Pascal's subrange types and not an actual sequence.

That
>>> range(10)+range(10)

works in python 2 but not 3

does not help my case at all!

I'd be interested in thoughts on this -- is a range a 'real' or a 'notional'
sequence?

Related point: "A range is a sequence"
How to prove this introspectively?

ie if I do:

>>> type([]).__mro__
(<class 'list'>, <class 'object'>)
>>> type(range(10)).__mro__
(<class 'range'>, <class 'object'>)
>>> 

How to see that list and range are both sequences?
Or more generally how to to introspectively discover (ie not by reading docs!!)
the abstract base classes -- eg sequence, iterable etc -- for an arbitrary
object?

[toc] | [prev] | [next] | [standalone]


#79876 — Re: What for -- for? (was A bug?)

FromZachary Ware <zachary.ware+pylist@gmail.com>
Date2014-10-28 23:16 -0500
SubjectRe: What for -- for? (was A bug?)
Message-ID<mailman.15281.1414556215.18130.python-list@python.org>
In reply to#79873
On Tue, Oct 28, 2014 at 10:22 PM, Rustom Mody <rustompmody@gmail.com> wrote:
> How to see that list and range are both sequences?
> Or more generally how to to introspectively discover (ie not by reading docs!!)
> the abstract base classes -- eg sequence, iterable etc -- for an arbitrary
> object?

# Python 2/3 compatible.  Combine with pprint for nice interactive output
try:
    from collections import abc
except ImportError:
    import collections as abc

def get_abc_map(cls):
   return {n: issubclass(cls, getattr(abc, n)) for n in dir(abc) if
n[0].isupper()}

-- 
Zach

[toc] | [prev] | [next] | [standalone]


#79877 — Re: What for -- for? (was A bug?)

FromZachary Ware <zachary.ware+pylist@gmail.com>
Date2014-10-28 23:23 -0500
SubjectRe: What for -- for? (was A bug?)
Message-ID<mailman.15282.1414556609.18130.python-list@python.org>
In reply to#79873
On Tue, Oct 28, 2014 at 11:16 PM, Zachary Ware
<zachary.ware+pylist@gmail.com> wrote:
> def get_abc_map(cls):
>    return {n: issubclass(cls, getattr(abc, n)) for n in dir(abc) if
> n[0].isupper()}

Of course, Gmail decided to wrap my long line for me.  In case it's
not obvious, that should be a single line.

-- 
Zach

[toc] | [prev] | [next] | [standalone]


#79878 — Re: What for -- for? (was A bug?)

FromRustom Mody <rustompmody@gmail.com>
Date2014-10-28 21:40 -0700
SubjectRe: What for -- for? (was A bug?)
Message-ID<593c0099-770f-48cd-b7ba-22154b00f130@googlegroups.com>
In reply to#79877
On Wednesday, October 29, 2014 9:53:46 AM UTC+5:30, Zachary Ware wrote:
> On Tue, Oct 28, 2014 at 11:16 PM, Zachary Ware wrote:
> > def get_abc_map(cls):
> >    return {n: issubclass(cls, getattr(abc, n)) for n in dir(abc) if
> > n[0].isupper()}
> 
> Of course, Gmail decided to wrap my long line for me.  In case it's
> not obvious, that should be a single line.

Thanks

Wrapping -- no problem.
But the isupper looks like black-magic :-)

And removing the ' ... if n[0].isupper()'
breaks the code

[toc] | [prev] | [next] | [standalone]


#79880 — Re: What for -- for? (was A bug?)

FromZachary Ware <zachary.ware+pylist@gmail.com>
Date2014-10-28 23:59 -0500
SubjectRe: What for -- for? (was A bug?)
Message-ID<mailman.15283.1414558776.18130.python-list@python.org>
In reply to#79878
On Tue, Oct 28, 2014 at 11:40 PM, Rustom Mody <rustompmody@gmail.com> wrote:
> On Wednesday, October 29, 2014 9:53:46 AM UTC+5:30, Zachary Ware wrote:
>> On Tue, Oct 28, 2014 at 11:16 PM, Zachary Ware wrote:
>> > def get_abc_map(cls):
>> >    return {n: issubclass(cls, getattr(abc, n)) for n in dir(abc) if
>> > n[0].isupper()}
>>
>> Of course, Gmail decided to wrap my long line for me.  In case it's
>> not obvious, that should be a single line.
>
> Thanks
>
> Wrapping -- no problem.
> But the isupper looks like black-magic :-)
>
> And removing the ' ... if n[0].isupper()'
> breaks the code

It's a terrible hack for 2/3 compatibility; in 3 I'd do "if not
n.startswith('__')" since the Python 3 collections.abc module only
contains ABCs (and the standard dunder names).  Python 2 has all the
ABCs mixed into the toplevel collections namespace with some other
non-ABCs, but it happens that all the ABCs are capitalized, while the
non-ABCs are not.  The ABCs are imported into collections from
_abcoll, but _abcoll also some uncapitalized non-ABCs as well.

For somewhat greyer magic, do 'from abc import ABCMeta' and filter the
collections<.abc> namespace with isinstance(obj, ABCMeta).  I just
used the above because it's short and sweet and keeps the name handy
:)

-- 
Zach

[toc] | [prev] | [next] | [standalone]


#79883 — Re: What for -- for? (was A bug?)

FromRustom Mody <rustompmody@gmail.com>
Date2014-10-28 22:15 -0700
SubjectRe: What for -- for? (was A bug?)
Message-ID<ed95d533-c4db-4081-bd32-61a93aaf797c@googlegroups.com>
In reply to#79880
On Wednesday, October 29, 2014 10:29:48 AM UTC+5:30, Zachary Ware wrote:
> On Tue, Oct 28, 2014 at 11:40 PM, Rustom Mody  wrote:
> > On Wednesday, October 29, 2014 9:53:46 AM UTC+5:30, Zachary Ware wrote:
> >> On Tue, Oct 28, 2014 at 11:16 PM, Zachary Ware wrote:
> >> > def get_abc_map(cls):
> >> >    return {n: issubclass(cls, getattr(abc, n)) for n in dir(abc) if
> >> > n[0].isupper()}
> >>
> >> Of course, Gmail decided to wrap my long line for me.  In case it's
> >> not obvious, that should be a single line.
> >
> > Thanks
> >
> > Wrapping -- no problem.
> > But the isupper looks like black-magic :-)
> >
> > And removing the ' ... if n[0].isupper()'
> > breaks the code
> 
> It's a terrible hack for 2/3 compatibility; in 3 I'd do "if not
> n.startswith('__')" since the Python 3 collections.abc module only
> contains ABCs (and the standard dunder names).  Python 2 has all the
> ABCs mixed into the toplevel collections namespace with some other
> non-ABCs, but it happens that all the ABCs are capitalized, while the
> non-ABCs are not.  The ABCs are imported into collections from
> _abcoll, but _abcoll also some uncapitalized non-ABCs as well.
> 
> For somewhat greyer magic, do 'from abc import ABCMeta' and filter the
> collections<.abc> namespace with isinstance(obj, ABCMeta).  I just
> used the above because it's short and sweet and keeps the name handy
> :)

Ha!
Thanks

Maybe nicer to filter out the false's with a filter-false thus??

def ff(d): return [n for n in d if d[n]]

[toc] | [prev] | [next] | [standalone]


#79886 — Re: What for -- for? (was A bug?)

FromZachary Ware <zachary.ware+pylist@gmail.com>
Date2014-10-29 00:39 -0500
SubjectRe: What for -- for? (was A bug?)
Message-ID<mailman.15285.1414561194.18130.python-list@python.org>
In reply to#79883
On Wed, Oct 29, 2014 at 12:15 AM, Rustom Mody <rustompmody@gmail.com> wrote:
> Maybe nicer to filter out the false's with a filter-false thus??
>
> def ff(d): return [n for n in d if d[n]]

Sure.  Or, combining things:

try:
    from collections import abc
except ImportError:
    import collections as abc
from abc import ABCMeta

abcs = [o for o in vars(abc).values() if isinstance(o, ABCMeta)]

def get_abcs(cls):
    return [abc for abc in abcs if issubclass(cls, abc)]

def get_abc_names(cls):
    return [abc.__name__ for abc in get_abcs(cls)]



Of course, that's 3 (progressively shorter) loops to get the names of
the ABCs of a class compared to 1 (fairly short in the first place)
loop for a map of relationships to all available ABCs, but optimizing
such a toy as this would just be an exercise in futility :)

-- 
Zach

[toc] | [prev] | [next] | [standalone]


#79893 — Re: What for -- for? (was A bug?)

FromRustom Mody <rustompmody@gmail.com>
Date2014-10-28 23:11 -0700
SubjectRe: What for -- for? (was A bug?)
Message-ID<525eb5e7-c221-4336-8c9a-aafb4c545f51@googlegroups.com>
In reply to#79886
On Wednesday, October 29, 2014 11:10:06 AM UTC+5:30, Zachary Ware wrote:
> On Wed, Oct 29, 2014 at 12:15 AM, Rustom Mody wrote:
> > Maybe nicer to filter out the false's with a filter-false thus??
> >
> > def ff(d): return [n for n in d if d[n]]
> 
> Sure.  Or, combining things:
> 
> try:
>     from collections import abc
> except ImportError:
>     import collections as abc
> from abc import ABCMeta
> 
> abcs = [o for o in vars(abc).values() if isinstance(o, ABCMeta)]
> 
> def get_abcs(cls):
>     return [abc for abc in abcs if issubclass(cls, abc)]
> 
> def get_abc_names(cls):
>     return [abc.__name__ for abc in get_abcs(cls)]
> 
> 
> 
> Of course, that's 3 (progressively shorter) loops to get the names of
> the ABCs of a class compared to 1 (fairly short in the first place)
> loop for a map of relationships to all available ABCs, but optimizing
> such a toy as this would just be an exercise in futility :)

Not so.

The charm of introspection is that the introspection
itself can be introspected.
For that to be convincing there needs to be a good combo
of clarity and succinctness.  In particular why not reduce
the two functions to one?

def get_abc_names(cls):
    return [abc.__name__ for abc in abcs if issubclass(cls,abc)]

[toc] | [prev] | [next] | [standalone]


#79896 — Re: What for -- for? (was A bug?)

FromZachary Ware <zachary.ware+pylist@gmail.com>
Date2014-10-29 01:18 -0500
SubjectRe: What for -- for? (was A bug?)
Message-ID<mailman.15288.1414563555.18130.python-list@python.org>
In reply to#79893
On Wed, Oct 29, 2014 at 1:11 AM, Rustom Mody <rustompmody@gmail.com> wrote:
> On Wednesday, October 29, 2014 11:10:06 AM UTC+5:30, Zachary Ware wrote:
>> Of course, that's 3 (progressively shorter) loops to get the names of
>> the ABCs of a class compared to 1 (fairly short in the first place)
>> loop for a map of relationships to all available ABCs, but optimizing
>> such a toy as this would just be an exercise in futility :)
>
> Not so.
>
> The charm of introspection is that the introspection
> itself can be introspected.
> For that to be convincing there needs to be a good combo
> of clarity and succinctness.  In particular why not reduce
> the two functions to one?
>
> def get_abc_names(cls):
>     return [abc.__name__ for abc in abcs if issubclass(cls,abc)]

Well, it depends on what you actually want, the spec has been a bit fuzzy ;)

-- 
Zach

[toc] | [prev] | [next] | [standalone]


#79957 — Finding way around ABCs (was What for -- for? (was A bug?))

FromRustom Mody <rustompmody@gmail.com>
Date2014-10-30 10:01 -0700
SubjectFinding way around ABCs (was What for -- for? (was A bug?))
Message-ID<b2d79374-d0fd-4a47-ba3d-7d1b476d1526@googlegroups.com>
In reply to#79896
On Wednesday, October 29, 2014 11:49:27 AM UTC+5:30, Zachary Ware wrote:
> On Wed, Oct 29, 2014 at 1:11 AM, Rustom Mody wrote:
> > On Wednesday, October 29, 2014 11:10:06 AM UTC+5:30, Zachary Ware wrote:
> >> Of course, that's 3 (progressively shorter) loops to get the names of
> >> the ABCs of a class compared to 1 (fairly short in the first place)
> >> loop for a map of relationships to all available ABCs, but optimizing
> >> such a toy as this would just be an exercise in futility :)
> >
> > Not so.
> >
> > The charm of introspection is that the introspection
> > itself can be introspected.
> > For that to be convincing there needs to be a good combo
> > of clarity and succinctness.  In particular why not reduce
> > the two functions to one?
> >
> > def get_abc_names(cls):
> >     return [abc.__name__ for abc in abcs if issubclass(cls,abc)]
> 
> Well, it depends on what you actually want, the spec has been a bit fuzzy ;)

Thanks for this much -- its helpful.
Regarding ABCs -- is there a central documentation for them:

"What exactly is a sequence or iterable or etc protocol?"

This information seems to be strewn all over the place but systematically.

[toc] | [prev] | [next] | [standalone]


#79958 — Re: Finding way around ABCs (was What for -- for? (was A bug?))

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-10-30 11:09 -0600
SubjectRe: Finding way around ABCs (was What for -- for? (was A bug?))
Message-ID<mailman.15326.1414689029.18130.python-list@python.org>
In reply to#79957
On Thu, Oct 30, 2014 at 11:01 AM, Rustom Mody <rustompmody@gmail.com> wrote:
> On Wednesday, October 29, 2014 11:49:27 AM UTC+5:30, Zachary Ware wrote:
>> On Wed, Oct 29, 2014 at 1:11 AM, Rustom Mody wrote:
>> > On Wednesday, October 29, 2014 11:10:06 AM UTC+5:30, Zachary Ware wrote:
>> >> Of course, that's 3 (progressively shorter) loops to get the names of
>> >> the ABCs of a class compared to 1 (fairly short in the first place)
>> >> loop for a map of relationships to all available ABCs, but optimizing
>> >> such a toy as this would just be an exercise in futility :)
>> >
>> > Not so.
>> >
>> > The charm of introspection is that the introspection
>> > itself can be introspected.
>> > For that to be convincing there needs to be a good combo
>> > of clarity and succinctness.  In particular why not reduce
>> > the two functions to one?
>> >
>> > def get_abc_names(cls):
>> >     return [abc.__name__ for abc in abcs if issubclass(cls,abc)]
>>
>> Well, it depends on what you actually want, the spec has been a bit fuzzy ;)
>
> Thanks for this much -- its helpful.
> Regarding ABCs -- is there a central documentation for them:
>
> "What exactly is a sequence or iterable or etc protocol?"
>
> This information seems to be strewn all over the place but systematically.

Maybe the glossary?

https://docs.python.org/3/glossary.html

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

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


csiph-web