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

    <tfoot id='hglSl'></tfoot>

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

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

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

      Python:调用复制函数时调用 Python 对象时超出了最大递归深度

      Python: maximum recursion depth exceeded while calling a Python object when calling copy function(Python:调用复制函数时调用 Python 对象时超出了最大递归深度)
        <i id='poIlS'><tr id='poIlS'><dt id='poIlS'><q id='poIlS'><span id='poIlS'><b id='poIlS'><form id='poIlS'><ins id='poIlS'></ins><ul id='poIlS'></ul><sub id='poIlS'></sub></form><legend id='poIlS'></legend><bdo id='poIlS'><pre id='poIlS'><center id='poIlS'></center></pre></bdo></b><th id='poIlS'></th></span></q></dt></tr></i><div id='poIlS'><tfoot id='poIlS'></tfoot><dl id='poIlS'><fieldset id='poIlS'></fieldset></dl></div>

            • <small id='poIlS'></small><noframes id='poIlS'>

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

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

                <bdo id='poIlS'></bdo><ul id='poIlS'></ul>
                  <tbody id='poIlS'></tbody>
                本文介绍了Python:调用复制函数时调用 Python 对象时超出了最大递归深度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个类Particle,它有一些参数和属性,如下所示.但是,当它确实到达函数设置器的位置并执行 copy() 函数时,我收到错误消息:RuntimeError:调用 Python 对象时超出最大递归深度.我尝试了不同的选项,例如 deepcopy()import sys sys.setrecursionlimit(10000) ,但都没有奏效……有人知道吗?这是我的代码:

                I have a class Particle which has some parameters and attributes, as you can see below. But, when it does get to the function setter for position, and it executes the copy() function, I get the error message : RuntimeError: maximum recursion depth exceeded while calling a Python object. I've tried different options, like deepcopy(), or import sys sys.setrecursionlimit(10000) , but none of them worked... Does anyone have any idea? This is my code:

                def initCost(n):
                    a = random.randint(0,10)              #gram.
                    b = random.randint(0,5)             #price
                    return [random.randint(0,a*b) for i in range(n)]
                
                costs = initCost(10)
                
                class Particle:
                    def __init__(self, n, maxWeight):
                        self.position = [random.randint(0,1) for i in range(n)]  #position
                        self.velocity = [0 for i in range(n)]                    #velocity
                        #self.fit = self.fitness(self.position)
                        self.bp = self.position.copy()                           #best position
                        self.bf = self.fit                                 #best fitness
                        self.evaluate()
                
                    def fit(self, x):
                        fitt = 0
                        for i in range(len(x)-1):
                            if (x[i] == 1):
                                fitt = fitt + costs[i]
                        return fitt
                
                    def evaluate(self):
                        """ evaluates the particle """
                        self.fitness = self.fit(self.position)
                
                    @property
                    def position(self):
                        return self.position
                
                    @property
                    def bp(self):
                        return self.bp
                
                    @property
                    def bf(self):
                        return self.bf
                
                    @position.setter
                    def position(self, newPosition):
                        self.position = newPosition.copy()
                        #self.position = newPosition[:]
                        self.evaluate()
                        # automatic update of particle's memory
                        if (self.fit<self.bf):
                            self.bp = self.position
                            self.bf  = self.fit
                

                推荐答案

                看起来您正在尝试使用 position 作为属性名称和支持它的普通属性的名称.例如,

                It looks like you're trying to use position as the name of both the property and the ordinary attribute backing it. For example,

                @position.setter
                def position(self, newPosition):
                    self.position = newPosition.copy()
                #   ^^^^^^^^^^^^^^^
                

                设置 self.position 的尝试将使用您定义的设置器!同样,

                This attempt to set self.position will use the setter you're defining! Similarly,

                @property
                def position(self):
                    return self.position
                

                这个 getter 只是调用自己!

                This getter just calls itself!

                尝试在 position 属性定义中使用 self.position 不会绕过该属性.如果您想要一个支持属性的常规"属性,请将其命名为其他名称,例如 self._position 或其他名称.

                Trying to use self.position inside the position property definition won't bypass the property. If you want a "regular" attribute backing the property, call it something else, like self._position or something.

                这篇关于Python:调用复制函数时调用 Python 对象时超出了最大递归深度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                How to extend Python class init(如何扩展 Python 类初始化)
                What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                Initialize list with same bool value(使用相同的布尔值初始化列表)
                setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                <tfoot id='hszAn'></tfoot>
                    <tbody id='hszAn'></tbody>

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

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