这个问题在这里已经有答案了:
不退不退(5个答案)
一小时前关门了。
当你拿一本书的例子来胡闹的时候:
class Quick():
def __str__(self):
return "\n".join(f"{k}: {v}" for k, v in vars(self).items())
class User(Quick):
def __init__(self, first, last, location, gender):
super().__init__()
self.first = first
self.last = last
self.location = location
self.gender = gender
self.loginattempt = 0
def mmplusattempt(self):
self.loginattempt += 1
def mmresetattempt(self):
self.loginattempt = 0
def __str__(self):
return "\n".join(f"{k}: {v}" for k, v in vars(self).items())
class Admin(User):
def __init__(self, first, last, location, gender):
super().__init__(first, last, location, gender)
self.privileges = ["delete post", "ban user", "shutdown server", "restart server"]
def show(self):
for p in self.privileges:
print(f"This user can: {p}. \n")
admin = Admin("ADMIN", "ADMIN", "SERVER ROOM", "ADMIN")
print(admin.show())
出于某种原因,这会输出:
This user can: delete post.
This user can: ban user.
This user can: shutdown server.
This user can: restart server.
None
我不知道为什么会打印出来 None
. 改变 show()
要检查空值:
def show(self):
for p in self.privileges:
if p != None:
print(f"This user can: {p}. \n")
静止输出 None
.
我想可能循环没有像我预期的那样工作,所以在中添加了一个空值 self.privileges
要查看循环是否正确检查空值,请执行以下操作: self.privileges = ["delete post", "ban user", "shutdown server", None, "restart server"]
输出:
└─$ python3 classuser.py
This user can: delete post.
This user can: ban user.
This user can: shutdown server.
This user can: restart server.
None
显然,if测试工作正常。这个空值从何而来?
1条答案
按热度按时间brtdzjyr1#
show
如果在函数中返回某个值,则函数没有返回任何值show
,None
我要走了。