gpt4 book ai didi

python - 最大递归深度超出 getter Django

转载 作者:行者123 更新时间:2023-11-30 22:48:55 26 4
gpt4 key购买 nike

我有一些带有特定属性的自定义 getter 的类:

class Item(a, b, c, d, models.Model):
title = Field()
description = Field()
a = Field()
_custom_getter = ['title','description']

def __getattribute__(self, name):
if name in self.custom_getter:
return 'xxx'
else:
return super(Item, self).__getattribute__(name)

此代码引发RunetimeError:调用Python对象时超出最大递归深度但是当我使用这段代码时:

class Item(a, b, c, d, models.Model):
title = Field()
description = Field()
a = Field()

def __getattribute__(self, name):
custom_getter = ['title','description']
if name in custom_getter:
return 'xxx'
else:
return super(Item, self).__getattribute__(name)

一切都如我所愿。我的第一段代码的错误在哪里?

最佳答案

因为当您执行 self.custom_getter 时,__getattribute__ 会被调用。您可以使用 self.__dict__ 来实现此目的。了解更多 How is the __getattribute__ method used?

class Item(a, b, c, d, models.Model):
title = Field()
description = Field()
a = Field()
custom_getter = ['title','description']

def __getattribute__(self, name):
if name in self.__dict__['custom_getter']:
return 'xxx'
else:
return super(Item, self).__getattribute__(name)

关于python - 最大递归深度超出 getter Django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39993144/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com