str has other methods like __rmod__, __rsub__, or __rmul __
int had __radd__ method
Strange, and I would like to know why.
An example of a radd beeing that is called the first time you add a HAS method:
>>> class STR(str): ... def __radd__(self, other): ... print "jeje" ... return other.__add__(self) ... >>> 'aaaaa' + STR('bbbbb') jeje 'aaaaabbbbb'
__radd__used when the first member of the addition does not implement __add__. In the case, the intaddition is well defined by its mathematical definition, so int is trying to force another term to a number.
__radd__
__add__
int
c str, there is no such clearly defined meaning, and the python developers decided that there was no obvious need to have something + a "string".
str
, . , , , __radd__. __rmul__ :
__rmul__
>>> 4 * 'abc' 'abcabcabcabc'
, __rmod__, - . , , "Hello %s" % 'world'. . , __rsub__ str.
__rmod__
"Hello %s" % 'world'
__rsub__
, , - .
Source: https://habr.com/ru/post/1731678/More articles:Regex соответствует первому файлу в файле архивного файла rar. - c#Regular expression to match only the first file in a set of RAR files - ruby | fooobar.comRegex matches the first file in rar archive file installed in Python - pythonThe fastest way to pick up Joomla? - joomlaAccess to a subset of Python class functions - pythonScala project not automatically created in Eclipse - eclipseWill GTK + timeout calls be called in strict order? - c ++How to create an editor in the new Extensibilty VS2010 editor - visual-studioRun JAR in PHP - phpEC2 - Can I run Windows on an EC2 Linux instance? - windowsAll Articles