实验5 类与模块

class StudentDoc:
    def __init__(self,number,name,major,score):
        self.number = number
        self.name = name
        self.major = major
        self.score = score

    def info(self):
        print(f'number:{self.number}')
        print(f'name:{self.name}')
        print(f'major:{self.major}')

    def info_score(self):
        print(f'score:{self.score}')

    def change_score(self,new_score):
        self.score = new_score
        print(f'score:{self.score}')

 

import student
s1 = student.StudentDoc('666666','Bob','Python','100')
s2 = student.StudentDoc('111111','Mary','Math','59')
s1.info()
s1.info_score()
print()
s2.info()
s2.info_score()
print()
s2.info()
s2.change_score('60')

 

 

 

实验总结:

1.模块化编程的好处:(1)简化维护,不必把重点放在整个项目上(2)可维护性好,即使出了问题也便于排查(3)复用性好,直接使用编写好的模块去实现功能,当需要重复实验时,再次调用即可,不必再重新编写。(4)范围性好,每个模块都有单独的命名空间,避免发生一些如变量命名上的冲突

2.包其实就是文件夹,是一个包含“__init__.py”的文件夹。因此,如想手动创建一个包,只要新建一个文件夹,文件夹的名称就是新建包的包名;然后在该文件夹中,创建一个__init__.py文件,该文件中可以不编写任何代码。

3.当直接运行一个模块时,name变量的值变为__main__;而将模块被导入其他程序中并运行该程序时,处于模块中的__name__变量的值就变为了模块名。

posted @ 2021-05-25 16:46  申富明  阅读(128)  评论(2)    收藏  举报