[vtkusers] SetClientData in python (David Gobbi)
Oliver Natt
onatt at gmx.de
Sun Nov 12 09:26:09 EST 2017
Dear Andrew,
Davids proposal ist great. However, it appears a bit strange to most
programmers. I prefer for this purpose to make a new class which is a
callable. I think, this makes the code more readable for most
programmers since the data attribute is already initialized within the
class itself. Your example would look like this:
class DummyCallback:
def __init__(self, data):
self.data = data
def __call__(self, obj, event):
print(ev, self.data)
cb = DummyCallback(renwin)
interactor.AddObserver('LeftButtonPressEvent', cb)
The last two lines can, of course, be abbreviated to:
interactor.AddObserver('LeftButtonPressEvent', DummyCallback(renwin))
Best regards
Oliver
Am 11.11.2017 um 23:46 schrieb Andrew Maclean:
> Ok
> I have got it.
> This works:
>
> def DummyFunc(obj, ev):
> print(ev, DummyFunc.data)
>
> DummyFunc.data = renwin
> # How do I pass my obj_2 ? interactor.AddObserver('LeftButtonPressEvent', DummyFunc)
>
> David, you may be able to explain this better than I can but I assume
> this is what happens:
> Here we are adding a new attribute to DummyFunc. We can do this
> because Python functions are in fact objects.
>
> This is something totally strange to C++ programmers!
>
> What a neat approach!
>
>
--
Oliver Natt,
Jägerstraße 26b
90451 Nürnberg
Phone: +49 (0) 9 11 - 71 58 71 40
Mobile +49 (0) 1 76 - 20 82 27 38
E-Mail: onatt at gmx.de
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20171112/20daff6d/attachment.html>
More information about the vtkusers
mailing list