finding name of instances created
André
andre.roberge at gmail.com
Fri Jan 21 21:33:58 EST 2005
Steven Bethard wrote:
> André wrote:
> > Using the method suggested by Steven Bethard, I *almost* got it
working
> > the way I would like.
[snip]
>
> It looks like you want PrivateClass.dict updated every time that
> globals() is updated.
yes, that is what I would like to do.
>You can just use globals directly instead:
>
> py> class PrivateClass(object):
> ... def __init__(self, globals):
> ... self.globals = globals
> ... def apparently_simple_method(self):
> ... for name, value in self.globals.iteritems():
> ... if value is self:
> ... print "instance %s called not so simple" % name
> ...
> py> def public_class():
> ... return PrivateClass(globals())
> ...
> py> alpha = public_class()
> py> alpha.apparently_simple_method()
> instance alpha called not so simple
> py> beta = public_class()
> py> beta.apparently_simple_method()
> instance beta called not so simple
>
That's exactly what I was looking for; thank you!
> On the other hand, the iteration in
> PrivateClass.apparently_simple_method has a very bad code smell...
>
I'm not sure what you mean...
Is it because it makes use of information that is
exterior to the class, which is not passed as a parameter
to the method?
[feel free to ignore this question if you want; you have
already helped me tremendously!!!]
André
More information about the Python-list
mailing list