I looked at a similar question but it does not really answer the question that I have. Say I have the following code (overly simplified to highlight only my question).
class A:
def __init__(self,x):
self.val = x
a = A(4)
print a.val
This code resides in a file someones_class.py
. I now want to import and use class A
in my program without modifying someones_class.py
. If I do from someones_class import A
, python would still execute the script lines in the file.
Question: Is there a way to just import class A
without the last two lines getting executed?
I know about if __name__ == '__main__'
thing but I do not have the option of modifying someones_class.py
file as it is obtained only after my program starts executing.