Sto cercando di mantenere la pluralizzazione delle stringhe esistenti il più semplice possibile e mi chiedevo se fosse possibile ottenere str.format()
per interpretare un valore predefinito durante la ricerca di kwargs . Ecco un esempio:Valori kwarg predefiniti per il metodo str.format() di Python
string = "{number_of_sheep} sheep {has} run away"
dict_compiled_somewhere_else = {'number_of_sheep' : 4, 'has' : 'have'}
string.format(**dict_compiled_somewhere_else)
# gives "4 sheep have run away"
other_dict = {'number_of_sheep' : 1}
string.format(**other_dict)
# gives a key error: u'has'
# What I'd like is for format to somehow default to the key, or perhaps have some way of defining the 'default' value for the 'has' key
# I'd have liked: "1 sheep has run away"
Acclamazioni
correlati: [Plural String Formattazione] (http://stackoverflow.com/questions/21872366/plurale-stringa-formattazione) –
Nice link, avrei dovuto vederlo prima – Patrick
Vedi anche: https://stackoverflow.com/questions/20248355/how-to-get-python-to-gracefully-format-none-and -non-campi-esistenti – dreftymac