Interpreting Left to right?

Chris Angelico rosuav at gmail.com
Fri Jun 24 00:59:54 EDT 2011


On Fri, Jun 24, 2011 at 2:32 PM, Chetan Harjani
<chetan.harjani at gmail.com> wrote:
> x=y="some string"
> And we know that python interprets from left to right. so why it doesnt
> raise a name error here saying name 'y' is not defined?

In most languages, the answer is that the = operator associates right
to left, even though most other operators associate left to right.
(That's how C does it, for instance.) But in Python, I believe it's
actually one operator taking several parameters, because = is not an
expression.

In any case, you can safely treat = as an exception to the
left-to-right rule. There are other exceptions too - note the comments
in http://docs.python.org/reference/expressions.html#summary - there's
not many RTL operators in Python, only those that make sense that way.
:)

ChrisA



More information about the Python-list mailing list