Python Forum
Why A will be printed twice in the picture
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why A will be printed twice in the picture
#1
[Image: 6cZhJ.png]
[Image: YjF2i.png]
class A:
	x = 1
	def __getattr__(self,name):
		print("__getattr__ of A")
	def __getattribute__(self,name):
		print("__getattribute__ of A")
		return super().__getattribute__(name)

class B(A):
	x = 2
	def __getattr__(self,name):
		print("__getattr__ of B")
	def __getattribute__(self,name):
		print("__getattribute__ of B")
		a = A()
		return a.__getattribute__(name)
b = B()
print(b.x)
I want to ask Why A will be printed twice.I have tried debug ,but after that I am more puzzled.Thanks.
Reply
#2
the __getattribute__ method is called twice on line 16: first implicitly to get the __getattribute__ method itself, and then explicitly by your code.
Reply
#3
(Jul-25-2018, 11:19 AM)stranac Wrote: the __getattribute__ method is called twice on line 16: first implicitly to get the __getattribute__ method itself, and then explicitly by your code.
Thank you.But I have an another problem,
class B(A):
        x = 2
        def __getattr__(self,name):
                print("__getattr__ of B")
        def __getattribute__(self,name):
                print("__getattribute__ of B")
                return object().__getattribute__(name)

        
b = B()
b.x
why the output is:
__getattribute__ of B
__getattr__ of B
Reply
#4
In this case, __getattribute__ gets called first, and raises an AttributeError, because x is an attribute of the class, not the instance.
When this happens, __getattr__ gets called to get the class attribute.

You should read https://docs.python.org/3/reference/datamodel.html to get a better understanding of how all of this stuff works.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  coma separator is printed on a new line for some reason tester_V 4 495 Feb-02-2024, 06:06 PM
Last Post: tester_V
  openpyxl insert picture problem cools0607 2 1,563 May-03-2023, 06:48 AM
Last Post: cools0607
  How can histogram bins be separated and reduce number of labels printed on x-axis? cadena 1 892 Sep-07-2022, 09:47 AM
Last Post: Larz60+
  How to increase the size of a png picture for the heatmap of the correlation? lulu43366 9 3,533 Oct-06-2021, 04:15 PM
Last Post: deanhystad
  Picture changing triggered by GPIO q_nerk 2 2,590 Dec-14-2020, 03:32 PM
Last Post: DeaD_EyE
  How to scrolling Picture in x axis kalihotname 1 2,258 Jun-16-2020, 12:18 PM
Last Post: DeaD_EyE
  Reverse printed words hindubiceps 7 3,004 Apr-21-2020, 05:19 PM
Last Post: deanhystad
  I get "None" at the end of my printed result. dyshkant 3 2,664 Sep-06-2019, 06:31 PM
Last Post: dyshkant
  bytes not being printed as expected Skaperen 2 2,377 Aug-27-2019, 05:33 AM
Last Post: Skaperen
  Change linenumber and filename printed in exceptions like #line in C kryptomatrix 2 2,678 Jul-12-2019, 06:01 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020