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


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

Interesting list() un-optimization

Started byRoy Smith <roy@panix.com>
First post2013-03-06 22:20 -0500
Last post2013-03-10 19:50 -0400
Articles 1 on this page of 21 — 10 participants

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


Contents

  Interesting list() un-optimization Roy Smith <roy@panix.com> - 2013-03-06 22:20 -0500
    Re: Interesting list() un-optimization Dave Angel <davea@davea.name> - 2013-03-06 22:38 -0500
    Re: Interesting list() un-optimization Tim Chase <python.list@tim.thechases.com> - 2013-03-06 21:57 -0600
    Re: Interesting list() un-optimization Kev Dwyer <kevin.p.dwyer@gmail.com> - 2013-03-07 07:31 +0000
    Re: Interesting list() un-optimization Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-03-07 11:22 +0000
    Re: Interesting list() un-optimization Ian Kelly <ian.g.kelly@gmail.com> - 2013-03-07 09:00 -0700
    Re: Interesting list() un-optimization Christian Heimes <christian@python.org> - 2013-03-07 17:20 +0100
    Re: Interesting list() un-optimization Ian Kelly <ian.g.kelly@gmail.com> - 2013-03-07 10:31 -0700
    Re: Interesting list() un-optimization Stefan Behnel <stefan_ml@behnel.de> - 2013-03-07 20:19 +0100
    Re: Interesting list() un-optimization Ian Kelly <ian.g.kelly@gmail.com> - 2013-03-07 13:26 -0700
    Re: Interesting list() un-optimization Terry Reedy <tjreedy@udel.edu> - 2013-03-07 15:29 -0500
    Re: Interesting list() un-optimization Terry Reedy <tjreedy@udel.edu> - 2013-03-07 15:34 -0500
    Re: Interesting list() un-optimization Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-03-07 20:41 +0000
    Re: Interesting list() un-optimization Terry Reedy <tjreedy@udel.edu> - 2013-03-07 17:53 -0500
    Re: Interesting list() un-optimization Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-08 17:59 +0000
      Re: Interesting list() un-optimization Roy Smith <roy@panix.com> - 2013-03-08 13:45 -0500
    Re: Interesting list() un-optimization Roy Smith <roy@panix.com> - 2013-03-10 09:05 -0400
      Re: Interesting list() un-optimization Terry Reedy <tjreedy@udel.edu> - 2013-03-10 17:39 -0400
        Re: Interesting list() un-optimization Roy Smith <roy@panix.com> - 2013-03-10 18:34 -0400
          Re: Interesting list() un-optimization Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-10 23:35 +0000
            Re: Interesting list() un-optimization Roy Smith <roy@panix.com> - 2013-03-10 19:50 -0400

Page 2 of 2 — ← Prev page 1 [2]


#41047

FromRoy Smith <roy@panix.com>
Date2013-03-10 19:50 -0400
Message-ID<roy-3B4C01.19502210032013@70-1-84-166.pools.spcsdns.net>
In reply to#41046
In article <513d18d6$0$6512$c3e8da3$5496439d@news.astraweb.com>,
 Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote:

> On Sun, 10 Mar 2013 18:34:58 -0400, Roy Smith wrote:
> 
> > Yeah, that's what I was afraid of.  The "obvious" solution of:
> > 
> > class QuerySet(mongoengine.queryset.QuerySet):
> >     def __init__(self, document, collection):
> >         super(QuerySet, self).__init__(document, collection) [...]
> >         del self.__len__
> > 
> > results in:
> > 
> > [rest of stack dump elided]
> >     del self.__len__
> > AttributeError: __len__
> > 
> > which I don't understand.
> 
> You don't define a per-instance custom QuerySet attribute called __len__, 
> so you can't delete it from the instance.
> 
> The existing __len__ attribute is attached to the mongoengine 
> queryset.QuerySet class, not the instance. You could monkeypatch the 
> parent class, but that will probably break something else:
> 
>     del mongoengine.queryset.QuerySet.__len__
> 
> 
> or you could try over-riding __len__ with a fake that pretends it doesn't 
> exist:
> 
> 
> def __len__(self):
>     raise AttributeError
> 
> 
> Try that and see it it works. (It may not.)

Yeah, that was one of the things I experimented with.  It seems to work, 
but like most of these other things, I suspect it's version specific.  
If list() does:

try:
    l = obj.__len__()
except AttributeErrror:
    l = 0

then we're good.  On the other hand, if it does:

if obj.getattr('__len__', None):
    # etc

then it fails.

At this point, I'm thinking the monkeypatch is the right solution.

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

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


csiph-web