Use str.format() to access object attributes

半城伤御伤魂 提交于 2019-12-04 16:09:16

问题


I have a Python object with attributes a, b, c.

I still use old string formatting, so I'd normally print these manually:

 print 'My object has strings a=%s, b=%s, c=%s' % (obj.a, obj.b, obj.c)

Lately, my strings have been getting super long, and I'd much rather be able to simply pass the object into a string format function, something like:

 print 'My object has strings a=%a, b=%b, c=%c'.format(obj)

However, the syntax is incorrect. Is this possible?


回答1:


You can use the .attribute_name notation inside the format fields themselves:

print 'My object has strings a={0.a}, b={0.b}, c={0.c}'.format(obj)

Below is a demonstration:

>>> class Test(object):
...     def __init__(self, a, b, c):
...         self.a = a
...         self.b = b
...         self.c = c
...
>>> obj = Test(1, 2, 3)
>>> 'My object has strings a={0.a}, b={0.b}, c={0.c}'.format(obj)
'My object has strings a=1, b=2, c=3'
>>>

Note however that you do need to number the format fields when doing this. Also, as you can see, the str.format function has its format fields denoted by curly braces {...}, not the % sign.

For more information, here is a reference on the Format String Syntax in Python.




回答2:


I think it's preferable to use vars() to access an object's attributes as a dict rather than usng __dict__.

So you could do this:

"My object has strings a={a}, b={b}, c={c}".format(**vars(obj))

For more background on why vars() is preferable to __dict__, see the answer to the question Use dict or vars()?.




回答3:


As @igniteflow wrote in a buried comment:

'My object has strings a={a}, b={b}, c={c}'.format(**obj.__dict__)

With my limited python understanding: .__dict__ is a dict with all the instances attributes and the ** operator basically unpacks them and adds them as key=value args to the method




回答4:


Some of the suggested answers that rely on vars(...) don't work with immutable objects:

>>> class foo(object):
...   __slots__ = ()
...   def __init__(self):
...     self.x = 1
...
>>> vars(foo())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in __init__
AttributeError: 'foo' object has no attribute 'x'

I think it's preferable to stick with something reliable and explicitly list what you need to be printed.

If you need to print a ton of attributes then storing those values as attributes might not be the best idea.




回答5:


For the sake of completeness, building on @igniteflow and @Christian, you could use the % string format operator and write:

'My object has strings a=%(a)s, b=%(b)s, c=%(c)s' % obj.__dict__


来源:https://stackoverflow.com/questions/26791908/use-str-format-to-access-object-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!