[Tutor] new to python
Mats Wichmann
mats at wichmann.us
Sun Jul 23 12:10:33 EDT 2017
On 07/23/2017 09:16 AM, Alex Kleider wrote:
> On 2017-07-23 01:06, Anish Tambe wrote:
>>> for line in file:
>>
>> This line is not required as the you have opened your file to 'f'.
>> 'file' is a built-in class. Type -
>> help(file)
>> on the interpreter to know more about it.
>
> This appears to be true in python2x but not in python3:
>
> alex at X301n3:~$ python3
> Python 3.4.3 (default, Nov 17 2016, 01:11:57)
> [GCC 4.8.4] on linux
> Type "help", "copyright", "credits" or "license" for more information.
>>>> help(file)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> NameError: name 'file' is not defined
>>>> exit()
> alex at X301n3:~$ python
> Python 2.7.6 (default, Oct 26 2016, 20:32:47)
> [GCC 4.8.4] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> help(file)
> Help on class file in module __builtin__:
>
> class file(object)
> | file(name[, mode[, buffering]]) -> file object
> |
> ...
It is nonetheless still true that Python provides context manager
support for file objects that behave the same way as described (that is,
when handle - called 'f' in the example above - goes out of scope, it is
closed for you). Otherwise, the relevant documentation on file objects
is now in the IO discussion:
https://docs.python.org/3/library/io.html
> Also puzzling is that the 'intro' to python2 declares itself to be 'on
> linux2' vs just 'on linux' in the case of python3. (Something I'd not
> previously noticed.)
That's just a cleanup of an old issue, Python3 dropped the "linux2"
thing (which was never a great idea, linux kernels are now 4.x after 2.x
lived for a very long time, Python never followed those changes nor did
it need to);
FWIW, the preferred method now to check if a host is linux is to do:
if sys.platform.startswith("linux"):
instead of checking explicitly for a string "linux", "linux2", etc.
More information about the Tutor
mailing list