[Tutor] Stacks and Stack underflow/Stack overflow
wprins at gmail.com
Sun Nov 20 02:47:22 CET 2011
On 20 November 2011 00:09, Steven D'Aprano <steve at pearwood.info> wrote:
> Walter Prins wrote:
> pickle files are binary files and you probably opened it as a text file.
>> See here: http://docs.python.org/py3k/**library/functions.html#open<http://docs.python.org/py3k/library/functions.html#open>
> Not so. Pickle protocol 0 is text, and it happens to be the default
> protocol used if you don't instruct it different. So by default, pickles
> are text unless you specifically use a binary protocol.
Well I stand corrected, thanks! :) For some reason I thought they were
binary by default too, but hey ho... will try to remember that for future
To Joe, another hint: Note that you can /directly/ pass the /file-like/
object returned by urllib directly to pickle....
So, pickle can effectively unpickle data read directly from the website
(via a file-like object that actually reads from the webiste, not a real
file), and you then needn't care what format the pickle file (on the
website) is in... Neat eh?
This by the way, is a small demonstration of "duck typing" -- Pickle
doesn't really care whether what you pass into pickle.load() method is
actually *really* a file object -- As long as it walks like a file, and
quacks like a file, sufficiently well for pickle's purposes then everyone's
happy and you get back an unpickled object... ;)
PS You can get the unpickled object in Python for this puzzle with between
1 and 3 lines of code, depending on how much nesting of calls you use...
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor