Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 1 | from UserList import UserList |
| 2 | |
| 3 | def f(*a, **k): |
| 4 | print a, k |
| 5 | |
| 6 | def g(x, *y, **z): |
| 7 | print x, y, z |
| 8 | |
| 9 | def h(j=1, a=2, h=3): |
| 10 | print j, a, h |
| 11 | |
| 12 | f() |
| 13 | f(1) |
| 14 | f(1, 2) |
| 15 | f(1, 2, 3) |
| 16 | |
| 17 | f(1, 2, 3, *(4, 5)) |
| 18 | f(1, 2, 3, *[4, 5]) |
Jeremy Hylton | 003663d | 2000-03-28 23:53:22 +0000 | [diff] [blame^] | 19 | f(1, 2, 3, *UserList([4, 5])) |
Jeremy Hylton | aed0d8d | 2000-03-28 23:51:17 +0000 | [diff] [blame] | 20 | f(1, 2, 3, **{'a':4, 'b':5}) |
| 21 | f(1, 2, 3, *(4, 5), **{'a':6, 'b':7}) |
| 22 | f(1, 2, 3, x=4, y=5, *(6, 7), **{'a':8, 'b':9}) |
| 23 | |
| 24 | try: |
| 25 | g() |
| 26 | except TypeError, err: |
| 27 | print "TypeError:", err |
| 28 | else: |
| 29 | print "should raise TypeError: not enough arguments; expected 1, got 0" |
| 30 | |
| 31 | try: |
| 32 | g(*()) |
| 33 | except TypeError, err: |
| 34 | print "TypeError:", err |
| 35 | else: |
| 36 | print "should raise TypeError: not enough arguments; expected 1, got 0" |
| 37 | |
| 38 | try: |
| 39 | g(*(), **{}) |
| 40 | except TypeError, err: |
| 41 | print "TypeError:", err |
| 42 | else: |
| 43 | print "should raise TypeError: not enough arguments; expected 1, got 0" |
| 44 | |
| 45 | g(1) |
| 46 | g(1, 2) |
| 47 | g(1, 2, 3) |
| 48 | g(1, 2, 3, *(4, 5)) |
| 49 | try: |
| 50 | g(1, 2, 3, **{'x':4, 'y':5}) |
| 51 | except TypeError, err: |
| 52 | print err |
| 53 | else: |
| 54 | print "should raise TypeError: keyword parameter redefined" |
| 55 | |
| 56 | try: |
| 57 | g(1, 2, 3, a=4, b=5, *(6, 7), **{'a':8, 'b':9}) |
| 58 | except TypeError, err: |
| 59 | print err |
| 60 | else: |
| 61 | print "should raise TypeError: keyword parameter redefined" |
| 62 | |
| 63 | try: |
| 64 | f(**{1:2}) |
| 65 | except TypeError, err: |
| 66 | print err |
| 67 | else: |
| 68 | print "should raise TypeError: keywords must be strings" |
| 69 | |
| 70 | try: |
| 71 | h(**{'e': 2}) |
| 72 | except TypeError, err: |
| 73 | print err |
| 74 | else: |
| 75 | print "should raise TypeError: unexpected keyword argument: e" |
| 76 | |
| 77 | try: |
| 78 | h(*h) |
| 79 | except TypeError, err: |
| 80 | print err |
| 81 | else: |
| 82 | print "should raise TypeError: * argument must be a tuple" |
| 83 | |
| 84 | try: |
| 85 | h(**h) |
| 86 | except TypeError, err: |
| 87 | print err |
| 88 | else: |
| 89 | print "should raise TypeError: ** argument must be a dictionary" |
| 90 | |
| 91 | def f2(*a, **b): |
| 92 | return a, b |
| 93 | |
| 94 | d = {} |
| 95 | for i in range(512): |
| 96 | key = 'k%d' % i |
| 97 | d[key] = i |
| 98 | a, b = f2(1, *(2, 3), **d) |
| 99 | print len(a), len(b), b == d |