Python Forum
Attribute Error received not understood (Please Help)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Attribute Error received not understood (Please Help)
#1
Hello Community,

I’m having trouble understanding why the following query fails. I’ve run this in the editor a few times but it’s still not clicking conceptually. It returns the following error message “AttributeError: ‘A’ object has no attribute ‘__a’”

Can someone please help me understand?

class A:
    def __init__(self, v):
        self.__a = v + 1
a = A(0)
print(a.__a)
Larz60+ write Jun-19-2021, 02:50 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.
Reply
#2
This is due to the double underscore. This is covered under identifiers in the manual. Look for "name mangling".

The name is treated as a private name and is modified. Basically, you shouldn't use double underscores without a good reason. But the data is available under a different name.

class A:
    def __init__(self, v):
        self.__a = v + 1

a = A(0)
print(a._A__a)
Output:
1
Reply
#3
You can find these links useful
The Meaning of Underscores in Python
What is the meaning of single and double underscore before an object name?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(Jun-18-2021, 09:13 PM)crocolicious Wrote: Hello Community,

I’m having trouble understanding why the following query fails. I’ve run this in the editor a few times but it’s still not clicking conceptually. It returns the following error message “AttributeError: ‘A’ object has no attribute ‘__a’”

Can someone please help me understand?

class A:
    def __init__(self, v):
        self.__a = v + 1
a = A(0)
print(a.__a)
Reply
#5
Thank you both for your help, this is very helpful!

Croc
Reply
#6
(Jun-19-2021, 04:46 AM)buran Wrote: You can find these links useful
The Meaning of Underscores in Python
What is the meaning of single and double underscore before an object name?

Thank you very much Buran. Will read up at both links!

Thanks again,
Croc
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error: audioio has no attribute 'AudioOut' netwrok 3 630 Oct-22-2023, 05:53 PM
Last Post: netwrok
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,333 Apr-15-2023, 05:17 PM
Last Post: deanhystad
Question How to understand the received bytes of ser.read? pf2022 3 1,967 Mar-24-2022, 11:37 AM
Last Post: pf2022
  Getting 'NoneType' object has no attribute 'find' error when WebScraping with BS Franky77 2 5,248 Aug-17-2021, 05:24 PM
Last Post: Franky77
  f-string error message not understood Skaperen 4 3,332 Mar-16-2021, 07:59 PM
Last Post: Skaperen
  error in scapy attribute 'haslayer' evilcode1 5 6,517 Mar-02-2021, 11:19 AM
Last Post: evilcode1
  attribute error instead of correct output MaartenRo 2 2,184 Aug-28-2020, 10:22 AM
Last Post: Larz60+
  attribute error stumped on how to fix it. hank4eva 7 4,719 Aug-11-2020, 04:47 AM
Last Post: hank4eva
  Attribute Error - trying to create a pixel array out of PNG files The_Sarco 1 2,004 Apr-29-2020, 07:10 PM
Last Post: deanhystad
  Attribute Error for Rename / Replace warden89 6 7,832 Jan-07-2020, 06:08 PM
Last Post: warden89

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020