Dumb python questions

Paul Rubin phr-n2001 at nightsong.com
Wed Aug 15 09:59:35 CEST 2001


kosh <kosh at aesaeion.com> writes:
> > 1) Suppose I have a complex number like a = 3+4j.  How do I get the
> > real and imaginary parts separately? 

> >>> a = 3+4j
> >>> dir(a)
> ['conjugate', 'imag', 'real']
> >>> a.imag

Thanks.  Can you tell me if that's documented anywhere?  

  http://www.python.org/doc/current/lib/typesnumeric.html

describes the conjugate() operation but not the real or imag members.
I'd like to report that as a deficiency in the documentation.

> >>> import types
> >>> a = 3
> >>> isinstance(a, types.IntType)
> 1

Thanks!  I can probably find that in the module docs.

> > 3) Are the complex math functions specified to have any particular
> >    branch cuts?  If they're unspecified, what happens in the actual cmath
> >    implementation?  If they're unspecified, I'd like to propose that the
> >    Scheme/Common Lisp conventions be adopted in a future version.
> 
> I am not sure what this question is asking so I will leave it to others to 
> answer.

It's a math thing, you might not want to know.  But basically:
cmath.sqrt(-1 + .01j) is very close to 1j, while cmath.sqrt(-1 - .01j)
is very close to -1j.  This discontinuity in the sqrt function is
called a branch cut.  It's where the sqrt function implementation
switches between returning the "positive" and "negative" square root.
Other functions like sin, cos, and log also have branch cuts.  The
location of the branch cuts depend on precisely how the functions are
implemented.  The Scheme and Common Lisp standards specify the exact
location of branch cuts but the cmath documentation doesn't say
anything about them.  So I think Python should adopt the Scheme/Common
Lisp standards.

Here's another dumb question, about the for loop: if I want to add
up the first 100 numbers in the naive way, am I really supposed to say

   sum = 0
   for i in range(101) :
     sum = sum + i

and if I say that, will range(101) really malloc up and populate an
array of length 100?  That's pretty horrible in terms of memory use
just to to make the syntax elegant (shades of APL).  I saw something
about iterators in the Python 2.2 notes so maybe there's a fix in 2.2
but I'm using 2.1.1 for now.

Thanks again

Paul



More information about the Python-list mailing list