Skip to content

Calling a class method from within another class method causes AttributeError #136727

@phucly01

Description

@phucly01

Bug report

Bug description:

class A:
    def __init__(self, cash):
        self.msg = "I am A"
        self.cash = cash
    def __iama(self):
        print(f"{self.msg} and have ${self.cash}")


class B:
    def __init__(self):
        self.A = A(5)
    
    def show(self):
        self.A.__iama()

if __name__ == '__main__':
    B().show()

Error:

Traceback (most recent call last):
  File "/home/fmadmin/workspace/test_automation/src/./example.py", line 19, in <module>
    B().show()
  File "/home/fmadmin/workspace/test_automation/src/./example.py", line 16, in show
    self.A.__iama()
    ^^^^^^^^^^^^^
AttributeError: 'A' object has no attribute '_B__iama'. Did you mean: '_A__iama'?

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions