功能:Lambda不是字符串 - 这是什么意思?

时间:2014-04-26 07:35:05

标签: python google-app-engine lambda app-engine-ndb

我对此比较陌生,但我正在尝试在Google App Engine中为我的NDB实体创建一个计算属性。现在我有:

class Player(ndb.Model):
    name = ndb.StringProperty(required=True)
    wins = ndb.IntegerProperty(required=True)
    losses = ndb.IntegerProperty(required=True)
    record = ndb.ComputedProperty(lambda self: 1. * self.wins / (self.wins + self.losses))
    rank = ndb.IntegerProperty(lambda self: self.record * 1000 if self.wins + self.losses >= 10 else 500 + 1000 * (self.record-0.5) * (self.wins+self.losses) * (self.wins+self.losses) / 100)

当我尝试创建一个Player对象时,这两个lambda属性中的一个(我不确定哪个,因为它们都不是850行)导致错误。我收到消息(在控制台上):

File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/model.py", line 850, in __init__
    raise TypeError('Name %r is not a string' % (name,))
TypeError: Name <function <lambda> at 0x10c531a28> is not a string

我做错了什么?为什么lambda函数需要是一个字符串?而且,我怎样才能将它变成一个符合我想要它的字符串呢?我也尝试了一个更简单的版本,没有if / else语句,我得到了同样的错误。

让我知道你还需要知道什么,我很乐意帮忙。

1 个答案:

答案 0 :(得分:2)

IntegerProperty的第一个位置参数(您将其用于&#39; rank&#39;)被视为其数据存储名称,该名称必须是字符串。你的意思是另一个ComputedProperty吗?