str.format utility function
Hi list, I'm sorry if this has been discussed before, but I did not find any references. I've been playing abit with the new str.format function. I really like the syntax and simplicity. However, when simply printing named variables in locals() or a class, I quite common use-case, I find it a bit too complicated with the **: "Local variable var1 is %(var1)s" % locals() vs "Local variable var1 is {var1}".format(**locals()) and "Instance variable var2 is %(var2)s" % self.__dict__ vs "Instance variable var2 is {var2}" % **self.__dict__ Therefore I have made myself a utility funcion: def easyformat(s, data): try: return s.format(**data) except TypeError: return s.format(**data.__dict__) so that I can do the following: "Local variable var1 is {var1}".format(**locals()) and "Instance variable var2 is %(var2)s" % self Should a function similar to this (maybe with a better name) be included included in some standard library? -Dag _________________________________________________________________ Få mer ut av Windows Live™ med Internet Explorer® 8. http://microsoft.no/ie
participants (3)
-
Dag Moxnes
-
Gerald Britton
-
Terry Reedy