Why object of python class returns address instead of return data? -
this first program in python , i'm working api used log in or register user.
this how 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 , password: self.userlogin(username,password) if username , password , email: self.userregistration(username, password, email) ''' , there function userlogin , userregistration i'll show 1 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 class code below:
api_test = myclass('apikeyadfasdf', 'gameiddfdfd', 'seckey34324324', 'json', 'admin', 'pass1') print(api_test)
and got output this:
<package1.package2.file_name.myclass object @ 0x7f278388b48>
but when change test this:
api_test = myclass('apikeyadfasdf', 'gameiddfdfd', 'seckey34324324', 'json', 'admin', 'pass1') data = api_test.userregistration(username, password, email) print(data)
it'll thing , i'll output, it'll execute function 2 times.
i thinking problem , remembered in java similar output when forget override tostring(). now, i've read python equivalents: repr , str firstly don't know how return data methods (not self.apikey etc.) , secondly i'm not 100% sure right way output, not address.
if problem userregistration
executed 2 times -- execute 2 times. once in __init__
, once explicitly.
regarding returning __init__
check "how return value __init__ in python?" :
__init__ returns newly created object. cannot (or @ least shouldn't) return else.
and besides, don't use return value indicate error - use exceptions: