2017-12-31 60 views
0

OOP (일반적으로 함수 프로그래밍에 사용됨)를 배우고 있습니다. 이 작은 스크립트는 여기에서 실행되지만 개체의 데이터 값을 영구 저장하는 방법을 잘 모르겠습니다 (이 경우 개체의 인스턴스 이름은 count이며 인스턴스에 self.count를 저장하려고합니다). '텍스트'의 '0 ['여기 '개체의 인스턴스에 값을 올바르게 저장하는 방법은 무엇입니까?

,'A ','무리 '을'이다 '

총 단어 수는 : 여기

#! /usr/bin/python3 

class WordCounter: 
    def __init__(self, count = 0): 
     self.__count = count 

    def set_count(self, path): 
     try: 
      nfile = open(path, "r") 
     except: 
      print("Could not open file") 
     fileList = [] 
     lowerCaseList = [] 
     line = nfile.read() 
     fileList.append(line.split()) 
     nfile.close 
     lowerCaseList = [word.lower() for word in fileList[0]] 
     print(lowerCaseList) 
     self.count = len(lowerCaseList) 

    def get_word_count(self): 
     return self.count 

    def __str__(self): 
     return "Total word count is: " + str(self.__count) 

if __name__ == "__main__": 
    count = WordCounter() 
    print(count) 
    count.set_count("/home/swim/Desktop/word_counter.txt") 
    print(count.get_word_count()) 
    print(count) 

그리고

는 출력 ',' ',' ',' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '할 수있다.

총 단어 수 : 0

get과 set 메서드가 제대로 작동하는지 알 수 있습니다. 하지만 self.count 변수를 count 객체에 저장하려면 어떻게해야합니까?

print (count)는 get/set의 전후에 0으로 평가됩니다.

내가 라인 count.set_count 후뿐만 아니라 STR 방법 (16)를 인쇄 할 기대했다 ("/ 경로// 내/텍스트/파일")

어떤 조언을? 다시

self.count = count

+2

각 방법에 self.__count 또는 self.count를 사용하는 이유는'STR (계산 .__ 자기)'이 아니라'STR (self.count을)'이 ? 'self.count = len (lowerCaseList)'와'self .__ count = count '와 같은 이름으로 속성을 두 번 정의했지만, 아마도 그것들은 같은 것으로 의도했다. 또한,'nfile.close'는 close 메소드를 호출하기 위해 괄호가 필요하기 때문에 파일을 닫지 않습니다. – roganjosh

+0

왜 수를 저장하는 데 2 ​​개의 변수를 사용합니까? –

+0

고맙습니다. 나는 무표정한 hahaha 인 것 같은 기분이 든다. 그 점을 지적 해 주셔서 감사합니다. __str__ 메서드를 사용하여 16을 올바르게 인쇄하고 인스턴스에 올바르게 저장했습니다. –

답변

0
#! /usr/bin/python3 

class WordCounter: 
    def __init__(self, count = 0): 
     self.__count = count 

    def set_count(self, path): 
     try: 
      nfile = open(path, "r") 
     except: 
      print("Could not open file") 
     fileList = [] 
     lowerCaseList = [] 
     line = nfile.read() 
     fileList.append(line.split()) 
     nfile.close() 
     lowerCaseList = [word.lower() for word in fileList[0]] 
     print(lowerCaseList) 
     self.__count = len(lowerCaseList) 

    def get_word_count(self): 
     return self.__count 

    def __str__(self): 
     return "Total word count is: " + str(self.__count) 

if __name__ == "__main__": 
    count = WordCounter() 
    print(count) 
    count.set_count("/home/swim/Desktop/word_counter.txt") 
    print(count.get_word_count()) 
    print(count) 

감사 roganjosh을 self.countself.__count가 동일한 속성 있어야되지만 주어진 지적에 대해 : 당신이 당신의 __init__ 문에서 하나 개의 라인을 변경하는 경우

0

감사 다른 이름. 당신의 init 방법에서

0

당신은 당신이 self.__count 다른 self.count를 사용하여 getter 및 setter 방법 반면에 당신이 __count을 설정 self.__count = count을 사용했다. 그런 다음 __str__ 메서드에서 self.__count을 사용하고 __init__0으로 변경하고 getter 또는 setter에서 self.__count의 값을 절대 변경하지 마십시오.

그러니 각 방법

관련 문제