简体   繁体   English

如何找出对象的实例化位置?

[英]How can I find out where an object has been instantiated?

I'm trying to find out, at runtime, where an object has been instantiated, as this would enable providing a very useful error message to users of my library. 我试图在运行时找出实例化对象的位置,因为这样可以向我的库用户提供非常有用的错误消息。

Suppose we have the following code: 假设我们有以下代码:

import mylib

obj = mylib.MyClass()

obj is then passed to an instance of another class from mylib , and proceeds on a wonderful journey. 然后将objmylib传递给另一个类的实例,并继续进行一次精彩的旅程。 Somewhere along the line, obj causes something bad to happen, and I'd like to point the user to where obj was instantiated. 在某个地方, obj导致一些不好的事情发生,我想将用户指向obj被实例化的地方。

I was hoping I could use the inspect module to find out in which file and at what line number obj was instantiated. 我希望我可以使用inspect模块找出obj被实例化的文件和行号。 Unfortunately, the inspect.getsourcefile and inspect.getsourcelines do not support instances. 不幸的是, inspect.getsourcefileinspect.getsourcelines不支持实例。 Is there a technical reason why this is not supported? 是否存在技术原因导致不支持此操作?

Is there another way I can obtain the data I'm looking for? 有没有其他方法可以获得我正在寻找的数据?

You could record this information in your class's constructor: 您可以在类的构造函数中记录此信息:

import traceback

class MyClass(object):
   def __init__(self):
       self.traceback = traceback.extract_stack()[-2]

obj = MyClass()

print 'Instantiated in {0}:{1}'.format(*obj.traceback)

may be you want this?? 可能你想要这个?

In [1]: def checkinstance(prohibitedclass):
   ...:     import sys
   ...:     final_result=set()
   ...:     for x in sys._getframe(1).f_locals:
   ...:         if isinstance(sys._getframe(1).f_locals.get(x),prohibitedclass):
   ...:             final_str="instance of class %s is at: %s"%(prohibitedclass,sys._getframe(1).f_locals.get(x))
   ...:             final_result.add(final_str)
   ...:     return list(final_result)

In [2]: class not_allowedclass(object):
   ...:     pass

In [3]: checkinstance(not_allowedclass)
Out[3]: []

In [4]: nk=not_allowedclass()

In [5]: nk1=not_allowedclass()

In [6]: checkinstance(not_allowedclass)
Out[6]: 
["instance of class <class '__main__.not_allowedclass'> is at: <__main__.not_allowedclass object at 0x102dcdb10>",
 "instance of class <class '__main__.not_allowedclass'> is at: <__main__.not_allowedclass object at 0x102dcda90>"]

In [7]: nk
Out[7]: <__main__.not_allowedclass at 0x102dcda90>

In [8]: nk1
Out[8]: <__main__.not_allowedclass at 0x102dcdb10>

In [9]: 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎样才能知道选择了哪个按钮 - how can I find out what button has been selected 如何找出Django中是否创建了新对象? - How to find out if there has been new object created in Django? 如何找出/打印生成pickle文件的协议版本 - How can I find out / print with which version of the protocol a pickle file has been generated 实例化后如何将上下文信息添加到LoggerAdapter实例? - How can I add contextual information to a LoggerAdapter instance AFTER it has been instantiated? 根据对象的实例化位置,对对象实例化执行其他操作 - Do additional operations on object instantation, based on where the object has been instantiated Python:对象在哪里被实例化以及如何在对象创建过程中传递参数? - Python: Where is the object getting instantiated and how can I pass arguments during object creation? 如何模拟在构造函数中实例化的对象? - How can I mock an object instantiated in the constructor? 如何找到实例化的 Python class object 的输入参数? - How do I find the input parameters of an instantiated Python class object? 实例化后向对象添加标志的pythonic方法是什么? - What is the pythonic way to add a flag to an object after it has been instantiated? 如何确定卫星是否进行了机动? - How can I find out if a satellite has performed maneuver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM