Sunday, 18 August 2013

Why object of python class returns address instead of return data?

Why object of python class returns address instead of return data?

This is my first program in Python and I'm working with some API which is
used to log in or register user.
This is how my class looks like:
class MyClass:
...
def __init__(self, apikey, gameid, securitykey, responseformat,
username=None, password=None, email=None)
self.apikey = apikey
self.gameid = gameid
self.securitykey = securitykey
self.responseformat = responseformat
if username and password:
self.userlogin(username,password)
if username and password and email:
self.userregistration(username, password, email)
''' And there are function userlogin and userregistration
i'll show just one of them '''
def userregistration(self, username, password, email):
securitypayload = self.buildurl(username, password, email)
user = requests.get('url', params = securitypayload)
if 'error' in user.text:
return user.text
else:
return 'Success!'
I tested this class with code below:
api_test = MyClass('apikeyadfasdf', 'gameiddfdfd', 'seckey34324324', 'JSON',
'admin', 'pass1')
print(api_test)
and I got output like this:
<package1.package2.file_name.MyClass object at 0x7f278388b48>
But when I change my test to this:
api_test = MyClass('apikeyadfasdf', 'gameiddfdfd', 'seckey34324324', 'JSON',
'admin', 'pass1')
data = api_test.userregistration(username, password, email)
print(data)
it'll do the thing and I'll get my output, but it'll execute that function
two times.
I was thinking where the problem could be and I remembered that in JAVA
you could get similar output when you forget about override toString().
Now, I've read about python equivalents: repr and str but firstly I don't
know how to return data from methods (not self.apikey etc.) and secondly
I'm not 100% sure that is the right way to get my output, not address.

No comments:

Post a Comment