[Python-Dev] str with base

"Martin v. Löwis" martin at v.loewis.de
Tue Jan 17 09:21:59 CET 2006


Alex Martelli wrote:
> Is it finally time in Python 2.5 to allow the "obvious" use of, say,  
> str(5,2) to give '101', just the converse of the way int('101',1)  
> gives 5?  I'm not sure why str has never allowed this obvious use --  
> any bright beginner assumes it's there and it's awkward to explain  
> why it's not!-). 

My main concern is what the impact on __str__ would be. It seems
"obvious" that

  def str(obj, *args):
    return obj.__str__(*args)

because it is ultimately int's responsibility to interpret the base
argument, not str's.

People would then come up with use cases like

  class Color:
    msg = {'en':['red', 'green', 'blue'], 'de':['rot','grün','blau']}
    def __str__(self, language='en'):
      return self.msg[language][self.value]

  red = Color(0)

so you could say

  print str(red, 'de')

I don't think I like this direction.

Regards,
Martin


More information about the Python-Dev mailing list