鱼C论坛

 找回密码
 立即注册
查看: 1814|回复: 0

[学习笔记] 042魔法方法:算术运算

[复制链接]
发表于 2017-7-18 22:03:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1.工厂函数
  1. >>> type(len)
  2. <class 'builtin_function_or_method'>
  3. >>> type(dir)
  4. <class 'builtin_function_or_method'>
  5. >>> type(int)
  6. <class 'type'>
  7. >>> type(list)
  8. <class 'type'>
  9. >>> class C:
  10.         pass

  11. >>> type(C)
  12. <class 'type'>
  13. >>> a = int('123')
  14. >>> a
  15. 123
  16. >>> b = int('456')
  17. >>> a + b
  18. 579
  19. >>> class New_int(int):
  20.         def __add__(self, other):
  21.                 return int.__sub__(self, other)
  22.         def __sub__(self, other):
  23.                 return int.__add__(self, other)

  24.        
  25. >>> a = New_int(3)
  26. >>> b = New_int(5)
  27. >>> a + b
  28. -2
  29. >>> a - b
  30. 8
  31. >>> class Try_int(int):
  32.         def __add__(self, other):
  33.                 return self + other
  34.         def __sub__(self, other):
  35.                 return self - other

  36.        
  37. >>> a = Try_int(3)
  38. >>> b = Try_int(5)
  39. >>> a + b
  40. Traceback (most recent call last):
  41.   File "<pyshell#26>", line 1, in <module>
  42.     a + b
  43.   File "<pyshell#23>", line 3, in __add__
  44.     return self + other
  45.   File "<pyshell#23>", line 3, in __add__
  46.     return self + other
  47.   File "<pyshell#23>", line 3, in __add__
  48.     return self + other
  49.   File "<pyshell#23>", line 3, in __add__
  50.     return self + other
  51.   File "<pyshell#23>", line 3, in __add__
  52.     return self + other
  53.   File "<pyshell#23>", line 3, in __add__
  54.     return self + other
  55.   File "<pyshell#23>", line 3, in __add__
  56.     return self + other
  57.   File "<pyshell#23>", line 3, in __add__
  58.     return self + other
  59.   File "<pyshell#23>", line 3, in __add__
  60.     return self + other
  61.   File "<pyshell#23>", line 3, in __add__
  62.     return self + other
  63.   File "<pyshell#23>", line 3, in __add__
  64.     return self + other
  65.   File "<pyshell#23>", line 3, in __add__
  66.     return self + other
  67.   File "<pyshell#23>", line 3, in __add__
  68.     return self + other
  69.   File "<pyshell#23>", line 3, in __add__
  70.     return self + other
  71.   File "<pyshell#23>", line 3, in __add__
  72.     return self + other
  73.   File "<pyshell#23>", line 3, in __add__
  74.     return self + other
  75.   File "<pyshell#23>", line 3, in __add__
  76.     return self + other
  77.   File "<pyshell#23>", line 3, in __add__
  78.     return self + other
  79.   File "<pyshell#23>", line 3, in __add__
  80.     return self + other
  81.   File "<pyshell#23>", line 3, in __add__
  82.     return self + other
  83.   File "<pyshell#23>", line 3, in __add__
  84.     return self + other
  85.   File "<pyshell#23>", line 3, in __add__
  86.     return self + other
  87.   File "<pyshell#23>", line 3, in __add__
  88.     return self + other
  89.   File "<pyshell#23>", line 3, in __add__
  90.     return self + other
  91.   File "<pyshell#23>", line 3, in __add__
  92.     return self + other
  93.   File "<pyshell#23>", line 3, in __add__
  94.     return self + other
  95.   File "<pyshell#23>", line 3, in __add__
  96.     return self + other
  97.   File "<pyshell#23>", line 3, in __add__
  98.     return self + other
  99.   File "<pyshell#23>", line 3, in __add__
  100.     return self + other
  101.   File "<pyshell#23>", line 3, in __add__
  102.     return self + other
  103.   File "<pyshell#23>", line 3, in __add__
  104.     return self + other
  105.   File "<pyshell#23>", line 3, in __add__
  106.     return self + other
  107.   File "<pyshell#23>", line 3, in __add__
  108.     return self + other
  109.   File "<pyshell#23>", line 3, in __add__
  110.     return self + other
  111.   File "<pyshell#23>", line 3, in __add__
  112.     return self + other
  113.   File "<pyshell#23>", line 3, in __add__
  114.     return self + other
  115.   File "<pyshell#23>", line 3, in __add__
  116.     return self + other
  117.   File "<pyshell#23>", line 3, in __add__
  118.     return self + other
  119.   File "<pyshell#23>", line 3, in __add__
  120.     return self + other
  121.   File "<pyshell#23>", line 3, in __add__
  122.     return self + other
  123.   File "<pyshell#23>", line 3, in __add__
  124.     return self + other
  125.   File "<pyshell#23>", line 3, in __add__
  126.     return self + other
  127.   File "<pyshell#23>", line 3, in __add__
  128.     return self + other
  129.   File "<pyshell#23>", line 3, in __add__
  130.     return self + other
  131.   File "<pyshell#23>", line 3, in __add__
  132.     return self + other
  133.   File "<pyshell#23>", line 3, in __add__
  134.     return self + other
  135.   File "<pyshell#23>", line 3, in __add__
  136.     return self + other
  137.   File "<pyshell#23>", line 3, in __add__
  138.     return self + other
  139.   File "<pyshell#23>", line 3, in __add__
  140.     return self + other
  141.   File "<pyshell#23>", line 3, in __add__
  142.     return self + other
  143.   File "<pyshell#23>", line 3, in __add__
  144.     return self + other
  145.   File "<pyshell#23>", line 3, in __add__
  146.     return self + other
  147.   File "<pyshell#23>", line 3, in __add__
  148.     return self + other
  149.   File "<pyshell#23>", line 3, in __add__
  150.     return self + other
  151.   File "<pyshell#23>", line 3, in __add__
  152.     return self + other
  153.   File "<pyshell#23>", line 3, in __add__
  154.     return self + other
  155.   File "<pyshell#23>", line 3, in __add__
  156.     return self + other
  157.   File "<pyshell#23>", line 3, in __add__
  158.     return self + other
  159.   File "<pyshell#23>", line 3, in __add__
  160.     return self + other
  161.   File "<pyshell#23>", line 3, in __add__
  162.     return self + other
  163.   File "<pyshell#23>", line 3, in __add__
  164.     return self + other
  165.   File "<pyshell#23>", line 3, in __add__
  166.     return self + other
  167.   File "<pyshell#23>", line 3, in __add__
  168.     return self + other
  169.   File "<pyshell#23>", line 3, in __add__
  170.     return self + other
  171.   File "<pyshell#23>", line 3, in __add__
  172.     return self + other
  173.   File "<pyshell#23>", line 3, in __add__
  174.     return self + other
  175.   File "<pyshell#23>", line 3, in __add__
  176.     return self + other
  177.   File "<pyshell#23>", line 3, in __add__
  178.     return self + other
  179.   File "<pyshell#23>", line 3, in __add__
  180.     return self + other
  181.   File "<pyshell#23>", line 3, in __add__
  182.     return self + other
  183.   File "<pyshell#23>", line 3, in __add__
  184.     return self + other
  185.   File "<pyshell#23>", line 3, in __add__
  186.     return self + other
  187.   File "<pyshell#23>", line 3, in __add__
  188.     return self + other
  189.   File "<pyshell#23>", line 3, in __add__
  190.     return self + other
  191.   File "<pyshell#23>", line 3, in __add__
  192.     return self + other
  193.   File "<pyshell#23>", line 3, in __add__
  194.     return self + other
  195.   File "<pyshell#23>", line 3, in __add__
  196.     return self + other
  197.   File "<pyshell#23>", line 3, in __add__
  198.     return self + other
  199.   File "<pyshell#23>", line 3, in __add__
  200.     return self + other
  201.   File "<pyshell#23>", line 3, in __add__
  202.     return self + other
  203.   File "<pyshell#23>", line 3, in __add__
  204.     return self + other
  205.   File "<pyshell#23>", line 3, in __add__
  206.     return self + other
  207.   File "<pyshell#23>", line 3, in __add__
  208.     return self + other
  209.   File "<pyshell#23>", line 3, in __add__
  210.     return self + other
  211.   File "<pyshell#23>", line 3, in __add__
  212.     return self + other
  213.   File "<pyshell#23>", line 3, in __add__
  214.     return self + other
  215.   File "<pyshell#23>", line 3, in __add__
  216.     return self + other
  217.   File "<pyshell#23>", line 3, in __add__
  218.     return self + other
  219.   File "<pyshell#23>", line 3, in __add__
  220.     return self + other
  221.   File "<pyshell#23>", line 3, in __add__
  222.     return self + other
  223.   File "<pyshell#23>", line 3, in __add__
  224.     return self + other
  225.   File "<pyshell#23>", line 3, in __add__
  226.     return self + other
  227.   File "<pyshell#23>", line 3, in __add__
  228.     return self + other
  229.   File "<pyshell#23>", line 3, in __add__
  230.     return self + other
  231.   File "<pyshell#23>", line 3, in __add__
  232.     return self + other
  233.   File "<pyshell#23>", line 3, in __add__
  234.     return self + other
  235.   File "<pyshell#23>", line 3, in __add__
  236.     return self + other
  237.   File "<pyshell#23>", line 3, in __add__
  238.     return self + other
  239.   File "<pyshell#23>", line 3, in __add__
  240.     return self + other
  241.   File "<pyshell#23>", line 3, in __add__
  242.     return self + other
  243.   File "<pyshell#23>", line 3, in __add__
  244.     return self + other
  245.   File "<pyshell#23>", line 3, in __add__
  246.     return self + other
  247.   File "<pyshell#23>", line 3, in __add__
  248.     return self + other
  249.   File "<pyshell#23>", line 3, in __add__
  250.     return self + other
  251.   File "<pyshell#23>", line 3, in __add__
  252.     return self + other
  253.   File "<pyshell#23>", line 3, in __add__
  254.     return self + other
  255.   File "<pyshell#23>", line 3, in __add__
  256.     return self + other
  257.   File "<pyshell#23>", line 3, in __add__
  258.     return self + other
  259.   File "<pyshell#23>", line 3, in __add__
  260.     return self + other
  261.   File "<pyshell#23>", line 3, in __add__
  262.     return self + other
  263.   File "<pyshell#23>", line 3, in __add__
  264.     return self + other
  265.   File "<pyshell#23>", line 3, in __add__
  266.     return self + other
  267.   File "<pyshell#23>", line 3, in __add__
  268.     return self + other
  269.   File "<pyshell#23>", line 3, in __add__
  270.     return self + other
  271.   File "<pyshell#23>", line 3, in __add__
  272.     return self + other
  273.   File "<pyshell#23>", line 3, in __add__
  274.     return self + other
  275.   File "<pyshell#23>", line 3, in __add__
  276.     return self + other
  277.   File "<pyshell#23>", line 3, in __add__
  278.     return self + other
  279.   File "<pyshell#23>", line 3, in __add__
  280.     return self + other
  281.   File "<pyshell#23>", line 3, in __add__
  282.     return self + other
  283.   File "<pyshell#23>", line 3, in __add__
  284.     return self + other
  285.   File "<pyshell#23>", line 3, in __add__
  286.     return self + other
  287.   File "<pyshell#23>", line 3, in __add__
  288.     return self + other
  289.   File "<pyshell#23>", line 3, in __add__
  290.     return self + other
  291.   File "<pyshell#23>", line 3, in __add__
  292.     return self + other
  293.   File "<pyshell#23>", line 3, in __add__
  294.     return self + other
  295.   File "<pyshell#23>", line 3, in __add__
  296.     return self + other
  297.   File "<pyshell#23>", line 3, in __add__
  298.     return self + other
  299.   File "<pyshell#23>", line 3, in __add__
  300.     return self + other
  301.   File "<pyshell#23>", line 3, in __add__
  302.     return self + other
  303.   File "<pyshell#23>", line 3, in __add__
  304.     return self + other
  305.   File "<pyshell#23>", line 3, in __add__
  306.     return self + other
  307.   File "<pyshell#23>", line 3, in __add__
  308.     return self + other
  309.   File "<pyshell#23>", line 3, in __add__
  310.     return self + other
  311.   File "<pyshell#23>", line 3, in __add__
  312.     return self + other
  313.   File "<pyshell#23>", line 3, in __add__
  314.     return self + other
  315.   File "<pyshell#23>", line 3, in __add__
  316.     return self + other
  317.   File "<pyshell#23>", line 3, in __add__
  318.     return self + other
  319.   File "<pyshell#23>", line 3, in __add__
  320.     return self + other
  321.   File "<pyshell#23>", line 3, in __add__
  322.     return self + other
  323.   File "<pyshell#23>", line 3, in __add__
  324.     return self + other
  325.   File "<pyshell#23>", line 3, in __add__
  326.     return self + other
  327.   File "<pyshell#23>", line 3, in __add__
  328.     return self + other
  329.   File "<pyshell#23>", line 3, in __add__
  330.     return self + other
  331.   File "<pyshell#23>", line 3, in __add__
  332.     return self + other
  333.   File "<pyshell#23>", line 3, in __add__
  334.     return self + other
  335.   File "<pyshell#23>", line 3, in __add__
  336.     return self + other
  337.   File "<pyshell#23>", line 3, in __add__
  338.     return self + other
  339.   File "<pyshell#23>", line 3, in __add__
  340.     return self + other
  341.   File "<pyshell#23>", line 3, in __add__
  342.     return self + other
  343.   File "<pyshell#23>", line 3, in __add__
  344.     return self + other
  345.   File "<pyshell#23>", line 3, in __add__
  346.     return self + other
  347.   File "<pyshell#23>", line 3, in __add__
  348.     return self + other
  349.   File "<pyshell#23>", line 3, in __add__
  350.     return self + other
  351.   File "<pyshell#23>", line 3, in __add__
  352.     return self + other
  353.   File "<pyshell#23>", line 3, in __add__
  354.     return self + other
  355.   File "<pyshell#23>", line 3, in __add__
  356.     return self + other
  357.   File "<pyshell#23>", line 3, in __add__
  358.     return self + other
  359.   File "<pyshell#23>", line 3, in __add__
  360.     return self + other
  361.   File "<pyshell#23>", line 3, in __add__
  362.     return self + other
  363.   File "<pyshell#23>", line 3, in __add__
  364.     return self + other
  365.   File "<pyshell#23>", line 3, in __add__
  366.     return self + other
  367.   File "<pyshell#23>", line 3, in __add__
  368.     return self + other
  369.   File "<pyshell#23>", line 3, in __add__
  370.     return self + other
  371.   File "<pyshell#23>", line 3, in __add__
  372.     return self + other
  373.   File "<pyshell#23>", line 3, in __add__
  374.     return self + other
  375.   File "<pyshell#23>", line 3, in __add__
  376.     return self + other
  377.   File "<pyshell#23>", line 3, in __add__
  378.     return self + other
  379.   File "<pyshell#23>", line 3, in __add__
  380.     return self + other
  381.   File "<pyshell#23>", line 3, in __add__
  382.     return self + other
  383.   File "<pyshell#23>", line 3, in __add__
  384.     return self + other
  385.   File "<pyshell#23>", line 3, in __add__
  386.     return self + other
  387.   File "<pyshell#23>", line 3, in __add__
  388.     return self + other
  389.   File "<pyshell#23>", line 3, in __add__
  390.     return self + other
  391.   File "<pyshell#23>", line 3, in __add__
  392.     return self + other
  393.   File "<pyshell#23>", line 3, in __add__
  394.     return self + other
  395.   File "<pyshell#23>", line 3, in __add__
  396.     return self + other
  397.   File "<pyshell#23>", line 3, in __add__
  398.     return self + other
  399.   File "<pyshell#23>", line 3, in __add__
  400.     return self + other
  401.   File "<pyshell#23>", line 3, in __add__
  402.     return self + other
  403.   File "<pyshell#23>", line 3, in __add__
  404.     return self + other
  405.   File "<pyshell#23>", line 3, in __add__
  406.     return self + other
  407.   File "<pyshell#23>", line 3, in __add__
  408.     return self + other
  409.   File "<pyshell#23>", line 3, in __add__
  410.     return self + other
  411.   File "<pyshell#23>", line 3, in __add__
  412.     return self + other
  413.   File "<pyshell#23>", line 3, in __add__
  414.     return self + other
  415.   File "<pyshell#23>", line 3, in __add__
  416.     return self + other
  417.   File "<pyshell#23>", line 3, in __add__
  418.     return self + other
  419.   File "<pyshell#23>", line 3, in __add__
  420.     return self + other
  421.   File "<pyshell#23>", line 3, in __add__
  422.     return self + other
  423.   File "<pyshell#23>", line 3, in __add__
  424.     return self + other
  425.   File "<pyshell#23>", line 3, in __add__
  426.     return self + other
  427.   File "<pyshell#23>", line 3, in __add__
  428.     return self + other
  429.   File "<pyshell#23>", line 3, in __add__
  430.     return self + other
  431.   File "<pyshell#23>", line 3, in __add__
  432.     return self + other
  433.   File "<pyshell#23>", line 3, in __add__
  434.     return self + other
  435.   File "<pyshell#23>", line 3, in __add__
  436.     return self + other
  437.   File "<pyshell#23>", line 3, in __add__
  438.     return self + other
  439.   File "<pyshell#23>", line 3, in __add__
  440.     return self + other
  441.   File "<pyshell#23>", line 3, in __add__
  442.     return self + other
  443.   File "<pyshell#23>", line 3, in __add__
  444.     return self + other
  445.   File "<pyshell#23>", line 3, in __add__
  446.     return self + other
  447.   File "<pyshell#23>", line 3, in __add__
  448.     return self + other
  449.   File "<pyshell#23>", line 3, in __add__
  450.     return self + other
  451.   File "<pyshell#23>", line 3, in __add__
  452.     return self + other
  453.   File "<pyshell#23>", line 3, in __add__
  454.     return self + other
  455.   File "<pyshell#23>", line 3, in __add__
  456.     return self + other
  457.   File "<pyshell#23>", line 3, in __add__
  458.     return self + other
  459.   File "<pyshell#23>", line 3, in __add__
  460.     return self + other
  461.   File "<pyshell#23>", line 3, in __add__
  462.     return self + other
  463.   File "<pyshell#23>", line 3, in __add__
  464.     return self + other
  465.   File "<pyshell#23>", line 3, in __add__
  466.     return self + other
  467.   File "<pyshell#23>", line 3, in __add__
  468.     return self + other
  469.   File "<pyshell#23>", line 3, in __add__
  470.     return self + other
  471.   File "<pyshell#23>", line 3, in __add__
  472.     return self + other
  473.   File "<pyshell#23>", line 3, in __add__
  474.     return self + other
  475.   File "<pyshell#23>", line 3, in __add__
  476.     return self + other
  477.   File "<pyshell#23>", line 3, in __add__
  478.     return self + other
  479.   File "<pyshell#23>", line 3, in __add__
  480.     return self + other
  481.   File "<pyshell#23>", line 3, in __add__
  482.     return self + other
  483.   File "<pyshell#23>", line 3, in __add__
  484.     return self + other
  485.   File "<pyshell#23>", line 3, in __add__
  486.     return self + other
  487.   File "<pyshell#23>", line 3, in __add__
  488.     return self + other
  489.   File "<pyshell#23>", line 3, in __add__
  490.     return self + other
  491.   File "<pyshell#23>", line 3, in __add__
  492.     return self + other
  493.   File "<pyshell#23>", line 3, in __add__
  494.     return self + other
  495.   File "<pyshell#23>", line 3, in __add__
  496.     return self + other
  497.   File "<pyshell#23>", line 3, in __add__
  498.     return self + other
  499.   File "<pyshell#23>", line 3, in __add__
  500.     return self + other
  501.   File "<pyshell#23>", line 3, in __add__
  502.     return self + other
  503.   File "<pyshell#23>", line 3, in __add__
  504.     return self + other
  505.   File "<pyshell#23>", line 3, in __add__
  506.     return self + other
  507.   File "<pyshell#23>", line 3, in __add__
  508.     return self + other
  509.   File "<pyshell#23>", line 3, in __add__
  510.     return self + other
  511.   File "<pyshell#23>", line 3, in __add__
  512.     return self + other
  513.   File "<pyshell#23>", line 3, in __add__
  514.     return self + other
  515.   File "<pyshell#23>", line 3, in __add__
  516.     return self + other
  517.   File "<pyshell#23>", line 3, in __add__
  518.     return self + other
  519.   File "<pyshell#23>", line 3, in __add__
  520.     return self + other
  521.   File "<pyshell#23>", line 3, in __add__
  522.     return self + other
  523.   File "<pyshell#23>", line 3, in __add__
  524.     return self + other
  525.   File "<pyshell#23>", line 3, in __add__
  526.     return self + other
  527.   File "<pyshell#23>", line 3, in __add__
  528.     return self + other
  529.   File "<pyshell#23>", line 3, in __add__
  530.     return self + other
  531.   File "<pyshell#23>", line 3, in __add__
  532.     return self + other
  533.   File "<pyshell#23>", line 3, in __add__
  534.     return self + other
  535.   File "<pyshell#23>", line 3, in __add__
  536.     return self + other
  537.   File "<pyshell#23>", line 3, in __add__
  538.     return self + other
  539.   File "<pyshell#23>", line 3, in __add__
  540.     return self + other
  541.   File "<pyshell#23>", line 3, in __add__
  542.     return self + other
  543.   File "<pyshell#23>", line 3, in __add__
  544.     return self + other
  545.   File "<pyshell#23>", line 3, in __add__
  546.     return self + other
  547.   File "<pyshell#23>", line 3, in __add__
  548.     return self + other
  549.   File "<pyshell#23>", line 3, in __add__
  550.     return self + other
  551.   File "<pyshell#23>", line 3, in __add__
  552.     return self + other
  553.   File "<pyshell#23>", line 3, in __add__
  554.     return self + other
  555.   File "<pyshell#23>", line 3, in __add__
  556.     return self + other
  557.   File "<pyshell#23>", line 3, in __add__
  558.     return self + other
  559.   File "<pyshell#23>", line 3, in __add__
  560.     return self + other
  561.   File "<pyshell#23>", line 3, in __add__
  562.     return self + other
  563.   File "<pyshell#23>", line 3, in __add__
  564.     return self + other
  565.   File "<pyshell#23>", line 3, in __add__
  566.     return self + other
  567.   File "<pyshell#23>", line 3, in __add__
  568.     return self + other
  569.   File "<pyshell#23>", line 3, in __add__
  570.     return self + other
  571.   File "<pyshell#23>", line 3, in __add__
  572.     return self + other
  573.   File "<pyshell#23>", line 3, in __add__
  574.     return self + other
  575.   File "<pyshell#23>", line 3, in __add__
  576.     return self + other
  577.   File "<pyshell#23>", line 3, in __add__
  578.     return self + other
  579.   File "<pyshell#23>", line 3, in __add__
  580.     return self + other
  581.   File "<pyshell#23>", line 3, in __add__
  582.     return self + other
  583.   File "<pyshell#23>", line 3, in __add__
  584.     return self + other
  585.   File "<pyshell#23>", line 3, in __add__
  586.     return self + other
  587.   File "<pyshell#23>", line 3, in __add__
  588.     return self + other
  589.   File "<pyshell#23>", line 3, in __add__
  590.     return self + other
  591.   File "<pyshell#23>", line 3, in __add__
  592.     return self + other
  593.   File "<pyshell#23>", line 3, in __add__
  594.     return self + other
  595.   File "<pyshell#23>", line 3, in __add__
  596.     return self + other
  597.   File "<pyshell#23>", line 3, in __add__
  598.     return self + other
  599.   File "<pyshell#23>", line 3, in __add__
  600.     return self + other
  601.   File "<pyshell#23>", line 3, in __add__
  602.     return self + other
  603.   File "<pyshell#23>", line 3, in __add__
  604.     return self + other
  605.   File "<pyshell#23>", line 3, in __add__
  606.     return self + other
  607.   File "<pyshell#23>", line 3, in __add__
  608.     return self + other
  609.   File "<pyshell#23>", line 3, in __add__
  610.     return self + other
  611.   File "<pyshell#23>", line 3, in __add__
  612.     return self + other
  613.   File "<pyshell#23>", line 3, in __add__
  614.     return self + other
  615.   File "<pyshell#23>", line 3, in __add__
  616.     return self + other
  617.   File "<pyshell#23>", line 3, in __add__
  618.     return self + other
  619.   File "<pyshell#23>", line 3, in __add__
  620.     return self + other
  621.   File "<pyshell#23>", line 3, in __add__
  622.     return self + other
  623.   File "<pyshell#23>", line 3, in __add__
  624.     return self + other
  625.   File "<pyshell#23>", line 3, in __add__
  626.     return self + other
  627.   File "<pyshell#23>", line 3, in __add__
  628.     return self + other
  629.   File "<pyshell#23>", line 3, in __add__
  630.     return self + other
  631.   File "<pyshell#23>", line 3, in __add__
  632.     return self + other
  633.   File "<pyshell#23>", line 3, in __add__
  634.     return self + other
  635.   File "<pyshell#23>", line 3, in __add__
  636.     return self + other
  637.   File "<pyshell#23>", line 3, in __add__
  638.     return self + other
  639.   File "<pyshell#23>", line 3, in __add__
  640.     return self + other
  641.   File "<pyshell#23>", line 3, in __add__
  642.     return self + other
  643.   File "<pyshell#23>", line 3, in __add__
  644.     return self + other
  645.   File "<pyshell#23>", line 3, in __add__
  646.     return self + other
  647.   File "<pyshell#23>", line 3, in __add__
  648.     return self + other
  649.   File "<pyshell#23>", line 3, in __add__
  650.     return self + other
  651.   File "<pyshell#23>", line 3, in __add__
  652.     return self + other
  653.   File "<pyshell#23>", line 3, in __add__
  654.     return self + other
  655.   File "<pyshell#23>", line 3, in __add__
  656.     return self + other
  657.   File "<pyshell#23>", line 3, in __add__
  658.     return self + other
  659.   File "<pyshell#23>", line 3, in __add__
  660.     return self + other
  661.   File "<pyshell#23>", line 3, in __add__
  662.     return self + other
  663.   File "<pyshell#23>", line 3, in __add__
  664.     return self + other
  665.   File "<pyshell#23>", line 3, in __add__
  666.     return self + other
  667.   File "<pyshell#23>", line 3, in __add__
  668.     return self + other
  669.   File "<pyshell#23>", line 3, in __add__
  670.     return self + other
  671.   File "<pyshell#23>", line 3, in __add__
  672.     return self + other
  673.   File "<pyshell#23>", line 3, in __add__
  674.     return self + other
  675.   File "<pyshell#23>", line 3, in __add__
  676.     return self + other
  677.   File "<pyshell#23>", line 3, in __add__
  678.     return self + other
  679.   File "<pyshell#23>", line 3, in __add__
  680.     return self + other
  681.   File "<pyshell#23>", line 3, in __add__
  682.     return self + other
  683.   File "<pyshell#23>", line 3, in __add__
  684.     return self + other
  685.   File "<pyshell#23>", line 3, in __add__
  686.     return self + other
  687.   File "<pyshell#23>", line 3, in __add__
  688.     return self + other
  689.   File "<pyshell#23>", line 3, in __add__
  690.     return self + other
  691.   File "<pyshell#23>", line 3, in __add__
  692.     return self + other
  693.   File "<pyshell#23>", line 3, in __add__
  694.     return self + other
  695.   File "<pyshell#23>", line 3, in __add__
  696.     return self + other
  697.   File "<pyshell#23>", line 3, in __add__
  698.     return self + other
  699.   File "<pyshell#23>", line 3, in __add__
  700.     return self + other
  701.   File "<pyshell#23>", line 3, in __add__
  702.     return self + other
  703.   File "<pyshell#23>", line 3, in __add__
  704.     return self + other
  705. RuntimeError: maximum recursion depth exceeded
  706. >>> class Try_int(int):
  707.         def __add__(self, other):
  708.                 return int(self) + int(other)
  709.         def __sub__(self, other):
  710.                 return int(self) - int(other)

  711.        
  712. >>> a = Try_int(3)
  713. >>> b = Try_int(5)
  714. >>> a + b
  715. 8
复制代码

2.算术运算符
__add__(self, other)                  定义加法的行为:+
__sub__(self, other)                  定义减法的行为:-
__mul__(self, other)                  定义加法的行为:*
__truediv__(self, other)             定义加法的行为:/
__floordiv__(self, other)            定义加法的行为://
__mod__(self, other)                 定义取模算法的行为:%
__divmod__(self, other)            定义当被divmod()调用时的行为
__pow__(self, other[,modulo])  定义当被power()调用或**运算时的行为
__lshift__(self, other)                 定义按位左移位的行为:<<
__rshift__(self, other)                 定义按位右移位的行为:>>
__and__(self, other)                   定义按位与操作的行为: &
__xor__(self, other)                    定义按位异或操作的行为: ^
__or__(self, other)                      定义按位或操作的行为: |

评分

参与人数 1鱼币 +2 收起 理由
小甲鱼 + 2 支持楼主!

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-28 18:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表