欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python基础学习(十三)

程序员文章站 2024-01-28 12:25:22
26.模块 第一个student.py name = "Song Ke" name_list = ["张三", "李四", "王五"] def who_am_i(myName): print(f'my name is : {myName}') class Student: def __init__( ......

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结果:

python基础学习(十三)

 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结果:

python基础学习(十三)