Question: 5) __module__ attribute as a class (data type), object, with __str__ to display its type. Create two modules (Q5_animals.py and Q5_test_animals.py) For the Q5_animals.py, enter
5) __module__ attribute as a class (data type), object, with __str__ to display its type. Create two modules (Q5_animals.py and Q5_test_animals.py) For the Q5_animals.py, enter the following codes and run it (no import) ''' Import this module (Q5_animals.py) to Q5_use_animals_module_mclass.py and run it in class. comment out the __str__(self) and run it again... ''' class Cat: att1 = "Cat...." def speak(self): print("meow....") def __str__(self): return self.att1 class Dog: att1 = "Dog****" def speak(self): print('woof *** ') def __str__(self): return self.att1
For Q5_test_animal.py , enter the following codes and run it. import Q5_animals as a1 c1 = a1.Cat() print(f' what is c1 module? {c1.__module__}') # Q5_animals c1.speak() print(f"c1 is a {type(c1)} ") # animals.Cat (Cat is a class name) print(f' what is c1 ? {c1}') # show just the cat object without the module name d1 = a1.Dog() print(f' what is d1 module? {d1.__module__}') d1.speak() print(f' what is d1 ? {d1}') Result: what is c module? Q5_animals meow.... c1 is a
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
