How can we add methods to a class that we imported from another code file in python?
Asked
Active
Viewed 373 times
0
-
In Python, you can dynamically add methods to classes directly:" `MyClass.some_method = some_method`. Or perhaps, as the answers here suggest and the linked duplicate addresses, you can just use inheritance. – juanpa.arrivillaga Feb 26 '22 at 03:40
2 Answers
0
You can extend the class like this:
class a:
def __init__(self):
self.value = "42"
class b(a):
def printValue(self):
print(self.value)
b().printValue()

kpie
- 9,588
- 5
- 28
- 50
0
from other_library import ThatClass
class NewClass(ThatClass):
def new_method(self):
# Implement here
pass

aminrd
- 4,300
- 4
- 23
- 45