class类初始化之后调用赋值问题记录
生活随笔
收集整理的這篇文章主要介紹了
class类初始化之后调用赋值问题记录
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
class PWSTRDELL:def __init__(self, pw_str):#該方法在類實例化時會自動調用self.pw = pw_strself.strength_level = 0#通過初始化函數從外部傳入def pw_str_process(self):#這里不用在傳入password,直接用self.pw就可以了if len(self.pw)>=8:self.strength_level +=1else:print("密碼至少8位")if self.pw_hasnumer():#雖然需要處理self.pw,但不必須要明顯傳入。因為def函數定義的時候參數是selfself.strength_level +=1else:print("密碼至少含1位數字")if self.pw_hasalpha():self.strength_level+=1else:print("密碼至少含1位字母")def pw_hasnumer(self):has_number = Falsefor i in self.pw:if i.isnumeric():has_number = Truebreakreturn has_numberdef pw_hasalpha(self):has_alpha = Falsefor i in self.pw:if i.isalpha():has_alpha = Truebreakreturn has_alphaclass FILETOOL:def __init__(self, path, line):self.filepath = pathself.line = line#"密碼:{},強度:{},密碼強度級別:{}\n".format(pw_str, pw_strong,pw_strong_word(pw_strong))def write_file(self):f = open(self.filepath, "a")f.write(self.line)f.close()def read_file(self):f = open(self.filepath,"r")lines= f.readlines()f.close()return lines
def main():x = 5path ="pwd_03.txt"line=""file_process = FILETOOL(path, line)while x > 0:pw_str = input("請輸入密碼-需包含至少8位,由字母數字組成的密碼")#實例化class類的對象,傳入參數,初始化類pw_tool = PWSTRDELL(pw_str)#類的初始化函數要求一個輸入,所以這里需要輸入一個函數#類初始化之后開始調用類的處理
pw_tool.pw_str_process()print("輸入密碼{}的密碼強度為{},".format(pw_str,pw_tool.strength_level))line = "輸入密碼{}的密碼強度為{},".format(pw_str,pw_tool.strength_level)file_process.write_file()if pw_tool.strength_level>=3:print("恭喜,密碼設置成功")breakelse:print("密碼不符合要求,請重新設置\n")x -= 1else:print("嘗試次數太多,請稍后再試")print(file_process.read_file())
if __name__=="__main__":main()
或者用下面的方法:
class定義時去掉line,只在方法內引入line class PWSTRDELL:def __init__(self, pw_str):#該方法在類實例化時會自動調用self.pw = pw_strself.strength_level = 0#通過初始化函數從外部傳入def pw_str_process(self):#這里不用在傳入password,直接用self.pw就可以了if len(self.pw)>=8:self.strength_level +=1else:print("密碼至少8位")if self.pw_hasnumer():#雖然需要處理self.pw,但不必須要明顯傳入。因為def函數定義的時候參數是selfself.strength_level +=1else:print("密碼至少含1位數字")if self.pw_hasalpha():self.strength_level+=1else:print("密碼至少含1位字母")def pw_hasnumer(self):has_number = Falsefor i in self.pw:if i.isnumeric():has_number = Truebreakreturn has_numberdef pw_hasalpha(self):has_alpha = Falsefor i in self.pw:if i.isalpha():has_alpha = Truebreakreturn has_alphaclass FILETOOL:def __init__(self, path):self.filepath = path#self.line = line#"密碼:{},強度:{},密碼強度級別:{}\n".format(pw_str, pw_strong,pw_strong_word(pw_strong))def write_file(self, line):#寫入的line不是類的屬性,而是這個方法需要寫入linef = open(self.filepath, "a")f.write(line)f.close()def read_file(self):f = open(self.filepath,"r")lines= f.readlines()f.close()return lines def main():x = 5path ="pwd_03.txt"line=""file_process = FILETOOL(path)while x > 0:pw_str = input("請輸入密碼-需包含至少8位,由字母數字組成的密碼")#實例化class類的對象,傳入參數,初始化類pw_tool = PWSTRDELL(pw_str)#類的初始化函數要求一個輸入,所以這里需要輸入一個函數#類初始化之后開始調用類的處理 pw_tool.pw_str_process()print("輸入密碼{}的密碼強度為{}\n".format(pw_str,pw_tool.strength_level))line = "輸入密碼{}的密碼強度為{}\n".format(pw_str,pw_tool.strength_level)#file_process.line = "輸入密碼{}的密碼強度為{}\n".format(pw_str,pw_tool.strength_level) file_process.write_file(line)if pw_tool.strength_level>=3:print("恭喜,密碼設置成功")breakelse:print("密碼不符合要求,請重新設置\n")x -= 1else:print("嘗試次數太多,請稍后再試")print(file_process.read_file()) if __name__=="__main__":main()
上面這段代碼里,write的內容是空的,因為初始化的時候line寫了是空字符串,雖然下面line重新賦值了,但是并沒有改變類里line的值
上述line那行,需要改成
file_process.line = "輸入密碼{}的密碼強度為{}\n".format(pw_str,pw_tool.strength_level) 這樣才會寫入值或者用下面的方法:
class定義時去掉line,只在方法內引入line class PWSTRDELL:def __init__(self, pw_str):#該方法在類實例化時會自動調用self.pw = pw_strself.strength_level = 0#通過初始化函數從外部傳入def pw_str_process(self):#這里不用在傳入password,直接用self.pw就可以了if len(self.pw)>=8:self.strength_level +=1else:print("密碼至少8位")if self.pw_hasnumer():#雖然需要處理self.pw,但不必須要明顯傳入。因為def函數定義的時候參數是selfself.strength_level +=1else:print("密碼至少含1位數字")if self.pw_hasalpha():self.strength_level+=1else:print("密碼至少含1位字母")def pw_hasnumer(self):has_number = Falsefor i in self.pw:if i.isnumeric():has_number = Truebreakreturn has_numberdef pw_hasalpha(self):has_alpha = Falsefor i in self.pw:if i.isalpha():has_alpha = Truebreakreturn has_alphaclass FILETOOL:def __init__(self, path):self.filepath = path#self.line = line#"密碼:{},強度:{},密碼強度級別:{}\n".format(pw_str, pw_strong,pw_strong_word(pw_strong))def write_file(self, line):#寫入的line不是類的屬性,而是這個方法需要寫入linef = open(self.filepath, "a")f.write(line)f.close()def read_file(self):f = open(self.filepath,"r")lines= f.readlines()f.close()return lines def main():x = 5path ="pwd_03.txt"line=""file_process = FILETOOL(path)while x > 0:pw_str = input("請輸入密碼-需包含至少8位,由字母數字組成的密碼")#實例化class類的對象,傳入參數,初始化類pw_tool = PWSTRDELL(pw_str)#類的初始化函數要求一個輸入,所以這里需要輸入一個函數#類初始化之后開始調用類的處理 pw_tool.pw_str_process()print("輸入密碼{}的密碼強度為{}\n".format(pw_str,pw_tool.strength_level))line = "輸入密碼{}的密碼強度為{}\n".format(pw_str,pw_tool.strength_level)#file_process.line = "輸入密碼{}的密碼強度為{}\n".format(pw_str,pw_tool.strength_level) file_process.write_file(line)if pw_tool.strength_level>=3:print("恭喜,密碼設置成功")breakelse:print("密碼不符合要求,請重新設置\n")x -= 1else:print("嘗試次數太多,請稍后再試")print(file_process.read_file()) if __name__=="__main__":main()
?
轉載于:https://www.cnblogs.com/qiu-1010/p/10672183.html
總結
以上是生活随笔為你收集整理的class类初始化之后调用赋值问题记录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python3 模块相关及输入输出模式
- 下一篇: webpack打包问题