Dumb python questions

mac4 at chaos.theory.org mac4 at chaos.theory.org
Thu Aug 16 01:23:00 EDT 2001


Paul Rubin <phr-n2001 at nightsong.com> wrote:
> I just started playing with python and haven't been able to figure
> out the following things from the docs I've found:
> 
> 1) Suppose I have a complex number like a = 3+4j.  How do I get the
> real and imaginary parts separately?  I guess I could say
>   x = (a + a.conjugate())/2
>   y = (a - a.conjugate())/2
> but I can't take this language seriously if that's what the designers 
>intended.

Here is a tip. The first thing to do what you are curious about an 
object is use the dir method.

>>> c = 3+4j
>>> dir(c)
['conjugate', 'imag', 'real']
>>> c.real
3.0
>>> c.imag
4.0
>>> c.conjugate
<built-in method conjugate of complex object at 0x100aebe8>

> 
> 2) Is there a way I can tell if a value is of an integer type?  That is,
>   I want to write a function is_int(x) so that
>     is_int(3) = 1    # 3 is an integer type
>     is_int(3L) = 1   # 3L is an integer type
>     is_int(3.0) = 0  # 3.0 is float type
>     is_int(3+2j) = 0 # 3+2j is complex type

>>> from type import *
>>> i = 100
>>> type(i) == IntType
1
>>> s = "string"
>>> type(s) == IntType
0

The type module has all sorts of types in it.  If you want to check if
an object is of a certain type, use the isinstance method.

> 
> 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 dunno :)

Neil Macneale

-- 

If you want to email me, remove the '-devnull' from my email address



More information about the Python-list mailing list