'overriding a function implementation
I have the following files:
t.py:
def foo():
print("foo")
folder/t.py:
def foo():
print("bar")
folder/main.py:
import t
if __name__ == "__main__":
t.foo()
when I run it on the command line with python -m folder.main it prints "foo" but I actually want to import folder/t.py so that it prints "bar" instead. How can I do that?
Solution 1:[1]
You can import as, so like import folder.t as ft then run ft.foo()
Solution 2:[2]
Use the following code in main.py and run the command python -m folder.main.
sys.path.append() will add the folder to the system path so that it can find the module t in that folder.
import sys
sys.path.append('.\\folder\\')
from folder import t
if __name__ == "__main__":
t.foo()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | anarchy |
| Solution 2 | ksohan |
