-3

I'm writing simpe OOP programm in Python. The task is to write object's attributes in txt file. I've tried numerous methods but every time I get AttributeError: 'Message' object has no attribute 'self'. I've been changing file.write(ms1.self.__id) numerous time but no one helped.

class Message:
def __init__(self, id=10000000, subject='Title', text='Sample Text ', created_at='11.11.11', seen_at='11.11.11', support_group='sample text'):
    self.__id = id
    self.__subject = subject
    self.__text = text
    self.__created_at  = created_at
    self.__seen_at = seen_at
    self.__support_group = support_group
ms1 = Message(5775575, 'Order Telephone', 'The order is: Iphone 12 Pro Max 512 gb ', 'Created at: 
30.03.20', 'Seen at: 01.04.20', 'Account: Kim2030 \n Tech: Eldorado \n Billing: 5169147129584558 \n 
Order: 28048')
file = open('testfile.txt', 'w')

file.write(ms1.self.__id)
file.write(ms1.self.__subject)
file.write(ms1.self.__text)
file.write(ms1.self.__created_at)
file.write(ms1.self.__seen_at)
file.write(ms1.self.__support_group)

file.close()
  • 2
    Just remove all those `self`s... `ms1.self.__id ==> ms1.__id` etc. – Tomerikoo Nov 30 '20 at 16:11
  • 1
    Please edit to make sure that the code you posted reflects the *exact* indentation of your *actual* code. You need to add an *extra* four spaces in front of *each* line. You can do this easily by selecting the entire code block and pressing the `{}` button. – Karl Knechtel Nov 30 '20 at 16:11
  • @Tomerikoo I've tried it but i get `'Message' object has no attribute '__id'` – MaybeYesMaybeNo Nov 30 '20 at 16:15
  • 1
    Then please [edit] your question to include a ***working*** [mre] – Tomerikoo Nov 30 '20 at 16:17

1 Answers1

1

You're using the syntax slightly incorrectly. Remove the self references and don't use underscores for variable prefixes:

class Message:
    def __init__(self, id=10000000, subject='Title', text='Sample Text ', created_at='11.11.11', seen_at='11.11.11',
                 support_group='sample text'):
        self.id = id
        self.subject = subject
        self.text = text
        self.created_at = created_at
        self.seen_at = seen_at
        self.support_group = support_group

ms1 = Message(5775575, 'Order Telephone', 'The order is: Iphone 12 Pro Max 512 gb ', 'Created at:30.03.20', 'Seenat: 01.04.20', 'Account: Kim2030 \nTech: Eldorado \nBilling: 5169147129584558 \nOrder: 28048')
file = open('testfile.txt', 'w')

file.write(str(ms1.id))
file.write(ms1.subject)
file.write(ms1.text)
file.write(ms1.created_at)
file.write(ms1.seen_at)
file.write(ms1.support_group)

file.close()
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
JBirdVegas
  • 10,855
  • 2
  • 44
  • 50
  • I get error `'Message' object has no attribute 'id'` – MaybeYesMaybeNo Nov 30 '20 at 16:15
  • I don't get that error when I run the code above. The attribute `id` exists above – JBirdVegas Nov 30 '20 at 16:18
  • 1
    An answer that just fixes the problem without explaining the root cause is pretty unhelpful. Here's a full explanation of the reason this happens: https://stackoverflow.com/questions/1301346/what-is-the-meaning-of-single-and-double-underscore-before-an-object-name – Pranav Hosangadi Nov 30 '20 at 16:32