Quick question about sympify variables

45 views
Skip to first unread message

Andrew Spielberg

unread,
Dec 29, 2014, 5:48:17 PM12/29/14
to sy...@googlegroups.com
Hi guys,

I have a variable with a period in it (say, 'base.length').  I now wish to sympify an expression, say:

2*base.length

This throws an exception,

AttributeError: 'Symbol' object has no attribute 'length'

due to the period.  Is there a way I can have the sympify read the dot as just part of a variable name here?

-Andy S.

Jason Moore

unread,
Dec 30, 2014, 1:24:00 AM12/30/14
to sy...@googlegroups.com
Andy,

The period is an invalid character for Python variables. The period is reserved for accessing attributes and methods of Python objects.

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAM6e1JdM%3Dz_vcQkQo%3DLcSVO7oxznXhrVt0U2xQw5GMe8os_R1A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Andrew Spielberg

unread,
Dec 30, 2014, 2:39:54 PM12/30/14
to sy...@googlegroups.com
Hi Jason,

That's true that it's an invalid character for python variables, but not for sympy variables.  For instance, doing:

s = Symbol('base.length')

works just fine.

I was under the impression that sympify's inputs is treated as a string version of a sympy expression, NOT a Python expression.  This seems to be what the documentation implies: http://docs.sympy.org/dev/modules/core.html.

Shouldn't this work?  I feel like either the documentation is misleading/ambiguous or the function is not matching the specification.

Is there a workaround you would recommend that will properly parse periods as Symbol does?

-Andy S.

Jason Moore

unread,
Dec 30, 2014, 7:30:08 PM12/30/14
to sy...@googlegroups.com
Oh I see. I didn't realize you were using sympify. But I don't see anywhere in the docs that indicates that sympify should understand how to deal with variable names that include a period. The function that is failing is:

http://docs.sympy.org/dev/modules/parsing.html#sympy.parsing.sympy_parser.parse_expr

You can customize the parsers if you want to handle different kinds of string input.

Aaron Meurer

unread,
Dec 31, 2014, 12:25:40 AM12/31/14
to sy...@googlegroups.com
You can see if the parser is modifiable. sympify is more or less just a pass through on a Python parser, which maps undefined variable names to Symbols. sympify('2*lambda') doesn't work either, for instance ('lambda' is a reserved Python keyword).

Aaron Meurer

Reply all
Reply to author
Forward
0 new messages