Generic proxy (that proxies methods like __iter__)

D HANNEY spam2008 at nney.com
Thu Jan 28 08:54:16 EST 2010


On Jan 28, 1:31 pm, D HANNEY <spam2... at nney.com> wrote:
>
> Your solution works if I change "type(obj)" to say "obj.__class__".
> If I don't make this change Python complains "TypeError: Error when
> calling the metaclass bases type 'instance' is not an acceptable base
> type".
> So, I've got something that works but I don't understand why it works.
> I've read your docs.python reference but that hasn't helped yet. It
> might come to me but in the meantime, can you (or anyone) offer an
> explanation for this?
>
> Thanks in advance,
>
> David

Oh it looks like this is py3K thing.

Python 3.0.1+ (r301:69556, Apr 15 2009, 15:59:22)
>>> from io import StringIO
>>> type(StringIO("x"))
<class 'io.StringIO'>
>>> StringIO("x").__class__
<class 'io.StringIO'>
>>>

Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
>>> from StringIO import StringIO
>>> type(StringIO("x"))
<type 'instance'>
>>> StringIO("x").__class__
<class StringIO.StringIO at 0xb7ea15fc>
>>>

I was using 2.6.2. Your suggestion would have been fine in py3k.

Even if I stuck with 2.6.2 and StringIO was a "new style class" there
then it would have been fine ... but it isn't.
As it stands __class__ seems the only way that works on both 3k and
2k.
Bit weird, but I get it now.

Thanks again!

David



More information about the Python-list mailing list