takes exactly 2 arguments (1 given) when including self [closed]

限于喜欢 提交于 2019-12-13 08:07:20

问题


Everytime I call my function def hello(self,value) I get an error : takes exactly 2 arguments (1 given) so what could I do ?

Or is there another possibility to do this: self.statusitem.setImage_(self.iconsuccess)?

EDIT:

simple representation of my code

Class A:
   func_in_class_B(value)

Class B:
def finishLaunching(self):
   self.statusitem.setImage_(self.icon)
def func_in_class_B(self,value)
   self.statusitem.setImage_(self.iconsuccess)

Class A is a background thread and Class B my main thread, and I want to manipulate `self.statusitem.setImage_(self.icon)


回答1:


It sounds like you aren't calling your hello function correctly. Given the following class definition:

class Widget(object):
    def hello(self, value):
        print("hello: " + str(value))

You are probably calling it like a static function like this:

Widget.hello(10)

Which means no instance of a widget class gets passed as a first param. You need to either set up the hello function to be static:

class Widget(object):
    @staticmethod
    def hello(value):
        print("hello: " + str(value))

Widget.hello(10)

or call it on a specific object like this:

widget = Widget()
widget.hello(10)



回答2:


This is most probably because your hello function is not a class member. In that case you need not provide self in the method declaration....i.e. instead of hello(self,value) just say hello(value)

For example...this snippet works absolutely fine

def hello(value):
    print 'Say Hello to ' + value

hello('him')

If this is not the case then please provide your code snippet to help you further.



来源:https://stackoverflow.com/questions/14693545/takes-exactly-2-arguments-1-given-when-including-self

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