On Sat, Jun 9, 2018 at 6:28 AM Michel Desmoulin <desmoulinmic...@gmail.com>
wrote:

> Example, open this files, load all lines in memory, skip the first line,
> then get all the line until the first comment:
>
>     import itertools
>
>     def is_commented(line):
>         return lines.startwith('#')
>
>     def lines():
>         with open('/etc/fstab'):
>             lines = f.readlines()[1:]
>             return list(itertools.dropwhile(lines, is_commented)
>
> Becomes:
>
>     def is_commented(line):
>         return lines.startwith('#')
>
>     def lines():
>         with open('/etc/fstab'):
>             return f.readlines()[1:is_commented]
>
> It's not about how much shorter is is, but it is very nice to read.
>

If you're going to put it in a function anyway, why something like this?

    def lines():
        with open('/etc/fstab'):
            f.readline()
            for line in f:
                if line.startswith('#'):
                    break
                yield line
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to