python - unicode string formatting -
i saw in piece of uncommented code. there ever reason this?
new_var = u'%s' % (child.tag)
where child instance , tag attribute of instance? seems more simply:
new_var = unicode(child.tag)
they identical, , it's matter of preference. in terms of zen of python , such, there's not "better" way both explicit in getting unicode representation of child.tag
. mechanically same thing:
>>> class foo(object): def __init__(self, val): self.val = val def __str__(self): return "str: %s" % self.val def __unicode__(self): return "unicode: %s" % self.val >>> f = foo("bar") >>> u'%s' % f u'unicode: bar' >>> unicode(f) u'unicode: bar' >>> '%s' % f 'str: bar'
Comments
Post a Comment