How to identify the method that has called another method ?

Discussion in 'Python' started by Mars, Jul 18, 2003.

  1. Mars

    Mars Guest

    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
    Mars, Jul 18, 2003
    #1
    1. Advertising

  2. (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
    Michael Hudson, Jul 18, 2003
    #2
    1. Advertising

  3. On 18 Jul 2003 07:20:18 -0700, (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
    Bengt Richter, Jul 19, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Magne Bergfjord
    Replies:
    1
    Views:
    446
  2. Apricot
    Replies:
    4
    Views:
    509
    velthuijsen
    Apr 16, 2004
  3. Weng Tianxiang
    Replies:
    6
    Views:
    581
    glen herrmannsfeldt
    Sep 12, 2007
  4. S_K
    Replies:
    6
    Views:
    1,157
    Robert Dunlop
    Nov 8, 2007
  5. aidy

    Has a method been called?

    aidy, Sep 7, 2006, in forum: Ruby
    Replies:
    4
    Views:
    98
    Mauricio Fernandez
    Sep 7, 2006
Loading...

Share This Page