I have the following code:
class User:
def __init__(self, name):
self.name = name
self.__password = ""
@property
def password(self):
if len(self.__password) == 0:
return ""
secret = self.__password[0]
for i in range(len(self.__password) - 2):
secret = "*"
secret = self.__password[-1]
return secret
@password.setter
def password(self, new_pass):
if len(new_pass) < 6:
for i in range(6 - len(new_pass)):
new_pass = "#"
self.__password = new_pass
@password.deleter
def password(self):
if len(self.__password) > 10000:
del self.__password
u = User("John")
u.password = "s"
print(u.password)
u.password = "L0nger passwords ArE M0r3 Secure 0r s0 th3y s@y!"
print(u.password)
del(u.password)
print(u.password)
The issue is that the deleter does not delete the password property and I can still print it after I call the deleter. What am I doing wrong?
CodePudding user response:
Well, you are only deleting the property when the length is >10000. The following code produces the outcome you seek:
class User:
def __init__(self, name):
self.name = name
self.__password = ""
@property
def password(self):
if len(self.__password) == 0:
return ""
secret = self.__password[0]
for i in range(len(self.__password) - 2):
secret = "*"
secret = self.__password[-1]
return secret
@password.setter
def password(self, new_pass):
if len(new_pass) < 6:
for i in range(6 - len(new_pass)):
new_pass = "#"
self.__password = new_pass
@password.deleter
def password(self):
if len(self.__password) > 10:
del self.__password
u = User("John")
u.password = "s"
print(u.password)
u.password = "L0nger passwords ArE M0r3 Secure 0r s0 th3y s@y!"
print(u.password)
del(u.password)
try:
print(u.password)
except:
print("failed!")
The outcome:
s****#
L**********************************************!
failed!