
Hallo zusammen, kann mich mal jemand erhellen, was der feine Unterschied ist, wenn ich eine Klasse von UserDict.UserDict oder dict ableite und die in der Python Format String Syntax verwende? Gegeben ist der Code unten und ich würde in beiden Fällen die selbe Ausgabe erwarten (Python 2.7.5). Ist aber unterschiedlich: $python tmp/dict_template_test.py Str1stValueUserDict Classic: Foo a1 bar b1 Str1stValueUserDict New: Foo a1 bar b1 Str1stValueDict Classic: Foo a1 bar b1 Str1stValueDict New: Foo ['a1', 'a2'] bar ['b1', 'b2'] Warum? Ciao, Michael. ----------------------------- snip ----------------------------- import UserDict class Str1stValueUserDict(UserDict.UserDict): def __getitem__(self,key): return UserDict.UserDict.__getitem__(self,key)[0] e = { 'a':['a1','a2'], 'b':['b1','b2'], } e_1st = Str1stValueUserDict(e) print 'Str1stValueUserDict Classic:','Foo %(a)s bar %(b)s' % e_1st print 'Str1stValueUserDict New:','Foo {a} bar {b}'.format(**e_1st) class Str1stValueDict(dict): def __getitem__(self,key): return dict.__getitem__(self,key)[0] e_1st = Str1stValueDict(e) print 'Str1stValueDict Classic:','Foo %(a)s bar %(b)s' % e_1st print 'Str1stValueDict New:','Foo {a} bar {b}'.format(**e_1st)