我刚开始学蟒蛇。由于某些性能问题,我不得不在我的AWS lambda函数中把所有代码从Java转换到Python。我正在使用lambda控制台提供的ide。
目前,我有点困在python中的类上。我读了很多教程,我认为我在setters和getter方法上所做的是正确的。这是我的代码:
class User:
def __init__(self, user):
self.username = user
def get_username(self):
return self.username
def set_username(self, user):
self.username = user
def lambda_handler(request, context):
user = User("thisisaname")
print("name of user is " + user.get_username)
user.set_username("whatever")
print("name of user is " + user.get_username)
return "executed"
当lambda_处理程序被调用时,我希望输出是
name of user is thisisname
name of user is whatever
但是,当我打印user.get_username的值时。类型错误被抛出。以下是我得到的错误消息:
{ "errorMessage": "can only concatenate str (not \"method\") to str", "errorType": "TypeError", "stackTrace": [
" File \"/var/task/lambda_function.py\", line 28, in lambda_handler\n print(\"name of user is \" + user.get_username)\n" ] }
当我用str(user.get_username)包装它时,将打印方法描述。
name of user is <bound method User.get_username of <lambda_function.User object at 0x7f4d6a4cc4d0>>
name of user is <bound method User.get_username of <lambda_function.User object at 0x7f4d6a4cc4d0>>