[Python-Dev] Optional arguments for str.encode /.decode
aleaxit at yahoo.com
Fri Nov 7 10:49:27 EST 2003
On Friday 07 November 2003 04:31 pm, Barry Warsaw wrote:
> On Fri, 2003-11-07 at 10:24, Alex Martelli wrote:
> > We must keep supporting that approach, yes (alas), but maybe it's
> > not too late to encourage another alternative style instead? E.g., have
> > some object exposing attributes corresponding to those strings that
> > do name codecs, so that while e.g.
> > s.encode('zlib', level=9)
> > would have to keep working, the officially encouraged style would be:
> > s.encode(codec.zlib, level=9)
> > or something of that ilk...?
> If s.encode(codec.notacodec, level=9) throws an AttributeError, then
> +1. Add that to the original idea and +1 all around.
We should surely be able to arrange an object (codecs.codec ...? not
sure where it should best live) that exposes as attributes those codecs
that are registered, and raises AttributeError for attempts to access on
it attributes with other names, it seems to me. Q&D worst case,
def __getattr__(self, name):
except LookupError: raise AttributeError
else: return name
codecs.codec = _Codec_Lookupper()
[which is something we could try out right now...]
(but I suspect that we can do better, performance-wise, by returning
the lookup's result as a non-string in case of success, saving .encode
and .decode some duplicated work).
More information about the Python-Dev