Re: overriding __getitem__ for a subclass of dict

2009-11-17 Thread Steve Howell
On Nov 17, 7:11 am, Scott David Daniels wrote: > Steve Howell wrote: > > ... > > > Eventually, I realized that it was easier to just monkeypatch Django > > while I was in test mode to get a more direct hook into the behavior I > > was trying to monitor, and then I didn't need to bother with > > ov

Re: overriding __getitem__ for a subclass of dict

2009-11-17 Thread Scott David Daniels
Steve Howell wrote: ... Eventually, I realized that it was easier to just monkeypatch Django while I was in test mode to get a more direct hook into the behavior I was trying to monitor, and then I didn't need to bother with overriding __getitem__ or creating complicated wrapper objects Sin

Re: overriding __getitem__ for a subclass of dict

2009-11-17 Thread Steve Howell
On Nov 16, 10:11 pm, Carl Banks wrote: > On Nov 16, 10:32 am, Steve Howell wrote: > > > > > On Nov 16, 2:35 am, Carl Banks wrote: > > > > On Nov 15, 2:52 pm, Steve Howell wrote: > > > > > Does anybody have any links that points to the rationale for ignoring > > > > instance definitions of __get

Re: overriding __getitem__ for a subclass of dict

2009-11-16 Thread Carl Banks
On Nov 16, 10:32 am, Steve Howell wrote: > On Nov 16, 2:35 am, Carl Banks wrote: > > > > > On Nov 15, 2:52 pm, Steve Howell wrote: > > > > Does anybody have any links that points to the rationale for ignoring > > > instance definitions of __getitem__ when new-style classes are > > > involved?  I

Re: overriding __getitem__ for a subclass of dict

2009-11-16 Thread Steve Howell
On Nov 16, 4:06 pm, greg wrote: > Christian Heimes wrote: > > Most magic methods are implemented as descriptors. Descriptors only > > looked up on the type to increase the performance of the interpreter and > > to simply the C API. > > There's also a semantic problem. Since new-style > classes are

Re: overriding __getitem__ for a subclass of dict

2009-11-16 Thread Steve Howell
On Nov 16, 5:46 pm, Steven D'Aprano wrote: > On Mon, 16 Nov 2009 10:32:19 -0800, Steve Howell wrote: > > Actually, the __getitem__ workaround that I proposed earlier only works > > on subclasses of dict, not dict themselves.  So given a pure dictionary > > object, it is impossible to hook into att

Re: overriding __getitem__ for a subclass of dict

2009-11-16 Thread Steven D'Aprano
On Mon, 16 Nov 2009 10:32:19 -0800, Steve Howell wrote: > Actually, the __getitem__ workaround that I proposed earlier only works > on subclasses of dict, not dict themselves. So given a pure dictionary > object, it is impossible to hook into attribute lookups after > instantiation in debugging/t

Re: overriding __getitem__ for a subclass of dict

2009-11-16 Thread greg
Christian Heimes wrote: Most magic methods are implemented as descriptors. Descriptors only looked up on the type to increase the performance of the interpreter and to simply the C API. There's also a semantic problem. Since new-style classes are also instances (of class 'type') and you can cr

Re: overriding __getitem__ for a subclass of dict

2009-11-16 Thread Steve Howell
On Nov 16, 2:35 am, Carl Banks wrote: > On Nov 15, 2:52 pm, Steve Howell wrote: > > > Does anybody have any links that points to the rationale for ignoring > > instance definitions of __getitem__ when new-style classes are > > involved?  I assume it has something to do with performance or > > pro

Re: overriding __getitem__ for a subclass of dict

2009-11-16 Thread Carl Banks
On Nov 15, 2:52 pm, Steve Howell wrote: > Does anybody have any links that points to the rationale for ignoring > instance definitions of __getitem__ when new-style classes are > involved?  I assume it has something to do with performance or > protecting us from our own mistakes? "Not important

Re: overriding __getitem__ for a subclass of dict

2009-11-15 Thread Steve Howell
On Nov 15, 4:58 pm, Steve Howell wrote: > On Nov 15, 4:03 pm, Christian Heimes wrote: > > > Try this untested code: > > > class Spam(dict): > >     def __getitem__(self, key): > >         getitem = self.__dict__.get("__getitem__", dict.__getitem__) > >         return getitem(self, key) > > [...]

Re: overriding __getitem__ for a subclass of dict

2009-11-15 Thread Steve Howell
On Nov 15, 4:03 pm, Christian Heimes wrote: > Steve Howell wrote: > > Does anybody have any links that points to the rationale for ignoring > > instance definitions of __getitem__ when new-style classes are > > involved?  I assume it has something to do with performance or > > protecting us from o

Re: overriding __getitem__ for a subclass of dict

2009-11-15 Thread MRAB
Christian Heimes wrote: Steve Howell wrote: Does anybody have any links that points to the rationale for ignoring instance definitions of __getitem__ when new-style classes are involved? I assume it has something to do with performance or protecting us from our own mistakes? Most magic method

Re: overriding __getitem__ for a subclass of dict

2009-11-15 Thread Christian Heimes
Steve Howell wrote: > Does anybody have any links that points to the rationale for ignoring > instance definitions of __getitem__ when new-style classes are > involved? I assume it has something to do with performance or > protecting us from our own mistakes? Most magic methods are implemented as

Re: overriding __getitem__ for a subclass of dict

2009-11-15 Thread Steve Howell
On Nov 15, 12:01 pm, Jon Clements wrote: > On Nov 15, 7:23 pm, Steve Howell wrote: > > > On Nov 15, 10:25 am, Steve Howell wrote: > > > > [see original post...] > > > I am most > > > interested in the specific mechanism for changing the __getitem__ > > > method for a subclass on a dictionary.  T

Re: overriding __getitem__ for a subclass of dict

2009-11-15 Thread Steve Howell
On Nov 15, 12:01 pm, Jon Clements wrote: > On Nov 15, 7:23 pm, Steve Howell wrote: > > > I am more precisely looking for a way to change the behavior of foo > > ['bar'] (side effects and possibly return value) where "foo" is an > > instance of a class that subclasses "dict," and where "foo" is no

Re: overriding __getitem__ for a subclass of dict

2009-11-15 Thread Jon Clements
On Nov 15, 7:23 pm, Steve Howell wrote: > On Nov 15, 10:25 am, Steve Howell wrote: > > > [see original post...] > > I am most > > interested in the specific mechanism for changing the __getitem__ > > method for a subclass on a dictionary.  Thanks in advance! > > Sorry for replying to myself, but

Re: overriding __getitem__ for a subclass of dict

2009-11-15 Thread Steve Howell
On Nov 15, 11:19 am, Gary Herron wrote: > Steve Howell wrote: > > I ran the following program, and found its output surprising in one > > place: > > >     class OnlyAl: > >         def __getitem__(self, key): return 'al' > > >     class OnlyBob(dict): > >         def __getitem__(self, key): return

Re: overriding __getitem__ for a subclass of dict

2009-11-15 Thread Steve Howell
On Nov 15, 10:25 am, Steve Howell wrote: > [see original post...] > I am most > interested in the specific mechanism for changing the __getitem__ > method for a subclass on a dictionary.  Thanks in advance! Sorry for replying to myself, but I just realized that the last statement in my original p

Re: overriding __getitem__ for a subclass of dict

2009-11-15 Thread Gary Herron
Steve Howell wrote: I ran the following program, and found its output surprising in one place: class OnlyAl: def __getitem__(self, key): return 'al' class OnlyBob(dict): def __getitem__(self, key): return 'bob' import sys; print sys.version al = OnlyAl() bo

overriding __getitem__ for a subclass of dict

2009-11-15 Thread Steve Howell
I ran the following program, and found its output surprising in one place: class OnlyAl: def __getitem__(self, key): return 'al' class OnlyBob(dict): def __getitem__(self, key): return 'bob' import sys; print sys.version al = OnlyAl() bob = OnlyBob() pri