Under python 2.7.
How to deal with methods those have passing only one argument, the "self"?
class MyTwitterClass(object):
...
def keys(self):
return {
'consumer_key': self.read_settings().get('Keys', 'consumer_key'),
"consumer_secret": self.read_settings().get('Keys', 'consumer_secret'),
"access_token_key": self.read_settings().get('Keys', 'access_token_key'),
"access_token_secret": self.read_settings().get('Keys', 'access_token_secret')
}
api = twitter.Api(consumer_key=keys()['consumer_key'],
consumer_secret=keys()['consumer_secret'],
access_token_key=keys()['access_token_key'],
access_token_secret=keys()['access_token_secret'])
The error I got is:
Traceback (most recent call):
File "..." ...
...
api = twitter.Api(consumer_key=keys()['consumer_key'],
TypeError: keys() takes exactly 1 argument (0 given)
Any idea will be welcome.
keys()
is a method of your class. To pass automatically the self
parameter, you have to call it this way :
c = MyTwitterClass()
keys = c.keys()
So, I suggest you add a get_api()
method to your class:
class MyTwitterClass(object):
...
def keys(self):
return {
'consumer_key': self.read_settings().get('Keys', 'consumer_key'),
"consumer_secret": self.read_settings().get('Keys', 'consumer_secret'),
"access_token_key": self.read_settings().get('Keys', 'access_token_key'),
"access_token_secret": self.read_settings().get('Keys', 'access_token_secret')
}
def get_api(self):
keys = self.keys() # self refer to the current instance of your class
api = twitter.Api(consumer_key=keys['consumer_key'],
consumer_secret=keys['consumer_secret'],
access_token_key=keys['access_token_key'],
access_token_secret=keys['access_token_secret'])
return api
# Now, you can do :
c = MyTwitterClass()
api = c.get_api()
You need to call the instance method. keys()
is the a method, which you can call on every MyTwitterClass
object, or with self
if your are in the class itself
obj = MyTwitterClass()
[...]
api = twitter.Api(consumer_key=obj.keys()['consumer_key'],
consumer_secret=obj.keys()['consumer_secret'],
access_token_key=obj.keys()['access_token_key'],
access_token_secret=obj.keys()['access_token_secret'])
^^^^
note this addition
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.