File: closure_class_T596.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 (62 lines) | stat: -rw-r--r-- 1,090 bytes parent folder | download
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
# mode: run
# tag: closures
# ticket: t596

def simple(a, b):
    """
    >>> kls = simple(1, 2)
    >>> kls().result()
    3
    """
    class Foo:
        def result(self):
            return a + b
    return Foo

def nested_classes(a, b):
    """
    >>> kls = nested_classes(1, 2)
    >>> kls().result(-3)
    0
    """
    class Foo:
        class Bar:
            def result(self, c):
                return a + b + c
    return Foo.Bar

def staff(a, b):
    """
    >>> kls = staff(1, 2)
    >>> kls.static()
    (1, 2)
    >>> kls.klass()
    ('Foo', 1, 2)
    >>> obj = kls()
    >>> obj.member()
    (1, 2)
    """
    class Foo:
        def member(self):
            return a, b
        @staticmethod
        def static():
            return a, b
        @classmethod
        def klass(cls):
            return cls.__name__, a, b
    return Foo

def nested2(a):
    """
    >>> obj = nested2(1)
    >>> f = obj.run(2)
    >>> f()
    3
    """
    class Foo:
        def run(self, b):
            def calc():
                return a + b
            return calc
    return Foo()