string formatting quirk?

A

anuraguniyal

Hi,

''%([]) doesn't raise exception
but
''%('') does

Can anyone explain me why??

rgds
Anurag
 
P

Peter Otten

''%([]) doesn't raise exception
but
''%('') does

Can anyone explain me why??

That is a side-effect of duck-typing. The duck-type of an empty list is
indistinguishable from that of an empty dictionary. Not testing the exact
type here achieves consistency with the behaviour of custom dictionaries,
e. g:
.... def __getitem__(self, index):
.... return list.__getitem__(self, int(index))
....
"%(0)s" % List([42]) '42'
"%(1)s %(0)s" % List([42, 24]) '24 42'
"" % List([])
''

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top