how to add new print %b format to python?

John Roth newsgroups at jhrothjr.com
Mon Apr 26 14:03:00 EDT 2004


"Rusty Shackleford" <rs at overlook.homelinux.net> wrote in message
news:slrnc8q9qp.htc.rs at frank.overlook.homelinux.net...
> I have a Summer in front of me without any school, and I'd like to add a
> new format for python print strings that will show any number in a
> binary representation.  For example:
>
> >>> '%b' % 3
> 11
> >>> '%b' % 5
> 101
>
> You get the idea.  I've written functions that return strings, so that
> part is done, but where do I go to tinker with the python interpreter to
> add this new format?
>
> Please don't argue with me about whether this is an advisable goal in
> itself -- I'm using it as a method to learn about the internals of the
> python language.

Well, if I was familiar with the internals, I'd start with the code
that implements the string object, since it's simply an overload of
the '%' operator. Sorry I can't help you with a module name, though.

To actually get it added to the core, you need to write a PEP and
champion it through the process. Writing a reference implementation
and the documentation is also a necessity. There's a PEP that
explains the documentation standards for Python C language code;
if your patch follows that you've improved your chances by some
significant amount.

John Roth






More information about the Python-list mailing list