Suppose I have this class:
class MyClass(object):
def uiFunc(self, MainWindow):
self.attr1 = "foo"
self.attr2 = "bar"
def test():
from subclassfile import MySubClass
MySubClass.firstFunc(self, 2, 2)
test()
And this subclass in other file:
from classfile import MyClass
class MySubclass(MyClass):
def firstFunc(self, a, b):
c = a + b
d = self.secondFunc(self, c)
return d
def secondFunc(self, c):
d = c / 2
return d
This is a silly example, but represents exactly what I'm trying to do.
I want to call the second function from the subclass inside the first function.
But when I do that it returns an error that says that MyClass doesn't have such function.
So I suppose that I shouldn't be using the self in front of the function name when I call it. But when I don't it raises an error too.
So my question is: How do I access this second function inside the first function of a subclass?
EDIT:
Thank you all for the help. I'll explain better what I'm trying to do while I guess that my implementation is really bad in the code. MyClass is the main Class of the code, where all the widgets and main functions are. There is also a button on this Class that is binded with the "test" function. So when the button is pressed the whole thing should work. The MySubClass file is an auxiliary module that exports data to some file. The firstFunc is the main function in this module that handles with all the data while secondFunc just builds the template of the file. So I call this secondFunc inside the firstFunc so that the template is loaded and then the data is written in the file. I made this MySubClass inherited from the MyClass so that it could have access to the database and other variables directly from the MyClass.
So I suppose that I could do this otherway. But I'm not really experienced with this kind of implementation. Also, sorry for my bad english.
You don't call this method from an instance of MyClass, You call it from an instance of MySubclass. The problem is that you're probably getting a TypeError since the argument (self) that you're passing to firstFunc is the wrong type (MyClass instead of MySubClass). The solution is to restructure your code so this doesn't happen. At least, I assume that's the problem (when I tried you code, I got all sorts of import errors). Here's the code that I tried (which I assume illustrates the problem you're describing):
class MyClass(object):
def uiFunc(self, MainWindow):
self.attr1 = "foo"
self.attr2 = "bar"
def test(self):
return MySubClass.firstFunc(self, 2, 2)
class MySubClass(MyClass):
def firstFunc(self, a, b):
c = a + b
d = self.secondFunc(c) #removed self from parameter list
return d
def secondFunc(self, c):
d = c / 2
return d
a=MyClass()
b=MySubClass()
print (b.test()) #this works because b is the right type to pass to first/second func
a.test() #This doesn't work because a is the wrong type.
Notice that it doesn't really make sense to have test as a method of MyClass because you can't use it from there anyway. You might as well just move test onto MySubClass.
Also, as others have noted, self.secondfunc(self,c) is also very strange. This is basically equivalent to MySubClass.secondfunc(self,self,c) which has the wrong number of arguments, as you're passing self twice.
This works:
class MyClass(object):
def uiFunc(self, MainWindow):
self.attr1 = "foo"
self.attr2 = "bar"
def test(self):
return firstFunc(self, 2, 2)
def firstFunc(self, a, b):
c = a + b
d = secondFunc(self,c) #self from in front of function, now in parameter list
return d
def secondFunc(self, c):
d = c / 2
return d
class MySubClass(MyClass):
firstFunc=firstFunc
secondFunc=secondFunc
a=MyClass()
b=MySubClass()
print (b.test())
print (a.test())
But I highly recommend restructuring your code.
Change:
d = self.secondFunc(self, c)
To:
d = self.secondFunc(c)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With