[Tutor] Python Assignment Expression and Callable Expression
Steven D'Aprano
steve at pearwood.info
Fri Sep 19 08:39:25 CEST 2014
On Fri, Sep 19, 2014 at 02:41:31AM +0000, Wang Lei (ERIAN) wrote:
> Hi, everyone:
>
> I really believe that python is a great language but assignment and callable are not flexible:
>
> I wish that I can do this:
>
> class matrixArray(list):
> bla bla bla
> def __call__(self, rid, cid):
> return self.head[rid][cid]
You can. __call__ is used for making objects a callable, function-like
object. C++ calls them "functors". (Not the same thing as what Haskell
calls functors!)
> Once I call mat(1,1), I can get a result. but I want it more a value not a reference.
You want to change the entire execution model of Python? Why?
Like most modern languages, such as Ruby, Java (mostly), and Javascript,
Python values are references, not unboxed low-level primitive values.
> Considering "mat(1,1) = 5" expression, I wish the parser can
> dynamically map "mat(1,1)" to reference of value of that "or anonymous
> reference" or reference defined in class.
I'm afraid I don't understand what you are trying to say, but if you
want to assign to individual items in a matrix, use indexing, not
function call:
mat[1, 1] = 5
will work perfectly once you add a __setitem__ method to your matrix
class.
> I don't want to modify the
> lexical parsing in C++ but failed after trying different method in
> pythonic ways:
What does C++ have to do with this?
> decorator: fun -> object mapping, because it just substitute function
> name with new function and cannot read "self" object.
>
> I wish python developers could think of an idea to update the lexical
> parsing method or simply provide us a tool to modify it in python
> context.
I think that their answer will be:
"We will not complicate the language, making our job enormously harder,
and Python much harder to learn and use, just because a beginner to
Python who doesn't understand what the language can do or how to use it,
wants to program in C++ instead of Python. If you want to program in
C++, use C++. If you want to program in Python, learn Python."
What do you expect to do with:
mat(1, 1) = 5
that cannot be done with this instead?
mat[1, 1] = 5
--
Steven
More information about the Tutor
mailing list