File-like objects

There is a notion in Python of "file-like objects". This isn't as formal as a specific protocol, but, thanks to the polymorphism afforded by duck-typing, it works well in practice.

The reason it's not closely specified is that different types of data streams and devices have many different capabilities, expectations, and behaviors. So in fact defining a set of protocols to model them would be quite complex, and it wouldn't actually gain us much in practice, other than a smug sense of theoretical achievement. This is where the EAFP philosophy comes into its own: If you want to perform seek() on a file-like object without knowing in advance that it supports random access, go ahead and try (literally!). Just be prepared to fail if the seek() method doesn't exist, or if it does exist but doesn't behave as you expect.

You might say "If it looks like a file and reads like a file, then it is a file".

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset