[Python-Dev] Adding __format__ to classic classes

Jeffrey Yasskin jyasskin at gmail.com
Thu Feb 14 06:26:36 CET 2008


Oops, sorry for the spam. I didn't see that there were already answers
in the rest of the thread. :-(

On Feb 13, 2008 9:25 PM, Jeffrey Yasskin <jyasskin at gmail.com> wrote:
> On Feb 13, 2008 1:42 PM, Eric Smith <eric+python-dev at trueblade.com> wrote:
> > Guido van Rossum wrote:
> > >> Much to my surprise, this already works:
> > >>
> > >>  >>> format(oldstyle(), '+^50s')
> > >> '+++++<__main__.oldstyle instance at 0x3d91f8>+++++'
> > >>  >>>
> > >>
> > >> So I guess it's a moot point.  I'm using the same code as I use in 3.0,
> > >> where I call:
> > >>    meth = _PyType_Lookup(Py_TYPE(value), format_str);
> > >> where format_str is "__format__" interned.  And I'm getting a value back
> > >> for old style classes.
> > >>
> > >> Eric.
> > >
> > > But now try overriding __format__().  The 3.0 code uses
> > > _PyType_Lookup() which is not traversing the class dicts for classic
> > > classes, so it won't find __format__ overrides.
> > >
> >
> > Okay, I see and understand that issue.  But looking at len or getattr, I
> > don't see how to generalize it to __format__.  __len__ and __getattr__
> > have special support in the classes, with cl_getattr, tp_getattr, etc.
> >
> > I hate to be dense, but could you point me to some code that does
> > something similar but looks up the method by name?
>
> I'm not sure if it'll be exactly analogous, but you might look at
> __trunc__ and math.trunc for inspiration.
>
> --
> Namasté,
> Jeffrey Yasskin
> http://jeffrey.yasskin.info/
>



-- 
Namasté,
Jeffrey Yasskin
http://jeffrey.yasskin.info/


More information about the Python-Dev mailing list