Re: Numbers in python

2006-03-10 Thread neildunn
If your still worried by using floats for your values you may wish to
look into the decimal module:

http://docs.python.org/lib/module-decimal.html

Example:

 from decimal import Decimal
 Decimal(2) + Decimal('1.47')
Decimal(3.47)


Regards,
Neil

-- 
http://mail.python.org/mailman/listinfo/python-list


Nested list comprehensions

2005-11-26 Thread neildunn
Hey guys:

 [(i,j,k) for i in range(1,j) for j in range(1,k) for k in range(1,5)]
[(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 2, 1), (1, 2, 2), (1,
2, 3), (1, 2, 4), (1, 3, 1), (1, 3, 2), (1, 3, 3), (1, 3, 4), (2, 1,
1), (2, 1, 2), (2, 1, 3), (2, 1, 4), (2, 2, 1), (2, 2, 2), (2, 2, 3),
(2, 2, 4), (2, 3, 1), (2, 3, 2), (2, 3, 3), (2, 3, 4)]
 def a():
... print [(j,k) for j in range(1,k) for k in range(1,5)]
...
 a()
Traceback (most recent call last):
  File stdin, line 1, in ?
  File stdin, line 2, in a
UnboundLocalError: local variable 'k' referenced before assignment

Why is it that I can execute the nested list comprehension in the
intepreter
but if the same line is within a method declaration I get an unbound
reference error?

Is this a bug or am I missing some deep rule?

Regards, Neil Dunn

-- 
http://mail.python.org/mailman/listinfo/python-list