VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > 简明python教程 >
  • 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结果:


相关教程