cconori
cconori

Reputation: 1

AttributeError: type object 'WordFamily' has no attribute 'words

when I call the check_type function, it would appear AttributeError: type object 'WordFamily' has no attribute 'words'

class WordFamily:
  def __init__(self, words, suffixes):
    self.words = words
    self.suffixes = suffixes
    
  def check_type(word):
    for w in WordFamily.words:
      if word in w or word + WordFamily.suffixes in w:
        return WordFamily.words
verb = ["love", "hate"]
WordFamily(verb,[""])
WordFamily.check_type("love")

What I expect is "verb"

what appear is AttributeError: type object 'WordFamily' has no attribute 'words' Error in this line: for w in WordFamily.words:

Upvotes: 0

Views: 39

Answers (1)

Cory Kramer
Cory Kramer

Reputation: 117886

I think you should refactor your class so check_type accepts self as the first argument and you can therefore reference instance variables. Otherwise check_type is trying to act like a @class_method but the values are not intended to act that way in your case.

class WordFamily:
  def __init__(self, words, suffixes):
    self.words = words
    self.suffixes = suffixes
    
  def check_type(self, word):
    for w in self.words:
      if word in w or word + self.suffixes in w:
        return self.words

Then you would use this like

verb = ["love", "hate"]
wf = WordFamily(verb,[""])
wf.check_type("love")

Upvotes: 1

Related Questions