• <tfoot id='EDSrB'></tfoot>

    • <bdo id='EDSrB'></bdo><ul id='EDSrB'></ul>

    <small id='EDSrB'></small><noframes id='EDSrB'>

    <legend id='EDSrB'><style id='EDSrB'><dir id='EDSrB'><q id='EDSrB'></q></dir></style></legend>
    <i id='EDSrB'><tr id='EDSrB'><dt id='EDSrB'><q id='EDSrB'><span id='EDSrB'><b id='EDSrB'><form id='EDSrB'><ins id='EDSrB'></ins><ul id='EDSrB'></ul><sub id='EDSrB'></sub></form><legend id='EDSrB'></legend><bdo id='EDSrB'><pre id='EDSrB'><center id='EDSrB'></center></pre></bdo></b><th id='EDSrB'></th></span></q></dt></tr></i><div id='EDSrB'><tfoot id='EDSrB'></tfoot><dl id='EDSrB'><fieldset id='EDSrB'></fieldset></dl></div>

      1. 递增 int 对象

        时间:2023-09-11

        <small id='NoGHe'></small><noframes id='NoGHe'>

      2. <tfoot id='NoGHe'></tfoot>

          • <legend id='NoGHe'><style id='NoGHe'><dir id='NoGHe'><q id='NoGHe'></q></dir></style></legend>

                <tbody id='NoGHe'></tbody>

                  <bdo id='NoGHe'></bdo><ul id='NoGHe'></ul>

                  <i id='NoGHe'><tr id='NoGHe'><dt id='NoGHe'><q id='NoGHe'><span id='NoGHe'><b id='NoGHe'><form id='NoGHe'><ins id='NoGHe'></ins><ul id='NoGHe'></ul><sub id='NoGHe'></sub></form><legend id='NoGHe'></legend><bdo id='NoGHe'><pre id='NoGHe'><center id='NoGHe'></center></pre></bdo></b><th id='NoGHe'></th></span></q></dt></tr></i><div id='NoGHe'><tfoot id='NoGHe'></tfoot><dl id='NoGHe'><fieldset id='NoGHe'></fieldset></dl></div>
                  本文介绍了递增 int 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  python 中有没有办法在适当的位置增加 int 对象,int 似乎没有实现 __iadd__ 所以 += 1 实际上返回一个新对象

                  Is there a way in python to increment int object in place, int doesn't seem to implement __iadd__ so += 1 actually returns a new object

                  >>> n=1
                  >>> id(n)
                  9788024
                  >>> n+=1
                  >>> id(n)
                  9788012
                  

                  我想要的是 n 保持指向同一个对象.

                  What I want is n to remain pointing to same object.

                  目的:我有从 int 派生的类,我想为该类实现 C 类型的 '++n' 运算符

                  Purpose: I have class derived from int and I want to implement C type '++n' operator for that class

                  结论:好的,因为 int 是不可变的,所以没有办法,看起来我将不得不编写我自己的类这样的东西

                  Conclusion: ok as int is immutable there is no way, looks like i will have to write my own class something like this

                  class Int(object):
                      def __init__(self, value):
                          self._decr = False
                          self.value = value
                  
                      def __neg__(self):
                          if self._decr:
                              self.value -= 1
                          self._decr = not self._decr
                          return self
                  
                      def __str__(self):
                          return str(self.value)
                  
                      def __cmp__(self, n):
                          return cmp(self.value, n)
                  
                      def __nonzero__(self):
                          return self.value
                  
                  n = Int(10)
                  while --n:
                      print n
                  

                  推荐答案

                  int 是不可变的,所以如果你想要一个可变的 int",你必须用所有 int 的方法构建你自己的类

                  ints are immutable, so you'll have to build your own class with all the int's methods if you want a "mutable int"

                  这篇关于递增 int 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:“OverflowError: Python int too large to convert to C long&q 下一篇:如何将 int 转换为十六进制字符串?

                  相关文章

                  最新文章

                  <legend id='uX5ai'><style id='uX5ai'><dir id='uX5ai'><q id='uX5ai'></q></dir></style></legend>
                  1. <tfoot id='uX5ai'></tfoot>

                  2. <i id='uX5ai'><tr id='uX5ai'><dt id='uX5ai'><q id='uX5ai'><span id='uX5ai'><b id='uX5ai'><form id='uX5ai'><ins id='uX5ai'></ins><ul id='uX5ai'></ul><sub id='uX5ai'></sub></form><legend id='uX5ai'></legend><bdo id='uX5ai'><pre id='uX5ai'><center id='uX5ai'></center></pre></bdo></b><th id='uX5ai'></th></span></q></dt></tr></i><div id='uX5ai'><tfoot id='uX5ai'></tfoot><dl id='uX5ai'><fieldset id='uX5ai'></fieldset></dl></div>

                    <small id='uX5ai'></small><noframes id='uX5ai'>

                      • <bdo id='uX5ai'></bdo><ul id='uX5ai'></ul>