您的位置:首页 > 编程语言 > Python开发

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()而不是其他
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐