how to add new print %b format to python?

John Roth newsgroups at jhrothjr.com
Mon Apr 26 13:56:45 EDT 2004


<P at draigBrady.com> wrote in message news:408D4480.8090104 at draigBrady.com...
> Rusty Shackleford wrote:
> > 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.
>
> I don't think this will be accepted as the
> format args are really a lowest common denominator
> across all systems. For e.g. on linux you can use
> the ' modifier to print numbers in locale format
> (and example for mine is 1,234).

I don't believe this is the case. I think the % operator
does not use the C library's sprintf function., but I could
be wrong.

[snip]

The rest of this is equally off base - he explicitly said he
was not interested in ***whether*** he should do it, but
only in *** where to look *** to find out how to do it
as a summer programming exercise in the python core.

John Roth
>
> Pádraig.





More information about the Python-list mailing list