1

on the begin I'll say that I was looking for the answer but can't find it and sorry for so basic question.I created program with TTS. I created global variable called "list_merge", but most of you said that global variables are BAD. So I decided to put this list in init. PS. ignore whitespaces, they exist only because I copied it here.

the error is: AttributeError: 'Ver2ProjectWithTTS' object has no attribute 'list_merge'

import json
import pyttsx
from openpyxl import load_workbook

class Ver2ProjectWithTTS(object):

    def __init__(self):
        self.read_json_file()
        self.read_xml_file()
        self.say_something()
        self.list_merge = []

    def read_json_file(self):
        with open("json-example.json", 'r') as df:
            json_data = json.load(df)
            df.close()
        for k in json_data['sentences']:
            text_json = k['text']
            speed_json = int(k['speed'])
            volume_json = float(k['volume'])
            dict_json = {'text': text_json, 'speed': speed_json, 'volume': volume_json}
            self.list_merge.append(dict_json)

    def read_xml_file(self):
        tree = et.parse('xml-example.xml')
        root = tree.getroot()
        for k in range(0, len(root)):
            text_xml = root[k][0].text
            speed_xml = int(root[k][1].text)
            volume_xml = float(root[k][2].text)
            dict_xml = {'text': text_xml, 'speed': speed_xml, 'volume': volume_xml}
            self.list_merge.append(dict_xml)

    def say_something(self):
        for item in self.list_merge:
            engine = pyttsx.init()
            engine.getProperty('rate')
            engine.getProperty('volume')
            engine.setProperty('rate', item['speed'])
            engine.setProperty('volume', item['volume'])
            engine.say(cleared_text)
            engine.runAndWait()

if __name__ == '__main__':
    a = Ver2ProjectWithTTS()

I'm getting AttributeError: 'Ver2ProjectWithTTS' object has no attribute 'list_merge'

Any ideas how to avoid this error? Well i'm not good in objectivity and I just cant move on without fixing this. PS. with global variable before init def it worked properly. Thanks for help :)

6
  • what line is throwing this error? Commented Aug 17, 2016 at 7:55
  • AttributeError: 'Ver2ProjectWithTTS' object has no attribute 'list_merge' Commented Aug 17, 2016 at 7:57
  • 1
    It's hard to 'ignore whitespaces' when they are part of the syntax. Is that your actual indentation ? Commented Aug 17, 2016 at 7:57
  • You still didn't say which line is throwing this error. Commented Aug 17, 2016 at 7:58
  • I mean, there are no tabs in this code which I printed for you, but in mine it's all ok. :) I know whitespaces are important and the problem isnt with whitespaces. :) Commented Aug 17, 2016 at 7:58

1 Answer 1

4

You have to set if first before you use it:

class Ver2ProjectWithTTS(object):

    def __init__(self):
        # first set it
        self.list_merge = []
        self.read_json_file()
        self.read_xml_file()
        self.say_something()

Anyway don't do any advanced logic in constructors, it's not a good practice. Make a method instead:

class Ver2ProjectWithTTS(object):

    def __init__(self):
        # first set it
        self.list_merge = []

    def do_the_job(self):
        self.read_json_file()
        self.read_xml_file()
        self.say_something()

    ...

instance = Ver2ProjectWithTTS()
instance.do_the_job()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, great answer. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.