Python AttributeError: 'NoneType' 对象没有属性 'format'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28378257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AttributeError: 'NoneType' object has no attribute 'format'
提问by MonotonousSonder
print ("Hello World")
print ("{} World").format(Hello)
I'm working on my first "Hello World" program and I can get it to work by using the print function and just a simple string text but when I try to use .formatit gives me the error:
我正在开发我的第一个“Hello World”程序,我可以通过使用 print 函数和一个简单的字符串文本来让它工作,但是当我尝试使用.format它时,出现了错误:
AttributeError: 'NoneType' object has no attribute 'format'
Is this saying that I need to initialize a variable for .formator am I missing something?
这是说我需要初始化一个变量.format还是我遗漏了什么?
回答by Bhargav Rao
Your brackets are wrong
你的括号错了
print("Hello World")
print("{} World".format('Hello'))
Note - the errors
注意 - 错误
- The
formatfunction is an attribute ofstrso it needs to be called on the string - Unless declared,
Hellois a string and should be'Hello'
- 该
format函数是一个属性,str因此需要在字符串上调用它 - 除非声明,否则
Hello是一个字符串,应该是'Hello'
For Py2 you can do
对于 Py2,你可以做
print "{} World".format('Hello')
回答by Alex Martelli
Function printreturns None, so that's obviously what you're getting from the start of your second statement, namely
函数print返回None,所以这显然是你从第二个语句开始时得到的,即
print ("{} World")
On that return value of None, you then call .format(Hello)-- even if a variable named Hellowas assigned somewhere in your code (and you're not showing it to us!), you're calling that .formatmethod on the Nonereturned from your printcall, which makes no sense.
在 的返回值上None,您然后调用.format(Hello)- 即使Hello在您的代码中的某处分配了一个名为的变量(并且您没有向我们展示它!),您正在调用从您的调用返回的该.format方法,这使得没有意义。Noneprint
Rather, you want to call .formaton the string"{} World"-- so the closed-paren right after the string and before the dot is clearly a terrible mistake! Move that )to the end of the statement, afterthe call to formaton that string.
相反,你要拨打.format的字符串"{} World"-这样的字符串后点之前闭括号权显然是一个可怕的错误!在对该字符串调用 to之后,将其)移到语句的末尾。format
Moreover, isHellothe name of a variable whose value you want to format? I sure hope not, else why haven't you shown us that variable being assigned?! I suspect you want to format a constant stringand just absent-mindedly forgot to put it in quotes(to show it's a constant, not the name of a variable!) -- 'Hello', notHellowithout quotes! Thatis what you should be passing to the proper form of the .formatcall...!
此外,是Hello您要格式化其值的变量的名称吗?我当然希望不会,否则你为什么不向我们展示正在分配的变量?!我怀疑你想格式化一个常量字符串,只是心不在焉地忘记把它放在引号中(以表明它是一个常量,而不是变量的名称!) -- 'Hello',而不是Hello没有引号! 这就是你应该传递给.format调用的正确形式的东西......!
回答by Ricky Levi
even thought the requestor of the original question was wrong in using the .formatstructure - I believe he's still right about one thing - the behavior in Python3 is different when having a value that equals None
甚至认为原始问题的请求者在使用该.format结构时是错误的- 我相信他对一件事仍然是正确的 - 当具有等于的值时,Python3 中的行为是不同的None
Example
例子
fmt = '{:^9}|{:^13}|{:^18}'
data = [1, None, 'test']
print(fmt.format(*data))
Python2.7
Python2.7
$ python2.7 test
1 | None | test
In Python3.6
在 Python3.6 中
python3.6 test
Traceback (most recent call last):
File "test", line 5, in <module>
print(fmt.format(*data))
TypeError: unsupported format string passed to NoneType.__format__
But if we remove the format's features of column-width
但是如果我们去掉format列宽的特征
fmt = '{}|{}|{}'
data = [1, None, 'test']
print(fmt.format(*data))
OR convert all values to Strings using !s
或使用将所有值转换为字符串 !s
fmt = '{!s:^9}|{!s:^13}|{!s:^18}'
It works just fine in both versions ...
它在两个版本中都可以正常工作......

