File: nonlocal_T490.pyx

package info (click to toggle)
cython 3.0.11%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 19,092 kB
  • sloc: python: 83,539; ansic: 18,831; cpp: 1,402; xml: 1,031; javascript: 511; makefile: 403; sh: 204; sed: 11
file content (157 lines) | stat: -rw-r--r-- 2,323 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
def simple():
    """
    >>> simple()
    1
    2
    """
    x = 1
    y = 2
    def f():
        nonlocal x
        nonlocal x, y
        print(x)
        print(y)
    f()

def assign():
    """
    >>> assign()
    1
    """
    xx = 0
    def ff():
        nonlocal xx
        xx += 1
        print(xx)
    ff()

def nested():
    """
    >>> nested()
    1
    """
    x = 0
    def fx():
        def gx():
            nonlocal x
            x=1
            print(x)
        return gx
    fx()()

def arg(x):
    """
    >>> arg('x')
    xyy
    """
    def appendy():
        nonlocal x
        x += 'y'
    x+='y'
    appendy()
    print x
    return

def argtype(int n):
    """
    >>> argtype(0)
    1
    """
    def inc():
        nonlocal n
        n += 1
    inc()
    print n
    return

def ping_pong():
    """
    >>> f = ping_pong()
    >>> inc, dec = f(0)
    >>> inc()
    1
    >>> inc()
    2
    >>> dec()
    1
    >>> inc()
    2
    >>> dec()
    1
    >>> dec()
    0
    """
    def f(x):
        def inc():
            nonlocal x
            x += 1
            return x
        def dec():
            nonlocal x
            x -= 1
            return x
        return inc, dec
    return f

def methods():
    """
    >>> f = methods()
    >>> c = f(0)
    >>> c.inc()
    1
    >>> c.inc()
    2
    >>> c.dec()
    1
    >>> c.dec()
    0
    """
    def f(x):
        class c:
            def inc(self):
                nonlocal x
                x += 1
                return x
            def dec(self):
                nonlocal x
                x -= 1
                return x
        return c()
    return f

def class_body(int x, y):
    """
    >>> c = class_body(2,99)
    >>> c.z
    (3, 2)
    >>> c.x     #doctest: +ELLIPSIS
    Traceback (most recent call last):
    AttributeError: ...
    >>> c.y     #doctest: +ELLIPSIS
    Traceback (most recent call last):
    AttributeError: ...
    """
    class c(object):
        nonlocal x
        nonlocal y
        y = 2
        x += 1
        z = x,y
    return c()

def nested_nonlocals(x):
    """
    >>> g = nested_nonlocals(1)
    >>> h = g()
    >>> h()
    3
    """
    def g():
        nonlocal x
        x -= 2
        def h():
            nonlocal x
            x += 4
            return x
        return h
    return g