• python基础学习(十三)


    26.模块

    第一个student.py

    name = "Song Ke"
    
    name_list = ["张三", "李四", "王五"]
    
    
    def who_am_i(myName):
        print(f'my name is : {myName}')
    
    
    class Student:
        def __init__(self, name, age, sex):
            self.__name = name
            self.__age = age
            self.__sex = sex
    
        def who_am_i(self):
            print(f'i am a student , {self.__name}, {self.__age}, {self.__sex}')

    第二个app.py

    # 模块
    # PYTHONPATH
    import student
    print(student.name)
    print(student.name_list)
    student.who_am_i("yang")
    
    import sys
    print("搜索路径-------")
    for line in sys.path:
        print(line)

    run结果:

     27.模块2.0

    test1:会报错

    import student
    
    # Traceback (most recent call last):
    #   File "/home/yangbin/Study/IdeaProjects/pythonWork/day4/module/app1.py", line 2, in <module>
    #     sk = Student("sk", 21, "男")
    # NameError: name 'Student' is not defined
    sk = Student("sk", 21, "")
    sk.who_am_i()
    
    print("-----------------1------------------------")
    # 同名会bb不会替换yang
    name = "bb"
    # import 另外导入形式     * 导出所有属性
    from student import Student,name,name_list,who_am_i
    # 这个cc会替换 name = "Song Ke"
    name = "cc"
    
    skk = Student("skk", 22, "")
    skk.who_am_i()
    
    print("-------------------2-------------------")
    print(name)
    who_am_i("yang")
    
    print("--------------------3--------------------")
    #  别名
    import student as s
    sk = s.Student("sk1", 21, "")
    sk.who_am_i()
    
    
    # dir 查看所有属性
    print(dir(s))

    test2:

    import student
    
    # Traceback (most recent call last):
    #   File "/home/yangbin/Study/IdeaProjects/pythonWork/day4/module/app1.py", line 2, in <module>
    #     sk = Student("sk", 21, "男")
    # NameError: name 'Student' is not defined
    sk = student.Student("sk", 21, "")
    sk.who_am_i()
    
    print("-----------------1------------------------")
    # 同名会bb不会替换yang
    name = "bb"
    # import 另外导入形式     * 导出所有属性
    from student import Student,name,name_list,who_am_i
    # 这个cc会替换 name = "Song Ke"
    name = "cc"
    
    skk = Student("skk", 22, "")
    skk.who_am_i()
    
    print("-------------------2-------------------")
    print(name)
    who_am_i("yang")
    
    print("--------------------3--------------------")
    #  别名
    import student as s
    sk = s.Student("sk1", 21, "")
    sk.who_am_i()
    
    
    # dir 查看所有属性
    print(dir(s))

    run结果:

  • 相关阅读:
    20190817-T1-LOJ6322「雅礼国庆 2017 Day6」Star Way To Heaven
    20190817-涪
    20190816-周期
    考试总结 模拟95
    考试总结 模拟94
    考试总结 模拟93
    考试总结 模拟92
    考试总结 模拟91
    考试总结 模拟90
    考试总结 模拟89
  • 原文地址:https://www.cnblogs.com/songxiaoke/p/11891745.html
Copyright © 2020-2023  润新知