how to give an object as argument for a method

Bertrand Geston bergeston at yahoo.fr
Wed Mar 13 03:44:16 EST 2002


I fully agree with Jeff : IMHO, it can only be a 'last chance' solution in
various situations (debug, plug-in development, test module...) but I advice
to never design an application from scratch on such an assumption or even
rely on that for business development.

B.

"Jeff Shannon" <jeff at ccvcorp.com> wrote in message
news:3C8E49ED.1C524CC1 at ccvcorp.com...
>
>
> Marco Herrn wrote:
>
> > [snip]
> > > ...  def sayNo(self):
> > > ...   caller = sys._getframe(1).f_locals['self']
> > this is the only line, that looks a bit ugly. It actually does the same
> > as when giving caller as parameter. So what can be a reason to implement
> > it this way? It looks much less readable to me. And I have to say I not
> > even understand it fully....
>
> Actually, it does *not* do the same as giving the caller as a parameter,
in
> general.  What this does, is that it gets the intepreter's stack frame in
which
> the current object was created.  In this case, that happens to be the
> Egg.__init__() frame.  It then searches this stack frame for a local name
> 'self';  if that name doesn't exist, this will throw an exception.  This
means
> that Spam can *only* be created from inside of another method which uses
the self
> convention;  you cannot create a Spam object globally or inside of a top
level
> function.  And, indeed, in the rare instances where a method uses some
name other
> than self to represent its bound instance, this will fail as well.  A
metaphor
> would be that, instead of simply being told who your parents are, you're
> searching through a phone book for a name that you *think* belongs to
them -- but
> you might have the name wrong, they might have an unlisted number, or they
might
> not even *be* in that phone book.  (Note also that even if it *does* work,
it's
> inappropriate to call the object found the "owner" or "caller" of the
current
> instance; it would be the *creator*, which is not necessarily the same
thing at
> all, especially when you consider object factories and such.)
>
> In short, this is black magic.  It might work, most of the time... but if
you
> turn your back on it, it's liable to blow up on you.  Use only with
caution, and
> only when you're *sure* that you know what you're asking for.  :)
>
> Jeff Shannon
> Technician/Programmer
> Credit International
>
>





More information about the Python-list mailing list