staticmethod and namespaces

darnzen darnzen at
Fri Feb 26 16:42:32 CET 2010

On Feb 26, 1:12 am, Steven D'Aprano <st... at REMOVE-THIS-> wrote:
> On Thu, 25 Feb 2010 21:07:55 -0800, darnzen wrote:
> > Having an odd problem that I solved, but wondering if its the best
> > solution (seems like a bit of a hack).
> > First off, I'm using an external DLL that requires static callbacks, but
> > because of this, I'm losing instance info.
> [...]
> > How can I get at the app instance (currently I'm storing that along with
> > the class instance in the constructor)? Is there another way to do this
> > that's not such a hack?
> Pass the instance explicitly:
> >>> class K(object):
> ...     @staticmethod
> ...     def static(self, x, y):
> ...         print self, x, y
> ...>>> k = K()
> >>> k.static(k, 1, 2)
> <__main__.K object at 0xb7c2544c> 1 2
> --
> Steven

Unfortunately, since the callback is from the DLL which I didn't
write, I can not change what is passed to the function. Also, the DLL
has no idea about my local namespace. The DLL typically will pass back
two integers (which I can set the values of when I register the
callback), and some data. Currently Im' using these integers as keys
in a local dict where I'm storing the instances.

I suppose I could use the integers to store the high and low order
bytes of a pointer to the instance, but not sure how to do that in
Python (would be easy in c/c++). Also, I'm not sure how python memory
management works, if it will move objects around and make things
buggy. Also, that is definitely a hack and isn't very 'pythonesque'

More information about the Python-list mailing list