In fact, there are quite a few Python tutorials online. It is very simple to write your own module. This is actually what you have been doing! This is because every Python program is also a module. You just need to make sure it has a .py extension. The following cases will provide a clear explanation.
Case (save as mymodule.py
):
import mymodule mymodule.say_hi() print('Version', mymodule.__version__)
What is presented above is a simple module. As you can see, there is no special difference compared to the Python programs we usually use. We'll see next how to use this module in other Python programs.
Remember that this module should be placed in the same directory as the other programs we are about to import this module, or placed in one of the directories listed in sys.path
.
Another module (saved as mymodule_demo.py
):
import mymodule mymodule.say_hi()print('Version', mymodule.__version__)
Output:
$ python mymodule_demo.py Hi, this is mymodule speaking. Version 0.1
How it works
You'll notice that we use the same dot notation to access members in the module. Python does a good job of reusing symbols, which gives it a "Pythonic" feel, so we don't have to learn new ways to do the same thing.
The following is a template using the from...import
syntax (saved as mymodule_demo2.py
):
from mymodule import say_hi, __version__ say_hi() print('Version', __version__)
mymodule_demo2. The content output by py
is the same as the content output by mymodule_demo.py
.
It should be noted here that if the name __version__
already exists in the module imported into mymodule, a conflict will occur. This is probably because each module usually declares their respective version number using this name. Therefore, we generally recommend using the import
statement, although this will make your program slightly longer.
You can also use:
from mymodule import *
This will import all public names such as say_hi
but not __version__
names because of the latter Start with a double underscore.
Warning: Remember that you should avoid using the form import , i.e. `from mymodule import `.
Zen of Python
One of the guiding principles of Python is that “clearness is better than obscurity”2. You can learn more by running
import this
in Python.
dir
Function The built-in dir()
function can return a list of names defined by the object. If this object is a module, the list includes functions, classes, and variables defined within the function.
This function accepts parameters. If the argument is a module name, the function returns a list of names for the specified module. If no arguments are provided, the function returns a list of names for the current module.
Case:
$ python >>> import sys # 给出 sys 模块中的属性名称 >>> dir(sys) ['__displayhook__', '__doc__', 'argv', 'builtin_module_names', 'version', 'version_info'] # 此处只展示部分条目 # 给出当前模块的属性名称 >>> dir() ['__builtins__', '__doc__', '__name__', '__package__','sys'] # 创建一个新的变量 'a' >>> a = 5 >>> dir() ['__builtins__', '__doc__', '__name__', '__package__', 'a'] # 删除或移除一个名称 >>> del a >>> dir() ['__builtins__', '__doc__', '__name__', '__package__']
How it works
The first thing we see is dir
in the imported sys
Usage on the module. We can see a huge list of properties it contains.
Subsequently, we use the dir
function without passing parameters. By default, it returns the current module's property list. Note that the list of imported modules will also be part of this list.
Given the operation of the observation dir
function, we define a new variable a
and assign it a value, and then check dir
From the returned results, we can find that a new value appears in the list with the same name. We removed a variable or attribute through the del
statement, and this change is again reflected in the content of the dir
function.
A little tip about del
- this statement is used to delete a variable or name. When this statement is run, in this case del a
, you will no longer be able to access the variable a
- it will be as if it never existed.
It should be noted that the dir()
function can work on any object. For example, running dir(str)
can access the properties of the str
(String, string) class.
At the same time, there is also a vars()
function that can also return you the properties of these values, but it is just possible that it will not work properly for all classes.
Recommended courses: Python video tutorial
The above is the detailed content of Write your own Python module. For more information, please follow other related articles on the PHP Chinese website!