""" Ex 9.1 in "A primer on..." Demonstrate inheritance by creating an "empty" subclass, and use the builtin function dir and the __dict__ attribute to inspect its contents. """ import numpy as np class Line: def __init__(self, c0, c1): self.c0, self.c1 = c0, c1 def __call__(self, x): return self.c0 + self.c1 * x def table(self, L, R, n): """Return a table with n points for L <= x <= R.""" s = '' for x in np.linspace(L, R, n): y = self(x) s += f'{x:12g} {y:12g}\n' return s class Parabola0(Line): pass p = Parabola0(1,1) #The builtin function dir lists all the contents of an object: print("All contents of Parabola instance 'p':") print(dir(p)) """The attribute __dict__ is automatically created by Python, and is a dictionary containing the defined attributes of an instance.""" print("Attributes of Parabola instance 'p':") print(p.__dict__) """ Terminal> python dir_subclass.py All contents of Parabola instance 'p': ['__call__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'c0', 'c1', 'table'] Attributes of Parabola instance 'p': {'c0': 1, 'c1': 1} """