Python\uuuGetAttribute\uuuuuu回退到\uuuuGetAttr__

Python\uuuGetAttribute\uuuuuu回退到\uuuuGetAttr__,python,Python,我遇到一种情况,getattribute回退到getattr,然后再次调用getattribute 如何再次调用当前的getattribute?我很困惑 class Count(object): def __init__(self,mymin,mymax): self.mymin=mymin self.mymax=mymax self.current=None def __getattr__(self, item):

我遇到一种情况,getattribute回退到getattr,然后再次调用getattribute

如何再次调用当前的getattribute?我很困惑

class Count(object):

    def __init__(self,mymin,mymax):
        self.mymin=mymin
        self.mymax=mymax
        self.current=None

    def __getattr__(self, item):
            print("akhjhd")
            self.__dict__[item]=0
            return 0

    def __getattribute__(self, item):
        print("this is called first")
        if item.startswith('cur'):
            print("this raised an error")
            raise AttributeError
        print("This will execute as well")
        return object.__getattribute__(self,item)


obj1 = Count(1,10)
print(obj1.mymin)
print(obj1.mymax)
print(obj1.current)
控制台输出:

this is called first
This will execute as well
1
this is called first
This will execute as well
10
this is called first
this raised an error
akhjhd
this is called first
This will execute as well
0

  • 调用
    getattr
    是因为
    getattribute
    引发
    AttributeError
  • self.\uuuu dict\uuuu
    调用对
    getattribute
清洁代码并添加
打印(项目)
以使其更清晰:

class Count(object):
    def __init__(self):
        self.current = None

    def __getattr__(self, item):
        print("in getattr")
        self.__dict__[item] = 0
        return 0

    def __getattribute__(self, item):
        print(item)
        print("in __getattribute__ 1")
        if item.startswith('cur'):
            print("starts with 'cur'")
            raise AttributeError
        print("in __getattribute__ 2")
        return object.__getattribute__(self, item)


obj1 = Count()
print(obj1.current)
输出

current
in __getattribute__ 1
starts with 'cur'
in getattr
__dict__
in __getattribute__ 1
in __getattribute__ 2
0

  • 调用
    getattr
    是因为
    getattribute
    引发
    AttributeError
  • self.\uuuu dict\uuuu
    调用对
    getattribute
清洁代码并添加
打印(项目)
以使其更清晰:

class Count(object):
    def __init__(self):
        self.current = None

    def __getattr__(self, item):
        print("in getattr")
        self.__dict__[item] = 0
        return 0

    def __getattribute__(self, item):
        print(item)
        print("in __getattribute__ 1")
        if item.startswith('cur'):
            print("starts with 'cur'")
            raise AttributeError
        print("in __getattribute__ 2")
        return object.__getattribute__(self, item)


obj1 = Count()
print(obj1.current)
输出

current
in __getattribute__ 1
starts with 'cur'
in getattr
__dict__
in __getattribute__ 1
in __getattribute__ 2
0

您需要咨询python

摘录如下:

无条件调用以实现类实例的属性访问。如果该类还定义了
\uuuuuGetAttr\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu()
,则后者将不会被调用,除非
\uuuuuuuuuuuuuuuuuuuuuuuuGetAttribute\uuuuu

我在你的代码中看到:

if item.startswith('cur'):
    print("this raised an error")
    raise AttributeError

所以我认为您是故意这么做的

您需要咨询python

摘录如下:

无条件调用以实现类实例的属性访问。如果该类还定义了
\uuuuuGetAttr\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu()
,则后者将不会被调用,除非
\uuuuuuuuuuuuuuuuuuuuuuuuGetAttribute\uuuuu

我在你的代码中看到:

if item.startswith('cur'):
    print("this raised an error")
    raise AttributeError

所以我认为你是故意这么做的

getattr
之所以被调用,是因为
getattribute
引发了
AttributeError
同意,但再次调用了,为什么?因为
self.\uuuu dict\uuuuu
第二次调用它,我添加了一个示例……感谢DeepSpace
getattr
之所以调用它,是因为
getattribute
引发了
AttributeError
同意,但再次调用了\uuuuuuuu\getattribute\uu,为什么会这样?因为我第二次添加答案时,self.\uuuu dict\uuuu.\uuu
调用了它,并添加了一个示例……感谢Deepspace如果我没有getattr方法并在getattribute中返回,为什么getattribute中的第一次打印会被一次又一次地打印(意味着getattribute的递归)……这怎么会不止一次被呼叫?@Pravin我无法重现这种行为。如果我注释掉了
getattr
,那么
getattribute
中的
AttributeError
就会传播。如果我没有getattr方法并在getattribute中返回,为什么在getattribute中第一次打印会被反复打印(getattribute的平均递归)……这怎么会不止一次被呼叫?@Pravin我无法重现这种行为。如果我将
getattr
注释掉,则
getattribute
中的
AttributeError
将传播。