def test_myfunction(monkeypatch): def mock_mydeco(f): return f monkeypatch.setattr('decorators.mydeco', mock_mydeco) importlib.reload(mymodule) assert mymodule.myfunction() == "ok"
# python formatting style using %. The old style still works, but shouldn't be used as # it will eventually stop being supported. # https://pyformat.info/ # ============================================================================ # # IMPORTING # So far we've only written code in one file, but it's possible to write it across # multiple files, and then use code from one file in another. This is done using # python's "import" mechanic, which "imports" the code into the file. import mymodule # This imported the file "mymodule.py" from the same directory as this examples.py # Notice that we don't need to give the extension - that's because python can only # import python files, so the extension is assumed. Providing the extension will # cause an error. Once the file is imported, we can run code from inside the file mymodule.myfunction() # It's as simple as that! However, we need to know how it knew where to find # "mymodule" just from the import statement above. This is done by using # "environment variables". This can be a complex topic, so here's the short version: # 1. Environment variables are like variables that are global to the entire # Operating system (eg, Windows) - not just for python! # 2. Environment variables are like a dictionary that can only map strings to strings # 3. Your computer sets environment variables when it starts up, and can be modified # 4. The most common usage is to map a short name to a file/folder on the filesystem. # This allows programs to lookup the environment variable value to find resources. # 5. Multiple values can be stored in one environment variable by separating the values # with a particular symbol - which symbol to use is defined by the Operating System. # 6. A default environment variable is called PATH and is mapped to a lot of folders # 7. Whenever a command is executed, it searches the folders defined in PATH to find a # script with the same name that it can run.
import mymodule #print(myfunction()) #error!! print(mymodule.myfunction()) from mymodule import myfunction print(myfunction())
import mymodule z = mymodule.myfunction(1,2)
import mymodule #print(myfunction()) #error" print(mymodule.myfunction()) #scoped variant from mymodule import myfunction #descoped variant print(myfunction())
# Creating and using your own module # Back to your program import mymodule mymodule.myfunction(42) # You called a function from a module!
print(add(1, 5)) print((lambda arg1, arg2: arg1 + arg2)(1, 2)) # 匿名函数的另一种调用方式 # --- 位置参数 和 关键字参数 的混合使用 --- function6(1, 2, 3, 4, 5) function6(1, *[2, 3, 4, 5]) function7(1, num=12, age=21) function8(1, 22, 2, 3, num=12) # =========================================================== # === 调用自定义模块 === # 导入模块并调用 (三种导入模块方式,选择其中一种) import time # 第一种, 引入单个模块 import time as t # 引入并重命名 import time import calendar # 第二种, 引入多个模块 from mymodule import myfunction # 第三种, 导入模块指定函数(*引入所有) print(myfunction()) # === 动态导入模块 === import importlib modulename = "os" # 方式1 obj = __import__(modulename) # 不推荐 # 方式2 obj = importlib.import_module(modulename) # 推荐使用 print(obj.getcwd())