0

I just started to code in Python. I am using Windows 8. I have the following code and it is not working. However the code worked fine on Ubuntu. The python version is same on both OS.

 class test:
 def __init__(self):
 def add(self,a,b):
 print a+b
 class subclass(test):
 print "testing"
 s = subclass()
 s.add(1,2)

The Output is given below:

 s.add(1,2)
 AttributeError: subclass instance has no attribute 'add'

PS: There are no ident problems.

Eric
98.1k54 gold badges257 silver badges389 bronze badges
asked Dec 17, 2013 at 22:23
5
  • Are you creating a method add within __init__ method? Commented Dec 17, 2013 at 22:26
  • 1
    ahahah, so There are no ident problems means no indent errors are reported back :) You should not add, but remove some (5 it seems) spaces before add, and remove unused def __init__(self): line Commented Dec 17, 2013 at 22:30
  • Yes you will get indent error and it is because your __init__ method should contain at-least one valid line of code after declaration. If you don't want to add any just write a pass statement. dpaste.com/hold/1510285 Commented Dec 17, 2013 at 22:30
  • Thanks Aamir and Alko now I understood the basic problem. :) Commented Dec 17, 2013 at 22:32
  • 1
    "if I don't add some space before add() I am getting ident error" - making changes you don't understand until the compiler stops complaining isn't an effective way to fix bugs. Do you know why the compiler was giving you an indentation error? It's because it expected your __init__ method to have a body, but you didn't write one. Your fix moved add into the body of __init__, which is not a solution. Commented Dec 17, 2013 at 22:35

3 Answers 3

2

If your code is indented as desired then the add() function only exists within __init__() and never becomes part of the class. There is no possible way that your code could work, even if you had an instance of test instead.

answered Dec 17, 2013 at 22:26
Sign up to request clarification or add additional context in comments.

1 Comment

For the time being, for author code works seems to mean runs without exceptions raised.
1

If indentation is ok, as stated, you should add

self.add = add 

to your __init__. Right now you declare add function in local __init__ scope, but assign it nowhere, so it gets immediately garbage collected.

update

As there is no indent problems stated by author seems to refer to code being successfully executer without IndentationError raised, correct line of action seems to be equally indent __init__ and add, to make them both instance methods, and remove empty __init__ or add pass placeholder:

class test:
 def __init__(self): pass
 def add(self,a,b):
 print a+b
class subclass(test):
 print "testing"
answered Dec 17, 2013 at 22:24

Comments

0

Why you have an empty init method? If you need it, the init implementation cant be blank. You can try:

class test:
 def __init__(self):
 pass
 def add(self, a, b):
 print a+b

Or:

class test:
 def add(self, a, b):
 print a+b
answered Dec 17, 2013 at 22:36

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.