【发布时间】:2013-08-20 16:40:48
【问题描述】:
这是我的第一个 Python 程序,我正在使用一些用于登录或注册用户的 API。
这就是我的班级的样子:
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!'
我用下面的代码测试了这个类:
api_test = MyClass('apikeyadfasdf', 'gameiddfdfd', 'seckey34324324', 'JSON',
'admin', 'pass1')
print(api_test)
我得到这样的输出:
<package1.package2.file_name.MyClass object at 0x7f278388b48>
但是当我把我的测试改成这样时:
api_test = MyClass('apikeyadfasdf', 'gameiddfdfd', 'seckey34324324', 'JSON',
'admin', 'pass1')
data = api_test.userregistration(username, password, email)
print(data)
它会做这件事,我会得到我的输出,但它会执行该函数两次。
我在想问题可能出在哪里,我记得在 JAVA 中,当您忘记覆盖 toString() 时,您可能会得到类似的输出。现在,我已经阅读了有关 python 等价物的信息: repr 和 str 但首先我不知道如何从方法(不是 self.apikey 等)返回数据,其次我不能 100% 确定这是获得输出的正确方法,而不是地址。
【问题讨论】:
-
您有一些缩进问题,我怀疑这是由于粘贴不正确或其他原因造成的。如果您可以检查
def userregistration(self...等...并查看该行和后续行是否正确缩进,那将有所帮助。 -
已修复。现在它看起来就像在我的 IDE 中一样。