先来看一段Python代码:

class Negate:
def __init__(self, val):
self.val = -val
def __repr__(self):
return str(self.val) if __name__ == '__main__':
print('{0:5}'.format(-5)) x = Negate(5)
print(x)
print('{0:5}'.format(x))

这段代码在不同的Python版本下有不同的输出结果:

在Python 2.7中,输出为:

-5

-5

-5

在Python 3.4中,输出为:

-5

-5

TypeError: non-empty format string passed to object.__format__

在Python 3.6中,输出为:

-5

-5

TypeError: unsupported format string passed to Negate.__format__

从上面的输出中可以看出,使用format来对齐数字和字符串时,其效果是不同的:

>>> '{0:5}'.format(-5)

'   -5'

>>> '{0:5}'.format('-5')

'-5   '

另外,对于Python3.4和Python 3.6中输出的TypeError错误,原因如下:

变量名x所引用的对象的类型为<class '__main__.Negate'>,而该类型没有自己的__format__()方法,所以只能使用默认继承自object对象的__format__()方法,而该默认方法不支持任何格式化选项(比如字段宽度),所以会引发上述TypeError错误。

对于上述TypeError错误,解决方法就是先把对象转换为字符串。转换方式有两种:

方式一:

print('{0:5}'.format(str(x)))

方式二:

print('{0!s:5}'.format(x))

使用其中的任何一种方式都可以。

TypeError: format string的更多相关文章

  1. Python TypeError: not enough arguments for format string

    今天使用mysqldb执行query语句的时候,在执行这条语句的时候: select PROJ, DATE_FORMAT(MAX(DATE),'%Y-%m-%') AS MAXDATE, DATE_F ...

  2. 使用Python过程出现的细节问题:TypeError: not enough arguments for format string

    今天使用字符串格式化时,遇到的一点小问题:调用这个方法解释器出错了:TypeError: not enough arguments for format string def ll(name,age) ...

  3. 关于Python json解析过程遇到的TypeError: expected string or buffer

    关于Python json解析过程遇到的问题:(爬取天气json数据所遇到的问题http://tianqi.2345.com/) part.1 url——http://tianqi.2345.com/ ...

  4. 【错误】python百分号冲突not enough arguments for format string

    query = "SELECT * FROM devices WHERE devices.`id` LIKE '%{}%'".format("f2333") d ...

  5. 解决:error: Cannot fetch repo (TypeError: expected string or buffer)

    同步源码,问题重现: Fetching project platform/external/libopus Fetching project repo error: Cannot fetch repo ...

  6. 再探Java基础——String.format(String format, Object… args)的使用

    最近看到类似这样的一些代码:String.format("参数%s不能为空", "birthday"); 以前还没用过这功能不知咐意思,后研究了一下,详细讲解如 ...

  7. OD: Format String, SQL Injection, XSS

    Format String 格式化串漏洞 考虑如下的代码: #include<stdio.h> int main() { int a=44,b=77; printf("a=%d, ...

  8. String.Format(string, arg0)中sring格式

    复合格式字符串和对象列表将用作支持复合格式设置功能的方法的参数.复合格式字符串由零个或多个固定文本段与一个或多个格式项混和组成.固定文本是所选择的任何字符串,并且每个格式项对应于列表中的一个对象或装箱 ...

  9. Oracle问题之literal does not match format string

    问题: oerr ora 186101861, 00000, "literal does not match format string"// *Cause: Literals i ...

随机推荐

  1. day23

    ## 复习 4.类与对象的语法class 类名: 代码块(一堆属性与方法)对象名 = 类名() 变量 | 函数 => 属性 | 方法:前者直接使用,通过所属者.语法调用 类会随所属文件加载而加载 ...

  2. psycopg2+postgis+pgAdmin4

    基于docker的postgres 部署见这篇 https://www.cnblogs.com/xuanmanstein/p/7742647.html 连接数据库 import psycopg2cla ...

  3. Oracle中国移动经典面试题(附代码跟两种答案)

    /*中国移动sql面试题: create table test(   id number(10) primary key,   type number(10) ,   t_id number(10), ...

  4. Java并发编程的艺术· 笔记(1)

    目录 1.volatile的原理 2.Synchonized 3.无锁-偏向锁-轻量级锁-重量级锁 4.Java实现原子操作 1.volatile的原理 如何保持可见性: 1)将当前处理器缓存行的数据 ...

  5. jquery tab切换

    首先引入jquery.js <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  6. D3比例尺

    D3中有个重要的概念就是比例尺.比例尺就是把一组输入域映射到输出域的函数.映射就是两个数据集之间元素相互对应的关系.比如输入是1,输出是100,输入是5,输出是10000,那么这其中的映射关系就是你所 ...

  7. licode测试

    https://github.com/lynckia/licode/tree/master/test 使用js模拟客户端调用,也可以使用mocha来进行同样的测试

  8. STL 小白学习(8) set 二叉树

    #include <iostream> using namespace std; #include <set> void printSet(set<int> s) ...

  9. 迁移 Emacs 的自定义设置

    在一台电脑上设置好了 Emacs 自定义的许多包和参数,想要便捷地把各项设置迁移到另一台目标电脑,其实是很简便的. 一般情况下,各个package位于 ~/.emacs.d/ 文件夹内,我们需要做的只 ...

  10. bind与继承 待研究

    class a { f() { console.log('a') } get f2() { console.log('f2') return (this['f'] = this.f.bind(this ...