ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 08 Mar 2019 06:23:39 +0100Why do I need to use real or real() some of the times?https://ask.sagemath.org/question/45711/why-do-i-need-to-use-real-or-real-some-of-the-times/I am making my own library to deal with polar to Cartesian conversions and for getting radius and angles in degrees.
I use .real and .imag quite a bit and have hit a wall when sometimes it would stop working due to error such as "built-in method real_part of sage.symbolic.expression.Expression" not compatible with method or something like that.
This is what I came up with as my solution.
def getReal(im):
if type(im)==complex:
return im.real
return im.real()
def getImag(im):
if type(im)==complex:
return im.imag
return im.real()
This resolves issue with incompatible expressions
My questions are
- Why is this necessary?
- How should I go about making my libraries so that it can be imported into other files without copying over the whole source?
- How do I create methods such as above that will work on types so I can instead of getReal(i) call i.getReal()?
Thanks!Thu, 07 Mar 2019 18:31:49 +0100https://ask.sagemath.org/question/45711/why-do-i-need-to-use-real-or-real-some-of-the-times/Comment by tiger for <p>I am making my own library to deal with polar to Cartesian conversions and for getting radius and angles in degrees.
I use .real and .imag quite a bit and have hit a wall when sometimes it would stop working due to error such as "built-in method real_part of sage.symbolic.expression.Expression" not compatible with method or something like that.
This is what I came up with as my solution.</p>
<pre><code> def getReal(im):
if type(im)==complex:
return im.real
return im.real()
def getImag(im):
if type(im)==complex:
return im.imag
return im.real()
</code></pre>
<p>This resolves issue with incompatible expressions</p>
<p>My questions are</p>
<ul>
<li>Why is this necessary?</li>
<li>How should I go about making my libraries so that it can be imported into other files without copying over the whole source?</li>
<li>How do I create methods such as above that will work on types so I can instead of getReal(i) call i.getReal()?</li>
</ul>
<p>Thanks!</p>
https://ask.sagemath.org/question/45711/why-do-i-need-to-use-real-or-real-some-of-the-times/?comment=45717#post-id-45717ps: ["library"](https://gist.github.com/kaovilai/8c5134a433e5288324a1e2708552144f)Fri, 08 Mar 2019 00:43:11 +0100https://ask.sagemath.org/question/45711/why-do-i-need-to-use-real-or-real-some-of-the-times/?comment=45717#post-id-45717Answer by nbruin for <p>I am making my own library to deal with polar to Cartesian conversions and for getting radius and angles in degrees.
I use .real and .imag quite a bit and have hit a wall when sometimes it would stop working due to error such as "built-in method real_part of sage.symbolic.expression.Expression" not compatible with method or something like that.
This is what I came up with as my solution.</p>
<pre><code> def getReal(im):
if type(im)==complex:
return im.real
return im.real()
def getImag(im):
if type(im)==complex:
return im.imag
return im.real()
</code></pre>
<p>This resolves issue with incompatible expressions</p>
<p>My questions are</p>
<ul>
<li>Why is this necessary?</li>
<li>How should I go about making my libraries so that it can be imported into other files without copying over the whole source?</li>
<li>How do I create methods such as above that will work on types so I can instead of getReal(i) call i.getReal()?</li>
</ul>
<p>Thanks!</p>
https://ask.sagemath.org/question/45711/why-do-i-need-to-use-real-or-real-some-of-the-times/?answer=45720#post-id-457201) The problem is that on `a=complex(1)`, we have that `a.real` and `a.imag` are attributes.
On `b=CC(1)` we have that `b.real` is a method so you need to call `b.real()` to get the part. Alternatively, you can use `b.real_part()`.
In sage it's become customary to access data via methods, not via attributes. That's a common tool to make it easy to hide implementation details. Strictly speaking it's not necessary in python because `@property` allows "getters" and "setters" that can be accessed as if they were attributes.
2) see [python modules](https://docs.python.org/2/tutorial/modules.html).
3) The methods available on a certain type of objects are the choice of that object. You don't get to change that. You can subclass, however, and then you can implement whatever methods you like in that subclass.Fri, 08 Mar 2019 06:23:39 +0100https://ask.sagemath.org/question/45711/why-do-i-need-to-use-real-or-real-some-of-the-times/?answer=45720#post-id-45720