Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly extend other classes in Python? (python v3.3)

Tags:

python

oop

class

I've started to learn python in the past few days, and while exploring object-oriented programming I'm running into problems. I'm using Eclipse while running the pydev plugin, am running on the python 3.3 beta, and am using a windows 64 bit system.

I can initialize a class fine and use any methods within it, as long as I'm not trying to extend the superclass (each class I've coded in a different source file) For example, the following code compiles and runs fine.

class pythonSuper:
    string1 = "hello"
    def printS():
        print pythonSuper.string1

and the code to access and run it...

from stackoverflow.questions import pythonSuper
class pythonSub:
    pysuper = pythonSuper.pythonSuper()
    pysuper.printS()

Like I said, that works. The following code doesn't

class pythonSuper: """Same superclass as above. unmodified, except for the spacing"""
string1 = "hello"
def printS(self):
    print(pythonSuper.string1)

Well, that's not quite true. The superclass is absolutely fine, at least to my knowledge. It's the subclass that weirds out

from stackoverflow.questions import pythonSuper
class pythonSub(pythonSuper):
    pass

pythonObject = pythonSub()
pythonSub.pythonSuper.printS()

when the subclass is run Eclipse prints out this error

Traceback (most recent call last):
   File "C:\Users\Anish\workspace\Python 3.3\stackoverflow\questions\pythonSub.py",           
   line 7, in <module>
class pythonSub(pythonSuper):
TypeError: module.__init__() takes at most 2 arguments (3 given)

I have no idea what's going on. I've been learning python from thenewboston's tutorials, but those are outdated (I think his tutorial code uses python version 2.7). He also codes in IDLE, which means that his classes are all contained in one file. Mine, however, are all coded in files of their own. That means I have no idea whether the code errors I'm getting are the result of outdated syntax or my lack of knowledge on this language. But I digress. If anyone could post back with a solution and/or explanation of why the code is going wrong and what I could do to fix it. An explanation would be preferred. I'd rather know what I'm doing wrong so I can avoid and fix the problem in similar situations than just copy and paste some code and see that it works. Thanks, and I look forward to your answers

like image 924
pipsqueaker117 Avatar asked Jul 21 '12 21:07

pipsqueaker117


1 Answers

I ran your code, albeit with a few modifications and it runs perfectly. Here is my code:

pythonSuper:

class pythonSuper:
    string1 = 'hello'
    def printS(self):
        print(self.string1)

main:

from pythonSuper import pythonSuper as pySuper

class pythonSub(pySuper):
    pass

pythonObject = pythonSub()
pythonObject.printS()

NOTE: The change I have made to your code is the following:

In your code, you have written pythonSub.pythonSuper.printS() which is not correct, because via pythonSub you already support a printS() method, directly inherited from the superclass. So there is no need to refer to the superclass explicitly in that statement. The statement that I used to substitute the aforementioned one, pythonObject.printS(), seems to have addressed this issue.

like image 196
NlightNFotis Avatar answered Oct 13 '22 00:10

NlightNFotis