#!/usr/bin/env python3
# encoding: utf-8
'''
%r是repr %s就是str
查看源码看具体区别。 在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." % q
print "It write: %r." % q
第一行将打印string对象q,第二行打印被repr处理后的string对象(非q)。
具体的打印显示实现可看源码,函数原型是:static int string_print(PyStringObject *op, FILE *fp, int flags)
上述的解释是字符串的默认实现,当然你也可以使用magic method:__repr__,__str__重载它们
'''
test = """hello,world"""
print("this is test1: %s" %test ) # this is test1: hello,world
print("this is test2: %r" %test) # this is test2: 'hello,world'
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)
print('%s, %s'%('one', 'two')) # one, two
print('%r, %r'%('one', 'two')) # 'one', 'two'
python中%s与%r的区别
最新推荐文章于 2024-08-22 18:28:19 发布