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.Mon, 29 Jan 2018 12:35:16 -0600Printing variable namehttp://ask.sagemath.org/question/40862/printing-variable-name/ How would I return a variable name in a function. E.g. If I have the function:
def mul(a,b):
return a*b
a = mul(1,2); a
b = mul(1,3); b
c = mul(1,4); c
This would return:
2
3
4
I would like it to return:
a = 2
b = 3
c = 4
How would I do this?Mon, 29 Jan 2018 12:05:32 -0600http://ask.sagemath.org/question/40862/printing-variable-name/Comment by dan_fulea for <p>How would I return a variable name in a function. E.g. If I have the function:</p>
<pre><code>def mul(a,b):
return a*b
a = mul(1,2); a
b = mul(1,3); b
c = mul(1,4); c
</code></pre>
<p>This would return:</p>
<pre><code>2
3
4
</code></pre>
<p>I would like it to return:</p>
<pre><code>a = 2
b = 3
c = 4
</code></pre>
<p>How would I do this?</p>
http://ask.sagemath.org/question/40862/printing-variable-name/?comment=40867#post-id-40867One answer could be:
print """a = 2
b = 3
c = 4"""
(Sorry, but this is strictly speaking a valid answer.)
If something else is needed, something "more general", and this is certainly the case, than please describe this generality. Note also that the name of the variables "live" only in the "namespace of the code", it is not a good idea to let them live also "outside", as output... For testing purposes one may try something like...
print "a = %s" % a
print "b = %s" % b
print "c = %s" % c
(using this old fashioned string formatter, that may become soon obsolete, but it is the most simple one...)
(So what is the reason for such prints? Three prints as above can be understood also in the
2
3
4
form...)Mon, 29 Jan 2018 12:35:16 -0600http://ask.sagemath.org/question/40862/printing-variable-name/?comment=40867#post-id-40867Answer by tmonteil for <p>How would I return a variable name in a function. E.g. If I have the function:</p>
<pre><code>def mul(a,b):
return a*b
a = mul(1,2); a
b = mul(1,3); b
c = mul(1,4); c
</code></pre>
<p>This would return:</p>
<pre><code>2
3
4
</code></pre>
<p>I would like it to return:</p>
<pre><code>a = 2
b = 3
c = 4
</code></pre>
<p>How would I do this?</p>
http://ask.sagemath.org/question/40862/printing-variable-name/?answer=40866#post-id-40866I am not sure about your request, does the following solve your issue ?
sage: def mul(a,b):
....: return a*b
sage: a = mul(1,2); print('a = {}'.format(a))
sage: b = mul(1,3); print('b = {}'.format(b))
sage: c = mul(1,4); print('c = {}'.format(c))
a = 2
b = 3
c = 4Mon, 29 Jan 2018 12:32:39 -0600http://ask.sagemath.org/question/40862/printing-variable-name/?answer=40866#post-id-40866Answer by FrédéricC for <p>How would I return a variable name in a function. E.g. If I have the function:</p>
<pre><code>def mul(a,b):
return a*b
a = mul(1,2); a
b = mul(1,3); b
c = mul(1,4); c
</code></pre>
<p>This would return:</p>
<pre><code>2
3
4
</code></pre>
<p>I would like it to return:</p>
<pre><code>a = 2
b = 3
c = 4
</code></pre>
<p>How would I do this?</p>
http://ask.sagemath.org/question/40862/printing-variable-name/?answer=40865#post-id-40865 sage: z=var('a')
sage: str(z)
'a'
Mon, 29 Jan 2018 12:27:50 -0600http://ask.sagemath.org/question/40862/printing-variable-name/?answer=40865#post-id-40865