zoukankan      html  css  js  c++  java
  • python调用其他文件的类和函数

    在同一个文件夹下

    调用函数

    source.py文件:

    def func():
        pass

    new.py文件:

    import source
    # 或者
    from  source import func

    调用类

    Student.py文件:

    class Student:
        def __init__(self, name, age, sex):
            self name = name
            self age = age
            self sex =sex
    
        def learn(self):
            print("学生学习!")

    handler.py文件:

    from Student import Student
    s = Student('egon', 18, 'male')
    s.learn()
    
    # 或者
    import Student
    s = Student.Student('jack', 28, 'male')
    s.learn()
    在不同一个文件夹下

    由于Python import模块时,是在sys.path里按顺序查找的。需要先将要使用文件的文件路径加入sys.path中。

    import sys
    sys.path.appent(r'/home/admin/PythonProject/CourseSelesct/')
    import Student
    
    s = Student.Student('egon', 18, 'male')
    s.learn()
  • 相关阅读:
    AcWing
    AcWing
    AcWing
    AcWing
    AcWing
    2019牛客国庆集训派对day1
    模板
    2019南昌网络赛H The Nth Item 矩阵快速幂
    ACwing92 递归实现指数型枚举 dfs
    ACwing91 最短Hamilton路径 状压dp
  • 原文地址:https://www.cnblogs.com/xiugeng/p/8681520.html
Copyright © 2011-2022 走看看