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

I started learning python in the last few days, and while studying object oriented programming, I am having problems. I use Eclipse when I run the pydev plugin, I run the beta version of python 3.3 and use the 64-bit Windows system.

I can initialize the class perfectly and use any methods inside it until I try to extend the superclass (each class that I encoded in a different source file) For example, the following code compiles and works fine.

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

and code to access and run ...

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

As I said, this works. The following code is not

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

Well, that’s not entirely true. The superclass is absolutely beautiful, at least as far as I know. This is a subclass that suffers from

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

when starting a subclass, Eclipse produces 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 is going on. I studied python from thenewboston tutorials, but they are deprecated (I think its tutorial code uses python version 2.7). It also encodes IDLE, which means its classes are all contained in a single file. However, the mines are encoded in their own files. This means that I have no idea whether the code errors that I receive are the result of outdated syntax or lack of knowledge in this language. But I was distracted. If someone can send a message using a solution and / or explanation of why the code is going wrong and what I can do to fix it. An explanation would be preferable. I would rather know what I'm doing wrong, so I can avoid and fix the problem in such situations than just copy and paste the code and see how it works. Thank you and I look forward to hearing your answers.

+6
source share
2 answers

I ran your code, albeit with some changes, and it works just fine. Here is my code:

pythonSuper:

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

home:

 from pythonSuper import pythonSuper as pySuper class pythonSub(pySuper): pass pythonObject = pythonSub() pythonObject.printS() 

NOTE. The change made to your code is as follows:

In your code, you wrote pythonSub.pythonSuper.printS() , which is incorrect, because through pythonSub you already support the printS() method directly inherited from the superclass. Therefore, there is no need to refer to the superclass explicitly in this expression. The statement I used to replace the above, pythonObject.printS() , seems to have addressed this issue.

+3
source

pythonSuper refers to a module, not a class.

 class pythonSub(pythonSuper.pythonSuper): pass 
0
source

Source: https://habr.com/ru/post/921038/


All Articles