簡體   English   中英

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

[英]Function: Lambda is not a string — what does this mean?

我對此比較陌生,但我正在嘗試為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語句,我得到了同樣的錯誤。

讓我知道你還需要知道什么,我很樂意幫忙。

IntegerProperty(您用於'rank')的第一個位置參數被視為其數據存儲名稱,該名稱必須是字符串。 你的意思是另一個ComputedProperty?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM