Python3关于点符号使用的混淆
这里是新的程序员,在Eric Matthes的Python速成课程的这个练习中,我在理解这个使用点符号的具体案例时遇到了一些困难。本练习来自第9-11章Imported Admin,该章介绍了类的面向对象编程。本练习希望我导入一个模块,该模块包含用于为用户建模的类集合,然后向管理员用户添加某些权限。以下是我要导入的模块:Python3关于点符号使用的混淆,python,python-3.x,oop,attributes,Python,Python 3.x,Oop,Attributes,这里是新的程序员,在Eric Matthes的Python速成课程的这个练习中,我在理解这个使用点符号的具体案例时遇到了一些困难。本练习来自第9-11章Imported Admin,该章介绍了类的面向对象编程。本练习希望我导入一个模块,该模块包含用于为用户建模的类集合,然后向管理员用户添加某些权限。以下是我要导入的模块: """A collection of classes for modeling users.""" class User(): """Represents a simple
"""A collection of classes for modeling users."""
class User():
"""Represents a simple user profile."""
def __init__(self, first_name, last_name, username, email, location):
"""Initialize attributes to decribe 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):
"""Print 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):
"""Print a personalized message to the user."""
print("\nHello, " + 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 back to 0."""
self.login_attempts = 0
class Admin(User):
"""Represents an administrator, a specific type of User."""
def __init__(self, first_name, last_name, username, email, location):
"""
Initialize attritbutes of parent class.
Then intialize attributes specific to admin.
"""
super().__init__(first_name, last_name, username, email, location)
self.privileges = Privileges() #instance attribute? Slightly confused here.
class Privileges():
"""Class to store an admin's privileges."""
def __init__(self, privileges=[]):
"""Initialize attributes for privileges."""
self.privileges = privileges
def show_privileges(self):
"""Display the privileges the Admin has."""
print("\nPrivileges:")
if self.privileges:
for privilege in self.privileges:
print("- " + privilege)
else:
print("- This user has no privileges.")
除了我拥有self.privileges=privileges之外,我了解这个模块的大部分工作原理。我认为这是AdminUser类的一个属性,它指向“Privileges”类的内部?抱歉用词不当
练习的第二部分是导入模块并创建Admin实例,然后添加某些权限:
"""Import class Admin(User)"""
from user import Admin
"""Create admin instance."""
bertrand = Admin('bertrand', 'russell', 'bruss,' 'brussell@trinity.edu','united kingdom')
bertrand.describe_user() # Apply method from parent class User()
"""Add admin privileges."""
bertrand_privileges = ['can add post',
'can delete post',
'can delete user',
]
bertrand.privileges.privileges = bertrand_privileges #I don't understand this!
"""Concatenate and apply method to show added privileges."""
print("\nThe admin " + bertrand.username +
" has these privileges: ")
bertrand.privileges.show_privileges()
当考虑行bertrand.privileges.show_privileges时,我是否正确地认为.privileges告诉Python查看实例bertrand并找到privileges属性,以便它可以从privileges类调用方法show_privileges
然而,我的主要误解是行bertrand.privileges.privileges=bertrand\u privileges。
为什么是第二个,特权?如果此行与bertrand.privileges.show_privileges行类似,那么第二个.privileges尝试引用的是什么?当我删除第二个.privileges并运行代码时,我得到一个错误。所以我认为它一定有目的。任何帮助都将不胜感激。也许我只是在理解这个实例属性的工作原理时被误导了?还有,为什么这种方法会有用呢?你的第一个假设是正确的 你问题的第二部分所发生的情况基本相同。您正在获取实例属性bertrand.Privileges,它是Privileges类的实例化版本。由于它是在没有参数的情况下实例化的,因此使用特权类init中的关键字参数,或者使用空列表
现在,当您执行bertrand.Privileges.Privileges时,您将有一个空列表,但是,由于您将此变量设置为bertrand_Privileges,它是包含bertrand的Privileges的列表,现在bertrand.Privileges.Privileges将引用此填充的列表。同样。。要显示可变对象和不可变对象之间的区别,请继续并更改列表bertrand_Privileges中的一项,然后再次调用bertrand.Privileges.show_Privileges。