ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 14 Feb 2019 15:18:51 -0600"=" vs "==" - when to use what?http://ask.sagemath.org/question/45458/vs-when-to-use-what/ i am confused as to when to use "=" and when "==" is correct.
My most recent model has been that "=" means "is defined as", while "==" means "evaluates to the same as".
But that does not make sense either, since for example stuff like
plot(f.subs(a == 3), (x, -1, +1)) is clearly a definition, not an evaluation.
So please someone explain this to me.Thu, 14 Feb 2019 02:46:42 -0600http://ask.sagemath.org/question/45458/vs-when-to-use-what/Answer by eric_g for <p>i am confused as to when to use "=" and when "==" is correct.
My most recent model has been that "=" means "is defined as", while "==" means "evaluates to the same as". </p>
<p>But that does not make sense either, since for example stuff like </p>
<p>plot(f.subs(a == 3), (x, -1, +1)) is clearly a definition, not an evaluation. </p>
<p>So please someone explain this to me.</p>
http://ask.sagemath.org/question/45458/vs-when-to-use-what/?answer=45467#post-id-45467I agree that `f.subs(a == 3) ` is bad syntax and should probably be deprecated. For substitutions, it is much clearer to use a Python dictionary:
sage: a, b = var('a b')
sage: f = a*x + b
sage: f.subs({a: 3})
b + 3*x
sage: f.subs({a: 3, b: -2})
3*x - 2Thu, 14 Feb 2019 15:18:51 -0600http://ask.sagemath.org/question/45458/vs-when-to-use-what/?answer=45467#post-id-45467Answer by tmonteil for <p>i am confused as to when to use "=" and when "==" is correct.
My most recent model has been that "=" means "is defined as", while "==" means "evaluates to the same as". </p>
<p>But that does not make sense either, since for example stuff like </p>
<p>plot(f.subs(a == 3), (x, -1, +1)) is clearly a definition, not an evaluation. </p>
<p>So please someone explain this to me.</p>
http://ask.sagemath.org/question/45458/vs-when-to-use-what/?answer=45459#post-id-45459Actually, in Python `=` has another meaning when calling a function or a method: it is used to pass a value to a parameter, that is, if a function `f` has two parameters `m` and `n`, and you want to evaluate it with $m=1$ and $n=4$, you can do:
f(m=1, n=4)
I would say that `f(m==1)` is a attempt to be nice with the newcomer, but it would not mean anything outside the symbolic ring (that keeps `m==1` as a symbolic expression with two operands), since in general this will just reduce to `f(True)` or `f(False)`. There is nothing interesting to understand here, this is not Pythonic, and reserved to a dark part of Sage.Thu, 14 Feb 2019 02:50:28 -0600http://ask.sagemath.org/question/45458/vs-when-to-use-what/?answer=45459#post-id-45459Comment by stockh0lm for <p>Actually, in Python <code>=</code> has another meaning when calling a function or a method: it is used to pass a value to a parameter, that is, if a function <code>f</code> has two parameters <code>m</code> and <code>n</code>, and you want to evaluate it with $m=1$ and $n=4$, you can do:</p>
<pre><code>f(m=1, n=4)
</code></pre>
<p>I would say that <code>f(m==1)</code> is a attempt to be nice with the newcomer, but it would not mean anything outside the symbolic ring (that keeps <code>m==1</code> as a symbolic expression with two operands), since in general this will just reduce to <code>f(True)</code> or <code>f(False)</code>. There is nothing interesting to understand here, this is not Pythonic, and reserved to a dark part of Sage.</p>
http://ask.sagemath.org/question/45458/vs-when-to-use-what/?comment=45460#post-id-45460that makes the scope of my question bigger, and i would love to hear a good answer.Thu, 14 Feb 2019 06:20:50 -0600http://ask.sagemath.org/question/45458/vs-when-to-use-what/?comment=45460#post-id-45460