0

I am trying to access variable X. Here is my code:

class A:
    def A(self):   
        self.DD = "aaaaaaaaaaaaaaaa"
class B(A):

    def __init__(self):

        Scenario.__init__(self)
    def A(self):
        self.x = self.DD
        print(self.x)
if __name__ == '__main__':
    B = B()
    B.A()

I am getting error:

---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-23-89f50bf48427> in <module>

     14 if __name__ == '__main__':

     15     B = B()

---> 16     B.A()

 

<ipython-input-22-d2fecfd66f95> in A(self)

     15     def A(self):

     16         self.xx = self.Business_Org

---> 17         self.x = self.DD

     18         print(self.x)

     19

 

AttributeError: 'B' object has no attribute 'DD'

I found a similar question here: Accessing variable outside class using Inheritence. But not answer properly. If anyone can help me it will be really appreciatable.

1 Answer 1

1

In class B, I believe you override the inherited method A() from class A. This means self.DD is never assigned as the A.A() self.DD = "aaaaaaaaaaaaaaaa"; is never called and so the objects never have the DD attribute assigned. Try this instead:

class A:
    def __init__(self):
        self.A()

    def A(self):   
        self.DD = "aaaaaaaaaaaaaaaa"
class B(A):

    def __init__(self):
        Scenario.__init__(self)

    def B(self):
        self.A()
        self.x = self.DD
        print(self.x)


if __name__ == '__main__':
    B = B()
    B.B() # aaaaaaaaaaaaaaaa
Sign up to request clarification or add additional context in comments.

1 Comment

As some further tips, you should really avoid such confusing naming, as both the class and the methods have the same name. Better would be class A() and method a(). In general it is often seen to Capitalize Classes, and keep the methods lower_case or camelCase. Best Wishes.

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.