zoukankan      html  css  js  c++  java
  • 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结果:

  • 相关阅读:
    快速排序算法
    CopyOnWrite容器
    map函数的使用
    Hashmap的源码分析
    Tree数据从后台处理类
    Java开学考试感受
    第49讲:乱入——生成器
    第48讲:魔方方法——迭代器
    第47讲:魔方方法——定制序列
    第46讲:魔方方法——描述符(property的原理)
  • 原文地址:https://www.cnblogs.com/songxiaoke/p/11891745.html
Copyright © 2011-2022 走看看