Re: __call__ method in Python

From: <WLavrijsen_at_lbl.gov>
Date: Thu, 26 Jun 2008 17:25:30 -0700


Akira,

> > > > foo = Foo() # constructor is not written in the above example...
> > > > a = foo(1, 2) # OK
> > > > foo(1, 2) += 10. # NG
> SyntaxError: can't assign to function call

which version of ROOT are you using? If I try the above (with a little modification to actually allocate an array to assign to :) ), then I get:

SyntaxError: illegal expression for augmented assignment

Which might be fixable, but I'm not quite sure how. Note that this was done for pure assignment (i.e. '=' instead of '+=') for operator(int,int):

>>> foo(1, 2) = 2.
Are you trying to assign a value to a reference return, for example to the result of a call to "double& SMatrix<>::operator()(int,int)"? If so, then please use operator[] instead, as in e.g. "mymatrix[i,j] = somevalue".

  File "<stdin>", line 1
SyntaxError: can't assign to function call >>> foo[1, 2] = 2.
>>> print foo(1, 2)
2.0
>>>

Thus, as for:

> Is there any way to make python assign to foo(1, 2)? Other setter/getter
> methods will work if I add them. But I prefer simple script which is easy to
> read instead of using a setter.

assuming you're running the same version of ROOT as I am, please use:

>>> foo[1, 2] = foo(1, 2) + 10.

Which I'll agree doesn't look very elegant, but has been the best I can come up with, the language limitation being what it is.

Best regards,

           Wim

-- 
Wim.Lavrijsen_at_cern.ch   --   WLavrijsen_at_lbl.gov   --   www.lavrijsen.net

"Your day will be somewhat dictated by authority."      --fortune cookie
Received on Fri Jun 27 2008 - 02:25:30 CEST

This archive was generated by hypermail 2.2.0 : Sat Jun 28 2008 - 05:50:08 CEST