Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b7f1afe

Browse files
committed
Change the default repr() and str() of class instance objects to look
like <modulename.classname instance at ...> (to match the repr() of class objects.
1 parent 5c38bf6 commit b7f1afe

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

Objects/classobject.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,13 +655,21 @@ instance_repr(inst)
655655
if (func == NULL) {
656656
char buf[140];
657657
PyObject *classname = inst->in_class->cl_name;
658+
PyObject *mod = PyDict_GetItemString(
659+
inst->in_class->cl_dict, "__module__");
658660
char *cname;
659661
if (classname != NULL && PyString_Check(classname))
660662
cname = PyString_AsString(classname);
661663
else
662664
cname = "?";
663665
PyErr_Clear();
664-
sprintf(buf, "<%.100s instance at %lx>", cname, (long)inst);
666+
if (mod == NULL || !PyString_Check(mod))
667+
sprintf(buf, "<?.%.100s instance at %lx>",
668+
cname, (long)inst);
669+
else
670+
sprintf(buf, "<%.50s.%.50s instance at %lx>",
671+
PyString_AsString(mod),
672+
cname, (long)inst);
665673
return PyString_FromString(buf);
666674
}
667675
res = PyEval_CallObject(func, (PyObject *)NULL);

0 commit comments

Comments
 (0)