Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exercise 14.5, "Think Python" (Allen Downey)

Tags:

python

urllib

I'm a first time programmer, first time StackOverflow user, studying from Allen Downey's free online text "Think Python".

The code shown here is from exercise 14.5 (p.143). I've been trying to figure out what that .fp does; my searches all came up empty. I ran the code both with and without it and didn't notice any difference in the results. I'd appreciate anyone's help on this.

import urllib

conn = urllib.urlopen('http://thinkpython.com/secret.html')

for line in conn.fp:
    print line.strip()
like image 456
Noel Noche Avatar asked Dec 19 '25 11:12

Noel Noche


2 Answers

It's just a faux file object attached to a socket object.

Read the docstring:

>>> help(conn.fp)
like image 145
Maksym Polshcha Avatar answered Dec 21 '25 23:12

Maksym Polshcha


urllib.urlopen returns a Request object, which has .fp (file pointer) as a property. By default, the Request object when iterated over uses the same pointer, so there's no functional difference.

like image 35
Sean Johnson Avatar answered Dec 22 '25 01:12

Sean Johnson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!