ANNOUNCE: PySymbolic - Doing Symbolics in Python

Tim Peters tim_one at email.msn.com
Thu Oct 12 21:09:35 EDT 2000


[Pearu Peterson]
> In addition, I am thinking of the following syntax examples in Python:
>         Diff(x*x,x)
>         Integrate(a*x,x=[c,d])
>     [snip]
> It is not possible to get this kind of a look for Python and it
> looks pretty good to me.
>
> I really meant, "It is possible to get .."

[Alex Martelli]
> Python evaluates the arguments before passing them to any
> callable-object.  So, for example, if you write
>     Diff(x*x, x)
> the x*x multiplication will take place before Diff ever sees
> the argument -- it will only get a number (the square of
> x), not the information that it was obtained by an expression
> of a given form.  Similarly for x -- Diff will get the value,
> not the variable-name.
>
> You will probably need to quote your args...:
>
>     Diff('x*x', 'x')

I believe Pearu has already discovered "the right way" to get the effect
he's after in Python:  define a class for symbolic variables, and precede
his examples by, e.g.,

    x = Symbolic('x')
    a = Symbolic('a')

and so on.  Then define Symbolic.__add__ etc to build up an expression tree.
That slick trick has been rediscovered several times in Python, and I've
found it quite satsifying in practice the few times I've used it.

beats-incessant-quoting-anyway-ly y'rs  - tim






More information about the Python-list mailing list