[Tutor] Importing question
Kent Johnson
kent37 at tds.net
Sat Jul 14 04:18:44 CEST 2007
Dick Moores wrote:
> At http://wiki.python.org/moin/SimplePrograms I found this code:
>
> ============================================
> import itertools
>
> def iter_primes():
> # an iterator of all numbers between 2 and +infinity
> numbers = itertools.count(2)
>
> # generate primes forever
> while True:
> # get the first number from the iterator (always a prime)
> prime = numbers.next()
> yield prime
>
> # this code iteratively builds up a chain of
> # filters...slightly tricky, but ponder it a bit
> numbers = itertools.ifilter(prime.__rmod__, numbers)
>
> for p in iter_primes():
> if p > 1000:
> break
> print p
> ====================================================
>
> It works for me in Win XP, Python 2.5.
>
> However, in trying to dig into the code to understand it, I'm not able
> to find itertools.py, even though itertools is found in the docs at <
> http://www.python.org/doc/2.4/lib/module-itertools.html>.
> A search of my Python25 directory doesn't turn up an itertools.py.
>
> So my question is, how does the first line of the code work? /Where /is
> itertools?
You can find out where any module is located using its __file__
attribute. This works for modules written in C, too. On my (Mac OSX)
computer:
In [2]: itertools.__file__
Out[2]:
'/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-dynload/itertools.so'
Kent
More information about the Tutor
mailing list