Suggest: .get() method on lists.

Martin von Loewis loewis at informatik.hu-berlin.de
Thu Jan 20 08:27:31 EST 2000


Gerrit Holl <gerrit.holl at pobox.com> writes:

> I thought it would be useful, for example, for sys.argv. At the moment,
> I have this code:
> 
> file = sys.stdin
> if len(sys.argv) > 1:
>     file = sys.argv[1]
> 
> But this, would be much easier to read:
> file = sys.argv.get(1, sys.stdin)

I guess it wouldn't be that useful in this specific case, as
sys.argv[1] if present is a string, so you (probably) write

file = sys.stdin
if len(sys.argv) > 1:
    file = open(sys.argv[1])

right now, to open the file. I can't see how you could get the
equivalent of that code with .get.

Regards,
Martin





More information about the Python-list mailing list