What is causing “unbound method __init__() must be called with instance as first argument” from this Python code?

爱⌒轻易说出口 提交于 2019-12-04 02:30:35

You are doing:

Thread.__init__() 

Use:

Thread.__init__(self) 

Or, rather, use super()

This is a frequently asked question at SO, but the answer, in brief, is that the way you call your superclass's constructor is like:

super(Timer,self).__init__()

First, the reason you must use:

Thread.__init__(self)

instead of

Thread.__init__()

is because you are using the class name, and not an object (an instance of the class), so you cannot call a method in the same way as an object.

Second, if you are using Python 3, the recommended style for invoking a super class method from a sub class is:

super().method_name(parameters)

Although in Python 3 is possible to use:

SuperClassName.method_name(self, parameters)

It is an old style of syntax that is not the prefer style.

You just need to pass 'self' as an argument to 'Thread.init'. After that, it works on my machines.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!