python idioms : some are confusing

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Sep 21 02:29:10 EDT 2012


On Thu, 20 Sep 2012 22:52:45 -0700, alex23 wrote:

> On Sep 21, 3:34 pm, Vineet <vineet.deod... at gmail.com> wrote:
>> Amongst the python idioms, how the below-mentioned make sense? ## There
>> should be one-- and preferably only one --obvious way to do it. --- In
>> programming, there can be a number of ways, equally efficient, to do
>> certain  thing.
> 
> This isn't talking about your Python code as much as about Python
> itself. For example, in Python 2.x you can use either `open` or `file`
> to open a file, with `file` being a factory function for creating file
> objects, and `open` using it internally. In Python 3.x, `file` is no
> longer a built-in, as it produced a point of confusion as to which was
> the one obvious way to open a file.

I don't think that's the reason. I think the reason is that moving the 
built-in file into the _io library gives the developers a lot more 
flexibility in how they handle text and binary files. E.g.:


py> open('junk', 'w')
<_io.TextIOWrapper name='junk' mode='w' encoding='UTF-8'>

py> open('junk', 'wb')
<_io.BufferedWriter name='junk'>

py> open('junk', 'wb', buffering=0)
<_io.FileIO name='junk' mode='wb'>


The open() function now can return three (or more?) types instead of 
having a single built-in type handle all cases.



-- 
Steven



More information about the Python-list mailing list