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

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -