Simple Inherit from class in Python throws error
Hi I just started with Python, I'm currently developing a UI testing application for mobile devices and I have to work on a custom rendered Softkeyboard.
Button.py
class Button(): def __init__(self, name, x, y, x2=None, y2=None): self.name = name self.x = x self.y = y self.x2 = x2 self.y2 = y2
KeyboardKey.py
import Button class KeyboardKey(Button): def __init__(self, name, x, y): super(self.__class__, self).__init__(name, x, y)
That's my error:
Traceback (most recent call last): File "/home/thomas/.../KeyboardKey.py", line 2, in class KeyboardKey(Button): TypeError: Error when calling the metaclass bases module.__init__() takes at most 2 arguments (3 given)
The way you do in your code, you inherit from module Button
, not a class. You should inherit class Button.Button
instead.
In order to avoid this in future, I strongly suggest to name modules with lowercase, and capitalize classes. So, better naming would be:
import button
class KeyboardKey(button.Button):
def __init__(self, name, x, y):
super(self.__class__, self).__init__(name, x, y)
Modules in python are normal objects (of type types.ModuleType
), can be inherited, and have __init__
method:
>>> import base64
>>> base64.__init__
<method-wrapper '__init__' of module object at 0x00AB5630>
See usage:
>>> base64.__init__('modname', 'docs here')
>>> base64.__doc__
'docs here'
>>> base64.__name__
'modname'
链接地址: http://www.djcxy.com/p/40930.html
上一篇: 为什么在[False,True]中不是(True)“返回False?
下一篇: 简单从Python中的类继承将引发错误