[Python-ideas] String formatting and namedtuple

Adam Olsen rhamph at gmail.com
Wed Feb 11 22:27:39 CET 2009


On Wed, Feb 11, 2009 at 2:10 PM, Guido van Rossum <guido at python.org> wrote:
> class GetItemToGetAttrAdaptor:
>  def __init__(self, target):
>    self.target = target
>  def __getitem__(self, key):
>    try:
>      return getattr(self.target, key)
>    except AttributeError as e:
>      raise KeyError(str(e))
>
> You could then use "re={real} im={imag}".format(GetItemToGetAttrAdaptor(1j+2))

I'm confused, we can already satisfy this use case:

>>> "re={0.real} im={0.imag}".format(1j+2)
're=2.0 im=1.0'


-- 
Adam Olsen, aka Rhamphoryncus



More information about the Python-ideas mailing list