GPing should be called as first argument

2
if __name__ == '__main__':
top_100_domains = ['google.com','facebook.com','youtube.com','yahoo.com','baidu.com','wikipedia.org','live.com','qq.com','twitter.com','amazon.com','linkedin.com','blogspot.com','google.co.in','taobao.com','sina.com.cn','yahoo.co.jp','msn.com','google.com.hk','wordpress.com','google.de','google.co.jp','google.co.uk','ebay.com','yandex.ru','163.com','google.fr','weibo.com','googleusercontent.com','bing.com','microsoft.com','google.com.br','babylon.com','soso.com','apple.com','mail.ru','t.co','tumblr.com','vk.com','google.ru','sohu.com','google.es','pinterest.com','google.it','craigslist.org','bbc.co.uk','livejasmin.com','tudou.com','paypal.com','blogger.com','xhamster.com','ask.com','youku.com','fc2.com','google.com.mx','xvideos.com','google.ca','imdb.com','flickr.com','go.com','tmall.com','avg.com','ifeng.com','hao123.com','zedo.com','conduit.com','google.co.id','pornhub.com','adobe.com','blogspot.in','odnoklassniki.ru','google.com.tr','cnn.com','aol.com','360buy.com','google.com.au','rakuten.co.jp','about.com','mediafire.com','alibaba.com','ebay.de','espn.go.com','wordpress.org','chinaz.com','google.pl','stackoverflow.com','netflix.com','ebay.co.uk','uol.com.br','amazon.de','ameblo.jp','adf.ly','godaddy.com','huffingtonpost.com','amazon.co.jp','cnet.com','globo.com','youporn.com','4shared.com','thepiratebay.se','renren.com']
gp = GPing
for domain in top_100_domains:
    gp.send(domain,test_callback)
gp.join()

ERROR:

  

gp.send (domain, test_callback)   TypeError: unbound method send () must be called with GPing instance first argument (got str instance instead)

    
asked by anonymous 31.03.2017 / 21:27

1 answer

3

The error happens because you are calling the method statically, without instantiating the class. When doing:

gp = GPing

You are not creating an instance of the class, but passing the class reference to another object. To correct, just insert the parentheses:

gp = GPing()

You can check this by displaying the object gp .

>>> gp = GPing
>>> print(gp)
<class 'GPing'>

But when doing:

>>> gp = GPing()
>>> print(gp)
<GPing object at 0x...>

Notice that in the first way, gp refers to the GPing class while in the second it refers to object of the same class.

The error itself happens because the send method should be set to:

def send(self, domain, callback):
    ...

Where self is the instance itself of class GPing . Calling the method without creating the instance, the first argument of the method is not set automatically and therefore a type error is generated.

Related Questions

Why do we have to use the self attribute as an argument in the methods?

In Python, is there any rule or advantage over the use of 'Self'?

How call an external function, without sending the 'self'?

    
31.03.2017 / 21:44