python从入门到实践 9-11 导入Admin 类 : 以为完成练习9-8而做的工作为基础, 将User 、 Privileges 和Admin 类存储在一个模块中, 再创建一个文件, 在其中创建
2020-06-04 05:12
1001 查看
9-11 导入Admin 类 : 以为完成练习9-8而做的工作为基础, 将User 、 Privileges 和Admin 类存储在一个模块中, 再创建一个文件, 在其中创建一个Admin 实例
并对其调用方法show_privileges() , 以确认一切都能正确地运行。
#a9_11.py class User(): """Represent a simple user profile.""" def __init__(self, first_name, last_name, username, email, location): """Initialize the user.""" self.first_name = first_name.title() self.last_name = last_name.title() self.username = username self.email = email self.location = location.title() self.login_attempts = 0 def describe_user(self): """Display a summary of the user's information.""" print("\n" + self.first_name + " " + self.last_name) print(" Username: " + self.username) print(" Email: " + self.email) print(" Location: " + self.location) def greet_user(self): """Display a personalized greeting to the user.""" print("\nWelcome back, " + self.username + "!") def increment_login_attempts(self): """Increment the value of login_attempts.""" self.login_attempts += 1 def reset_login_attempts(self): """Reset login_attempts to 0.""" self.login_attempts = 0 class Admin(User): """A user with administrative privileges.""" def __init__(self, first_name, last_name, username, email, location): """Initialize the admin.""" super().__init__(first_name, last_name, username, email, location) # Initialize an empty set of privileges. self.privileges = Privileges() class Privileges(): """A class to store an admin's privileges.""" def __init__(self, privileges=[]): self.privileges = privileges def show_privileges(self): for privilege in self.privileges: print("- " + privilege)
#9-11test from a9_11 import Admin, Privileges eric = Admin('eric', 'matthes', 'e_matthes', 'e_matthes@example.com', 'alaska') eric_privileges = [ 'can reset passwords', 'can moderate discussions', 'can suspend accounts', ] eric.privileges.privileges = eric_privileges eric.privileges.show_privileges() #注意,eric.privileges.show_privileges()而不是其他
相关文章推荐
- Python基础练习(制作一个"密码薄",其可以存储一个网址(例如 www.itcast.cn),和一个密码(例如 123456),请编写程序完成这个“密码薄”的增删改查功能,并且实现文件存储功能)
- 创建一个模块calculator.py,完成任意两个数的加(add)、减(sub)、乘(mult)、除(div)运算;导入该模块,分别调用其中的函数,完成如下操作: 1、25+56 2、86-68 3
- Python3基础 创建一个模块 导入并调用其中的函数 主代码与模块代码在同一文件夹下
- 基础入门_Python-模块和包.运维开发中__import__动态导入最佳实践?
- python_基础总结1 python由来 字符编码 注释 pyc文件 python变量 导入模块 获取用户输入 流程控制if while
- 零基础入门学习Python(11)--列表:一个打了激素的数组(2)
- python基础练习之读取一个文件,显示除了以井号(#)开头的行以外的所有行
- 基础入门_Python-模块和包.运维开发中内建模块getopt的最佳实践?
- 基础入门_Python-模块和包.运维开发中watchdog事件监视的最佳实践?
- Python学习入门基础教程(learning Python)--5.7 Python文件数据记录存储与处理
- 依次读取文件中的一个字符 分类: python基础学习 python 小练习 2013-08-22 11:14 289人阅读 评论(0) 收藏
- 基础入门_Python-模块和包.运维开发中日志模块logging的最佳实践?
- Python3基础 getatime getctime getmtime 获取一个文件的最近访问时间 创建时间 最新修改时间
- 【微专业:Pthon职场实用技能(6期)】模块一:Python零基础入门-22.类的创建和调用
- python实践项目十:zipfile模块-将一个文件夹备份到一个 ZIP 文件
- 零基础入门学习Python(30)--文件系统:介绍一个高大上的东西
- 用python os模块完成查询一个目录下当前的文件总数,和文件个数,俩者之差为文件夹个数
- 基础入门_Python-模块和包.运维开发中MySQLdb CRUD操作的最佳实践?
- 基础入门_Python-模块和包.深入Celery之应用配置/独立模块配置实践?
- 1)创建一个工作空间和功能包,然后在功能包中完成如下工作,并使用launch文件启动涉及的节点。 2)话题和服务编程:通过代码新生一只海龟,放置在(5,5)点,命名为“turtle2”;通过代码订阅t