Suggest: .get() method on lists.

Gerrit Holl gerrit.holl at pobox.com
Fri Jan 21 08:16:03 EST 2000


Martin von Loewis wrote on 948374851:
> 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.

f=sys.argv.get(1) # default = None
if not f: f = sys.stdin

But what about a sys.argv like this (untested)
class argv(UserList.UserList):
    def has_key(self, num):
        return len(self.data) > num:

    def get(self, num, default=None):
        if self.has_key(num): return self.data[num]
        else: return default

Wouldn't this be a good idea? I see too much code like above.

regards,
Gerrit.

-- 
Please correct any bad English you encounter in my email message!
-----BEGIN GEEK CODE BLOCK----- http://www.geekcode.com
Version: 3.12
GCS dpu s-:-- a14 C++++>$ UL++ P--- L+++ E--- W++ N o? K? w--- !O !M !V PS+ PE?
Y? PGP-- t- 5? X? R- tv- b+(++) DI D+ G++ !e !r !y
-----END GEEK CODE BLOCK-----




More information about the Python-list mailing list