简体   繁体   中英

How do I use this variable in another file?

My method store_useful_words take some text and compare each word in the entered text and then compares those words to some keywords in my database. The impWords variable stores those words that match with the database from the sentence. I have imported WebOutput to my database file and I want to use the impWords list to iterate through. How do I use the impWords variable in the database file?

WebOutput.py file

import DatabaseInteractor
import nltk

db = DatabaseInteractor.DatabaseInteractor()

class WebOutput:

  def __init__(self,text, impWords = None):
    self.text= text
    self.impWords = self.store_useful_words()



  def store_useful_words(self):
    keywords = db.get_keywords()
    tweetWords = []
    for word in self.text.split():
      if word in keywords:
        tweetWords.append(word)
    return tweetWords

Just pass keyword to __init__ and then create instance of class. Code:

class WebOutput:
    def __init__(self, text, keywords, impWords=None):
        self.text = text
        self.keywords = keywords
        self.impWords = self.store_useful_words()

    def store_useful_words(self):
        tweetWords = []
        for word in self.text.split():
            if word in self.keywords:
                tweetWords.append(word)
        return tweetWords

# Using
db = DatabaseInteractor.DatabaseInteractor()
output = WebOutput('some_text', db.get_keywords())
for word in output.impWords:
    print(word)
    # ... do something else ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM