How do I get the filepath for a class in Python?

前端 未结 3 1801
执笔经年
执笔经年 2020-12-07 23:44

Given a class C in Python, how can I determine which file the class was defined in? I need something that can work from either the class C, or from an instance off C.

<
3条回答
  •  时光说笑
    2020-12-08 00:17

    You can use the inspect module, like this:

    import inspect
    inspect.getfile(C.__class__)
    

提交回复
热议问题