Advertisement
Python253

mro_attribute_demo

May 3rd, 2024
743
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.70 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: mro_attribute_demo.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. This script demonstrates the usage of the __mro__ attribute in Python, which stands for Method Resolution Order.
  9. It defines three classes: A, B, and C. Class C inherits from both classes A and B, showcasing multiple inheritance.
  10.  
  11. Requirements:
  12. - Python 3.x
  13.  
  14. Functions:
  15. - line(): A utility function to print divider lines for better readability.
  16.  
  17. Usage:
  18. 1. Run the script using Python 3.x interpreter.
  19. 2. The script will print the method resolution order (__mro__) for classes A, B, and C.
  20. 3. It will also perform hasattr checks to verify if instances of classes A, B, and C have the __mro__ attribute.
  21.  
  22. Additional Notes:
  23. - The __mro__ attribute provides insight into the sequence of classes that Python will search to resolve method calls in a class hierarchy.
  24. - Instances of classes do not inherit from 'type', hence they do not have the __mro__ attribute.
  25. - This script serves as a basic example to understand the method resolution order and multiple inheritance in Python.
  26. - It's recommended to run the script in an environment with Python 3.x installed to ensure compatibility.
  27. """
  28.  
  29. class A: pass
  30. class B: pass
  31.  
  32. class C(A, B): pass
  33.  
  34. # Function to print divider lines
  35. def line():
  36.     print("-" * 94)
  37.    
  38. line()
  39. print("\n\t\t\t:: Explanation for A.__mro__ ::\n")
  40. line()
  41. print("A.__mro__ represents the method resolution order for class A.\n")
  42. print("- It shows the sequence of classes that Python will search to resolve method calls for class A.")
  43. print("- In this case, A.__mro__ is:\n\n\t", A.__mro__)
  44. print()
  45.  
  46. line()
  47. print("\n\t\t\t:: Explanation for B.__mro__ ::\n")
  48. line()
  49. print("B.__mro__ represents the method resolution order for class B.\n")
  50. print("- It shows the sequence of classes that Python will search to resolve method calls for class B.")
  51. print("- In this case, B.__mro__ is:\n\n\t", B.__mro__)
  52. print()
  53.  
  54. line()
  55. print("\n\t\t\t:: Explanation for C.__mro__ ::\n")
  56. line()
  57. print("C.__mro__ represents the method resolution order for class C.\n")
  58. print("- It shows the sequence of classes that Python will search to resolve method calls for class C.")
  59. print("- In this case, C.__mro__ is:\n\n\t", C.__mro__)
  60. print()
  61.  
  62. a = A()
  63. b = B()
  64. c = C()
  65.  
  66. line()
  67. print("\n\t\t\t:: Explanation for hasattr checks ::\n")
  68. line()
  69. print("The following assertions check if the objects have the __mro__ attribute.\n")
  70. print("\t\t\tFor instance a:", hasattr(a, '__mro__'))
  71. print("\t\t\tFor instance b:", hasattr(b, '__mro__'))
  72. print("\t\t\tFor instance c:", hasattr(c, '__mro__'))
  73. print("\nSince instances do not inherit from 'type', they do not have this attribute.\n")
  74. line()
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement