summaryrefslogtreecommitdiff
path: root/tests/basics/import_instance_method.py
blob: d25b70ac5f0bfb70c4a8cdcbba173b57c3d2e3b6 (plain)
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
# Test importing a method from a class instance.
# This is not a common thing to do, but ensures MicroPython has the same semantics as CPython.

import sys

if not hasattr(sys, "modules"):
    print("SKIP")
    raise SystemExit


class A:
    def __init__(self, value):
        self.value = value

    def meth(self):
        return self.value

    def meth_with_arg(self, a):
        return [self.value, a]


# Register a class instance as the module "mod".
sys.modules["mod"] = A(1)

# Try importing it as a module.
import mod

print(mod.meth())
print(mod.meth_with_arg(2))

# Change the module.
sys.modules["mod"] = A(3)

# Try importing it using "from ... import".
from mod import meth, meth_with_arg

print(meth())
print(meth_with_arg(4))