How to identify the method that has called another method ?
Michael Hudson
mwh at python.net
Fri Jul 18 12:48:03 EDT 2003
martin_a_clausen at hotmail.com (Mars) writes:
> I am using Python 2.2.3 and new-style classes. I want to implement a
> static factory method to build objects for me. My plan is to have
> __init__ check that it has been called from said factory method and
> not directly. Is there a elegant way of achieving this ?
No. sys.getframe(1).f_code.co_name might be a start.
> (and is this a silly idea in general ?)
I've always disliked trying to disallow this kind of abuse -- it's
very hard to make it impossible, and I think you're better off just
documenting the restrictions.
Note that you might want to investigate __new__() by the sounds of
it...
Cheers,
M.
--
31. Simplicity does not precede complexity, but follows it.
-- Alan Perlis, http://www.cs.yale.edu/homes/perlis-alan/quotes.html
More information about the Python-list
mailing list