2

이것은 파이썬에서 문자열 인턴하기에 관한 나의 이전 질문에 대한 후속 조치입니다. 비록 그것이 별도의 질문으로 자격을 갖추기에 충분하지 않다고 생각합니다. 간단히 말해서, sys.intern을 사용할 때 대부분의/모든 용도에 해당 문자열을 함수에 전달해야합니까, 아니면 인턴을 한 번 사용하여 참조를 추적해야합니까? 내가이 올바른지 어떻게 생각 않는 의사 codeish 사용 사례와 명확히하기 위해 : 가 (주석 참조)sys.intern()은 모든 조회에 사용됩니까? 아니면 처음 문자열을 만들 때만 사용합니까? (파이썬 후속)

# stores all words in sequence, 
# we want duplicate words too, 
# but those should refer to the same string 
# (the reason we want interning) 
word_sequence = [] 
# simple word count dictionary 
word_dictionary = {} 
for line in text: 
    for word in line: # using magic unspecified parsing/tokenizing logic 
     # returns a canonical "reference" 
     word_i = sys.intern(word) 
     word_sequence.append(word_i) 
     try: 
      # do not need to intern again for 
      # specific use as dictonary key, 
      # or is something undesirable done 
      # by the dictionary that would require 
      # another call here? 
      word_dictionary[word_i] += 1 
     except KeyError: 
      word_dictionary[word_i] = 1 

# ...somewhere else in a function far away... 
# Let's say that we want to use the word sequence list to 
# access the dictionary (even the duplicates): 
for word in word_sequence: 
    # Do NOT need to re-sys.intern() word 
    # because it is the same string object 
    # interned previously? 
    count = word_dictionary[word] 
    print(count) 

내가 다른 사전에서 단어에 액세스하려면? 키를 이미 삽입 했더라도 key : value를 삽입 할 때 sys.intern()을 다시 사용해야합니까? 설명을 좀 해 주시겠습니까? 미리 감사드립니다.

답변