I am new to Python and I am trying to find a way to add new methods to pre-existing classes in Python. For example, to add a .print()
method to the list class.
I know I can create a new class that inherits the list class and add this method like so:
import builtins
class list(list):
def print(self):
builtins.print(self)
But this doesn't modify the pre-existing 'list' class. I can do assignments like this: trial_list = list([3,4,6])
but not like this: trial_list = [3,4,6].
Also, is there a way to view the actual content of the list class besides dir()
and help()
?