Erklären Sie die folgende Attribut -SuchePython

Python-Programme
Anonymous
 Erklären Sie die folgende Attribut -Suche

Post by Anonymous »

Code: Select all

[nav] In [532]: class Test(io.BufferedIOBase):
...:     def __init__(self, f):
...:         self.f = f
...:
...:     def __getattr__(self, name):
...:         print("getattr: searching for: ", name)
...:         #try:
...:         #    v = getattr(self.f, name)
...:         #except Exception:
...:         #    print("    found nothing!")
...:         #    raise
...:         #print("    found:     ", v)
...:         #return v
...:         #return 22
...:         raise AttributeError(name)
...:
...:     def __getattribute__(self, name):
...:         print("1.getattribute@self: searching for: ", name, flush=True)
...:         try:
...:             v = super().__getattribute__(name)
...:         except Exception as e:
...:             print("    1.exception: ", e)
...:             print("    1.found nothing!", flush=True)
...:             pass
...:         else:
...:             print("    1.found:     ", v, flush=True)
...:             return v
...:         print("[email protected]: searching for: ", name, flush=True)
...:         #f = super().__getattribute__("f")
...:         f = object.__getattribute__(self, "f")
...:         try:
...:             v = getattr(f, name)
...:         except Exception as e:
...:             print("    2.exception: ", e)
...:             print("    2.found nothing!", flush=True)
...:             raise
...:         else:
...:             print("    2.found:     ", v, flush=True)
...:             return v
...:         print("ERROR")
...:
...: f1 = open(p, "rb")
...: f2 = Test(f1)
...: print("\nTest 01:")
...: v = f2.closed
...: print("result: ", v)
...: print("\nTest 02:")
...: v = f2.qwer
...: print("result: ", v)

Test 01:
1.getattribute@self: searching for:  closed
1.getattribute@self: searching for:  __IOBase_closed
1.exception:  'Test' object has no attribute '__IOBase_closed'
1.found nothing!
[email protected]: searching for:  __IOBase_closed
2.exception:  '_io.BufferedReader' object has no attribute '__IOBase_closed'
2.found nothing!
getattr: searching for:  __IOBase_closed
1.found:      False
result:  False

Test 02:
1.getattribute@self: searching for:  qwer
1.exception:  'Test' object has no attribute 'qwer'
1.found nothing!
[email protected]: searching for:  qwer
2.exception:  '_io.BufferedReader' object has no attribute 'qwer'
2.found nothing!
getattr: searching for:  qwer
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[532], line 49
47 print("result: ", v)
48 print("\nTest 02:")
---> 49 v = f2.qwer
50 print("result: ", v)

Cell In[532], line 15, in Test.__getattr__(self, name)
6 print("getattr: searching for: ", name)
7 #try:
8 #    v = getattr(self.f, name)
9 #except Exception:
(...)
13 #return v
14 #return 22
---> 15 raise AttributeError(name)

AttributeError: qwer
< /code>
folgende Object_getAtTribute () < /code>, das ist meiner Meinung nach passiert: < /p>
[list]
[*]closed
entschlossen in die Nicht-Daten-Eigenschaft von IOBase. ist eigentlich _iobase__closed pro Namens-Mangling-Regeln.
[/list]
Jetzt werden die Schritte seltsam. "3">
Mein __GetAttribute __ scheint nach __iobase_closed zu suchen (was nicht existiert) und nicht _iobase__closed . Dies kann ich Mismpraines zwischen dem Python- und CPython -Code zuschreiben. AttributeError . Und doch wird der Wert von _iobase__closed zurückgegeben. Ich kann dies nur erklären, wenn Python eine andere GetAttr* -Funktion weiter oben in der MRO aufruft.>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post