1

Let say I have a file a/b/c.py with following content.

import mymodule
a = mymodule.MyClass()
print(a.scriptpath)

I want a.scriptpath to print a/b/c.py. Essentially, what I am asking: Is there a way for MyClass.__init__ or mymodule.__init__.py to know from which file they are being called?

1 Answer 1

2

Why not just instantiate the class with that information:

a = mymodule.MyClass(__file__)
Sign up to request clarification or add additional context in comments.

3 Comments

Is it possible to do it without asking the user? inspect module looks promising.
Yes. In that case, there are answers here: stackoverflow.com/questions/7150998/…
I ended up using inspect module. Thanks @costaparas .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.