0
class A:
 def __init__(self):
 self.i = 0
 def demo(self):
 self.a=1
class B(A):
 def __init__(self, j = 0):
 super().__init__()
 self.j = j
 print(self.i)
 self.demo()
 def demo(self):
 print(self.a)
def main():
 b = B()
 print(b.i)
 print(b.j)
main()

why am i not able to access self.a inside class b does prefixing a variable with self. will make it an instance variable Thanks

asked Jan 3, 2016 at 6:57
1
  • 1
    As question was already anwsered I would only like to point that in almost every case you SHOULD declare all instance parameters in init, even with simple self.a = None. Fist - you will never get AttributeError, second - you will have cleaner code with all your instance variables declared in the same place. Commented Jan 8, 2016 at 9:57

4 Answers 4

4

When you include a demo method for both classes, the most recently-defined one masks the others. Since you define B after you define A but before you call any of the methods in A, demo will try to access a variable that was never defined. You should either call demo within A (in __init__, probably), or change the name of demo in B to something unique, which will allow you to access both methods (probably the best approach, since they do different things and you want to make use of both).

answered Jan 3, 2016 at 7:14
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks what will be the scope of variable a is that instance or local
It is an instance variable, as its reference is self.a.
Note that calling self.demo() in A wouldn't address the issue, since it'd still get B.demo. If you were to go with that route, you'd need to explicitly invoke A.demo(self). I'd say using different names is a better solution, but the code is contrived enough and has enough other design issues that it's hard to talk about what it "should" do.
1

Because you overwrite demo method on B class.
If you want to access self.a add it to __init__ method of A class or call parent demo method like this:

 def demo(self):
 super().demo()
 print(self.a)
answered Jan 3, 2016 at 7:11

Comments

0

It is because instance variable b is not initiated within __init__ of A

answered Jan 3, 2016 at 7:08

Comments

0

Because class A.demo() is not executed:
class A: def init(self): self.i = 0 def demo(self): self.a=1

class B(A):
 def __init__(self, j = 0):
 super().__init__()
 self.j = j
 print(self.i)
 super().demo()
 self.demo()
 def demo(self):
 print(self.a)
def main():
 b = B()
 print(b.i)
 print(b.j)
main() 
answered Jan 3, 2016 at 7:17

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.