python %r和%s分别在什么情况下使用?,python%r,看代码的时候一头雾水,不


看代码的时候一头雾水,不清楚什么情况下使用:

print "It write: %s." % qprint "It write: %r." % q

这二者有什么区别吗?

可以查看源码看具体区别。
在python的C语言实现版本,获取repr时会调用:

static PyObject *string_repr(PyObject *op){    return PyString_Repr(op, 1);}

它调用的PyString_Repr函数代码如下:

PyObject *PyString_Repr(PyObject *obj, int smartquotes){    register PyStringObject* op = (PyStringObject*) obj;    size_t newsize = 2 + 4 * Py_SIZE(op);    PyObject *v;    if (newsize > PY_SSIZE_T_MAX || newsize / 4 != Py_SIZE(op)) {        PyErr_SetString(PyExc_OverflowError,            "string is too large to make repr");        return NULL;    }    v = PyString_FromStringAndSize((char *)NULL, newsize);    if (v == NULL) {        return NULL;    }    else {        register Py_ssize_t i;        register char c;        register char *p;        int quote;        /* figure out which quote to use; single is preferred */        quote = '\'';        if (smartquotes &&            memchr(op->ob_sval, '\'', Py_SIZE(op)) &&            !memchr(op->ob_sval, '"', Py_SIZE(op)))            quote = '"';        p = PyString_AS_STRING(v);        *p++ = quote;        for (i = 0; i < Py_SIZE(op); i++) {            /* There's at least enough room for a hex escape               and a closing quote. */            assert(newsize - (p - PyString_AS_STRING(v)) >= 5);            c = op->ob_sval[i];            if (c == quote || c == '\\')                *p++ = '\\', *p++ = c;            else if (c == '\t')                *p++ = '\\', *p++ = 't';            else if (c == '\n')                *p++ = '\\', *p++ = 'n';            else if (c == '\r')                *p++ = '\\', *p++ = 'r';            else if (c < ' ' || c >= 0x7f) {                /* For performance, we don't want to call                   PyOS_snprintf here (extra layers of                   function call). */                sprintf(p, "\\x%02x", c & 0xff);                p += 4;            }            else                *p++ = c;        }        assert(newsize - (p - PyString_AS_STRING(v)) >= 1);        *p++ = quote;        *p = '\0';        if (_PyString_Resize(&v, (p - PyString_AS_STRING(v))))            return NULL;        return v;    }}

实际上repr对象就是一个string对象,对应python代码repr("a string").__class__返回的<type 'str'>也说明了这一点。
仔细看看源码可以发现,创建repr对象时需要传入string对象,而且repr对象的大小(占用内存空间)是string的4倍,其主要目的就是进行一些字符串格式化,如代码所见,repr将\,",\t等字符格式化成\\,\",\\t等。
简而言之,repr就是对string的一个格式化处理,即,返回的是一个格式化处理过的string(新对象)。
对应你的代码:

print "It write: %s." % qprint "It write: %r." % q

第一行将打印string对象q,第二行打印被repr处理后的string对象(非q)。
具体的打印显示实现可看源码,函数原型是:static int string_print(PyStringObject *op, FILE *fp, int flags)

上述的解释是字符串的默认实现,当然你也可以使用magic method:__repr__,__str__重载它们

s意义是字符串
r意义是使用repr,而不是str
%r 用来做 debug 比较好,因为它会显示变量的原始数据(raw data),而其它的符号则是用来向用户显示输出的。在《笨办法学习Python(第三版)》中有详细说明
比如

>>> print '%s, %s'%('one', 'two')one, two>>> print '%r, %r'%('one', 'two')'one', 'two'

个人习惯还是喜欢用format方法

%r是repr
%s就是str

比如\x27是单引号>>> print '%r'%'\x27' # 带括号的单引号"'" >>> print '%s'%'\x27' # 纯单引号'>>> class Example(object): ...     __repr__ = lambda cls: '<Demo>(repr)'...     __str__  = lambda cls: '<Demo>(str)'... >>> example = Example()>>> print '%s'%example<Demo>(str)>>> print '%r'%example<Demo>(repr)

编橙之家文章,

评论关闭