[Python-Dev] No __mod__ on str?

Guido van Rossum guido@python.org
Wed, 25 Sep 2002 19:46:05 -0400


> Here's the code for PyNumber_Remainder:
> 
>     PyObject *
>     PyNumber_Remainder(PyObject *v, PyObject *w)
>     {
>         if (PyString_Check(v))
>                 return PyString_Format(v, w);
>     #ifdef Py_USING_UNICODE
>         else if (PyUnicode_Check(v))
>                 return PyUnicode_Format(v, w);
>     #endif
>         return binary_op(v, w, NB_SLOT(nb_remainder), "%");
>     }
> 
> Is there any good reason why str.__mod__ != PyString_Format?  I want to
> make a subclass of str that overrides the format operator.  I guess one
> side effect would be that PyNumber_Check(astring) would start returning
> true.

Good catch.  I think this is a relic from before str and unicode were
subclassable.

> Should I file a bug saying "can't override __mod__ on str and unicode
> subclasses"?  I guess the fix would be to check for nb_remainder first
> and then fallback to PyString_Format or PyUnicode_Format.

Yes please.  If you can provide a fix, make it a patch.  Anyway assign
it to me.

--Guido van Rossum (home page: http://www.python.org/~guido/)