instance creatiion as an "event"

Alex Martelli aleax at aleax.it
Sat Apr 12 05:50:30 EDT 2003


<posted & mailed>

Arthur wrote:

> I am trying to re-engineer, as lightly as possible, a Python app I
> wrote, to be make it more functional when used at the interactive prompt.
> 
> The only thing I seem to be missing is some kind of "hook" to trigger a
> general update cycle of all existing class instances upon the creation
> of a new class instance at the interactive prompt

I'm somewhat confused, I think -- is your problem:

[A] how do I distinguish whether an operation is being performed "at
    the interactive prompt" or elsewhere?

and/or is it:

[B] how do I find out when an instance of a class has been created (quite
    apart from the issue of whether it's created at the interactive prompt
    or within some module),

and/or is it:

[C] how do I find out all the existing instances of a given class at any
    given point (during an interactive session), whether that is the time
    of the creation of a new instance, or not

Each of these tasks is quite feasible -- some trivially easy (B), some
devilishly hard and tricky (A), some not too bad (C).  But unless you
clarify which one (or more) of these tasks you want to perform, it's
quite hard to offer you appropriate suggestions -- I wouldn't want to
get into the intricacies of possible architectures to meet [A] (which
depend on why exactly you want to distinguish between interactive and
non-interactive stuff -- so, I'd need more information on what you're
trying to achieve, to supply maximally helpful information) unless [A]
is indeed what you need.


> The ideas I come up with to explore do not seem very straight forward -
> though some variation of one or another of them will probably work. But
> am generally weak on introspection issues and am wondering, before
> embarking done a crooked road:
> 
> Am I missing a something straight forward.

What about "the need to specify precisely what you're trying to
accomplish" (and, in the case of a "what" that is particularly subtle
and "black magic" oriented, maybe the "why" too, in order to allow
experts to suggest possible alternative architectures...).

[B] is easy: instance creation is accompanied by calls to the class's
__new__ staticmethod (which returns the new instance object) and to the
__init__ method (to initialize the newly created instance).  You can
piggyback your operations on either or both of these (or, if you want
to not touch your class's source code, you can use a custom metaclass
and operate in the __call__ method of the metaclass).

[C] is reasonably easy: you need the class (or its custom metaclass)
to keep a container of all its extant instances in order to be able
to iterate on them.  As I've already seen suggested, weak references
are what you normally want (if your class is a newstyle one that uses
__slots__, this may be a problem), and a weakref.WeakValueDictionary
(with id(...) of each instance as the key and the instance as the value)
is more often than not the handiest way to accomplish this.

[A] is devilishly hard because distinguishing between "things that
happen at the interactive prompt" and "things that happen elsewhere"
is not normally needed, and it's hard to see exactly what you want
to accomplish with it.  Suppose your module foo.py is:

class Foo:
    "whatever"

def makFoo(): return Foo()

and at the interactive prompt you have:

>>> a = Foo()
>>> b = makFoo()
>>> def xx(): return Foo()
...
>>> c = xx()

which of these calls to Foo counts as being "at the interactive
prompt", and which don't?  If all of them count, can you give
examples of instance creations that you want to consider are NOT
"happening at the interactive prompt"?  Or clarify the real purpose
of the distinction so that we can perhaps suggest alternatives?


Alex





More information about the Python-list mailing list