luofeiyu_cu 发表于 2014-08-21 09:34

如何理解这个format函数

本帖最后由 luofeiyu_cu 于 2014-08-21 09:35 编辑

print('My name is {0.name}'.format(open('out.txt', 'w')))

上面的语句为何输出:

My name is out.txt

这里的{0.name}这里的0 是什么意思?
.name通过测试的方式,得到一点启示

>>> print('My name is {0.mode}'.format(open('out.txt', 'w')))
My name is w
>>> print('My name is {0.encoding}'.format(open('out.txt', 'w')))
My name is cp936
>>> print('My name is {encoding}'.format(open('out.txt', 'w')))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'encoding'

0. 这个含义还是有点不清楚。

ssfjhh 发表于 2014-08-21 10:03

ssfjhh 发表于 2014-08-20 21:04 static/image/common/back.gif
Fancier Output Formatting

你到底是有多无视我的帖子

icymirror 发表于 2014-08-21 13:10

回复 2# ssfjhh
个人猜测,可能不是无视,有可能是没有办法读你提供的英文文档。所以,再次发贴寻求帮助。
页: [1]
查看完整版本: 如何理解这个format函数