美文网首页Python
Python3: Module

Python3: Module

作者: LET149 | 来源:发表于2023-05-03 08:53 被阅读0次

模块:module

本质上是一个 Python 脚本文件,这个脚本文件的 文件名 即为这个模块的 模块名

一个 模块 脚本中包含:函数变量 等;这些都可以通过对 模块 的调用而获得从而可以调用

1. 模块示例一

1.1 模块内容

模块名:first_module
文件名:first_module.py
文件所在目录:/home/zhiyong/Desktop/YYYYYYYYYY

variable_1= list([1,2,3])
variable_2= "aa"

def function_1(name, age):
    print(name +" is "+ "{}".format(age) + " years old.")

class Human:
    species= "H.sap"
    live_place= "Earth"

    def __init__(self, name, age, weight):
        self.name= name
        self.age= age
        self.weight= weight
        
    def run(self):
        print(self.name + " is running.")

    def eat(self, food):
        print(self.name + " is eating " + food + ".")

1.2 模块使用

>>> import os
>>> os.getcwd()
'/home/zhiyong/Desktop/YYYYYYYYYY'

>>> import first_module   '#导入模块

>>> first_module.variable_1   #调用模块中的变量
[1, 2, 3]
>>> first_module.variable_2
'aa'

>>> first_module.function_1(name="Jack", age=45)   #调用模块中的函数,注意参数的传入
Jack is 45 years old.

>>> first_module.Human.species   #调用模块中类的属性
'H.sap'
>>> first_module.Human.live_place
'Earth'

>>> Copper= first_module.Human(name="Copper", age=67, weight=80)   #用模块中定义的类创建一个对象
>>> Copper.age   #调用创建对象的属性
67
>>> Copper.eat("apple")   #调用创建对象的方法,注意参数的传入
Copper is eating apple.

相关文章

网友评论

    本文标题:Python3: Module

    本文链接:https://www.haomeiwen.com/subject/jxxkjdtx.html