Is there any way to trace life cycle of objects during python code running? I know about sys.settrace function for setting callback, I can stop on each function call or line of code, but how can I get access to "living" objects during this? I would like to trace program so that I can do random checks after each stop, as if I really added code at stop place in sources.
1 Answer
Up to some extend can be done by gc (garbage collector) module in CPython.
Garbage Collector There are few functions using object as parameter and can provide some information regarding object existence:
gc.get_referrers(*objs)
gc.get_referents(*objs)
gc.is_tracked(obj)
To simply check if object exists use 'try' as described here
2 Comments
Ganesh Sittampalam
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
asf las
The link was given to python documentation. Actually it would be better for me to comment on OP question but i don't have rights yet to do that. I will paste some information from link.
func1and 1 line allready runned in which objectobj1of some type was created. I want to check this obj1, for example get results fromdir(obj1)or check class ofobj1import pdb; pdb.set_trace()? That gives you an interactive debugger at that point. Object creation is itself not traceable, put traces in their__init__or__new__functions itself.pdb.set_trace()to all functions in program, which i am tracing. Also i am interesting not in interactive tracing, i want to write code which extract needed data during execution.