简体   繁体   中英

Python: object has no attribute

I have two classes: MyClass and MyClass2. For MyClass I took a file and returned every word on that file. In MyClass2 I inherited MyClass and basically wrote a code that was supposed to store all the words in a dictionary along with a frequency of the words as the values. The first class I already tested and it was able to return every word. MyClass2 I thought I had written correctly, but I think I'm not inheriting MyClass right or my iter method is written incorreclty. Every time I try to run my code, it returns an error. Since this is a homework assignment, (Also I don't want to be thought of as cheating..) I won't post all my code unless it is necessary to answer my questions nor will I be expecting anyone to rewrite or completely fix my code. I just need some guidance on whether or not my constructor is wrong or if the entire code is wrong, or if I'm just not formatting my code correctly and inheriting the class wrong...? I am new to python and I just need help.

from myclass import MyClass
class MyClass2(MyClass):
      def __init__(self, Dict):    #Is the problem within the constructor?
          self.Dict = Dict
          Dict = {}
      def dict(self, textfile):
          text = MyClass(textfile)    #Was I wrong here??
          ..................
              ..............
              ..............
              return self.Dict
      def __iter__(self):
          for key, value in self.Dict.items():
              yield key, value

When I run a test code, I get an error that says :

AttributeError: 'MyClass2' object has no attribute 'items'

Please let me know if I am missing anythign or if there isn't enough information.

I tested it using this code which was given:

filename = MyClass1('name of file')
y = MyClass2(filename)
for x in y:
    print x

Here is the traceback:

Traceback (most recent call last):
File "C:\myclass.py", line 25, in <module>
  for x in y:
File "C:\myclass2.py", line 19, in __iter__
  for key, value in self.Dict.items():
AttributeError: 'MyClass2' object has no attribute 'items'

Your variables' naming is quite weird. I'll try to unwind it:

from myclass import MyClass
class MyClass2(MyClass):
      def __init__(self, Dict):
          self.Dict = Dict
          Dict = {}
      def __iter__(self):
          for key, value in self.Dict.items():
              yield key, value

filename = MyClass1('name of file')
y = MyClass2(filename)

Here, filename is not a file name (which I would suspect of being a str or unicode ). Maybe it is an object somehow containing the file name. (The naming MyClass1 is not very helpful.)

This object, referred to by filename is given to MyClass2.__init__() . There it gets put into self.Dict . And then, the parameter Dict is set to {} , which is quite pointless.

Alas, I don't know what you want to achieve. Maybe you want something like

class MyClass2(MyClass):
      def __init__(self, filename):
          self.filename = filename
          self.Dict = {}
      def __iter__(self):
          for key, value in self.Dict.items():
              yield key, value

Note: It would be better to name variables in lowercase. And don't rename Dict just do dict , but name it in a way that a reader can see what it is supposed to mean.

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