Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   How to identify the method that has called another method ? (http://www.velocityreviews.com/forums/t319856-how-to-identify-the-method-that-has-called-another-method.html)

Mars 07-18-2003 02:20 PM

How to identify the method that has called another method ?
 
Hi.

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 ? (and is this
a silly idea in general ?)

Regards,

Martin

Michael Hudson 07-18-2003 04:48 PM

Re: How to identify the method that has called another method ?
 
martin_a_clausen@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

Bengt Richter 07-19-2003 04:34 PM

Re: How to identify the method that has called another method ?
 
On 18 Jul 2003 07:20:18 -0700, martin_a_clausen@hotmail.com (Mars) wrote:

>Hi.
>
>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 ? (and is this
>a silly idea in general ?)


You don't say why __init__ should need to check. Are you re-using instances
and only want to do part of the init job if the factory re-uses instances
from a free list?

Why not just leave __init__ out and give your class an ordinary method
for initializing that won't be called except on purpose? And limit __init__
to normal on-creation initialization and don't call it directly. E.g. (untested!)

class MyClass(object):
def __init__(self):
self.init_on_creation = 'whatever needs setting once on creation only'
def myinit(self, whatever):
self.whatever = whatever # or whatever ;-)

def myfactory(something):
if freelist: instance = freelist.pop()
else: instance = MyClass()
instance.myinit(something)
return instance
....
directly = MyClass() # no automatic call to directly.myinit
factorymade = myfactory(123)

I'm sure you can think of variations from there.
HTH

Regards,
Bengt Richter


All times are GMT. The time now is 04:35 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.