Re: [BangPypers] Issue with list comprehension
Hi, _sum=sum([n for n in itertools.takewhile(lambda x: x 400, fib)]) TypeError: 'int' object is not callable Probably 'sum' might have been used earlier in the code to store the value of some integer. sum is a builtin function but the moment you assign sum=(x+y) or something, you have lost that function. If you are doubtful, do `from __builtin__ import * ` to reclaim your builtin variables and functions. Regards, Abdul Muneer -- Follow me on Twitter: @abdulmuneer http://twitter.com/#%21/abdulmuneer On Thu, Oct 31, 2013 at 12:22 PM, Asokan Pichai paso...@gmail.com wrote: import itertools def fib(): a, b = 0, 1 while True: yield b a, b = b, a+b print sum(itertools.takewhile(lambda x: x 400, fib)) --- I tried the above; and it worked too (986) Asokan Pichai ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Issue with list comprehension
import itertools def fib(): a, b = 0, 1 while True: yield b a, b = b, a+b print sum(itertools.takewhile(lambda x: x 400, fib)) --- I tried the above; and it worked too (986) Asokan Pichai ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
[BangPypers] Issue with list comprehension
Hi folks, I am new to Python (have been on Java for 8 years), but I have quickly grasped its style. While working on a problem (as Python practice) to generate fibonacci numbers using generators and list comprehension, I ran into an issue. Here's my code: *import itertools* * * *def fibonacci():* *a, b = 0, 1* *while True:* * yield b* * a, b = b, a+b* * * *fib=fibonacci() * *_sum=sum([n for n in itertools.takewhile(lambda x: x 400, fib)])* *print _sum* Basically, I wanted to sum up all the fib numbers till 400. *This code runs fine when run on REPL (shell). But when I run it* *as a script, i get following error:* Traceback (most recent call last): File 2.py, line 10, in module _sum=sum([n for n in itertools.takewhile(lambda x: x 400, fib)]) TypeError: 'int' object is not callable Python version: 2.7.3 Is there a bug in Python or am I missing something? Why does this only work in REPL and not as shell script? Thanks for help. Cheers, Anant ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Issue with list comprehension
Thats surprising. The code runs just fine http://ideone.com/06qjoM (Ans: 986) As an aside I renamed _sum to sum_ When avoiding naming conflicts, its recommended to postfix a underscore Prefixing an underscore is to provide a hint that the value that gets assigned to the variable is going to be ignore (ie. that variable itself is going to be ignored) Dhananjay On Wed, Oct 30, 2013 at 5:42 PM, Anant TECHIE anant.tec...@gmail.com wrote: Hi folks, I am new to Python (have been on Java for 8 years), but I have quickly grasped its style. While working on a problem (as Python practice) to generate fibonacci numbers using generators and list comprehension, I ran into an issue. Here's my code: *import itertools* * * *def fibonacci():* *a, b = 0, 1* *while True:* * yield b* * a, b = b, a+b* * * *fib=fibonacci() * *_sum=sum([n for n in itertools.takewhile(lambda x: x 400, fib)])* *print _sum* Basically, I wanted to sum up all the fib numbers till 400. *This code runs fine when run on REPL (shell). But when I run it* *as a script, i get following error:* Traceback (most recent call last): File 2.py, line 10, in module _sum=sum([n for n in itertools.takewhile(lambda x: x 400, fib)]) TypeError: 'int' object is not callable Python version: 2.7.3 Is there a bug in Python or am I missing something? Why does this only work in REPL and not as shell script? Thanks for help. Cheers, Anant ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers