gpt4 book ai didi

python - Python 中的类型 - Google Appengine

转载 作者:太空宇宙 更新时间:2023-11-03 23:49:50 25 4
gpt4 key购买 nike

现在有点生气了;

我有一个模型和一个类,它们只是在数据库中存储一个获取请求;基本跟踪。

class SearchRec(db.Model):
WebSite = db.StringProperty()#required=True
WebPage = db.StringProperty()
CountryNM = db.StringProperty()
PrefMailing = db.BooleanProperty()
DateStamp = db.DateTimeProperty(auto_now_add=True)
IP = db.StringProperty()

class AddSearch(webapp.RequestHandler):
def get(self):
searchRec = SearchRec()

searchRec.WebSite = self.request.get('WEBSITE')
searchRec.WebPage = self.request.get('WEBPAGE')
searchRec.CountryNM = self.request.get('COUNTRY')
searchRec.PrefMailing = bool(self.request.get('MAIL'))
searchRec.IP = self.request.get('IP')

bool 有我的 cookies ;我认为设置 bool(self.reque....) 会设置字符串的类型,但无论我传递什么,它仍然将其存储为 TRUE 在数据库中。我在模型的字符串上使用 required=True 时遇到了同样的问题;该死的东西一直说什么都没有通过……但是它通过了。

最佳答案

为了理解 bool() 内置函数的作用,您添加了很多复杂的层次。为什么不直接在命令行上对其进行测试,然后再将其深入嵌入到您的 Google 应用引擎代码中。

您会发现 bool() 函数使用 python 的真值:

http://docs.python.org/library/stdtypes.html#truth-value-testing

The following values are considered false:

* None
* False
* zero of any numeric type, for example, 0, 0L, 0.0, 0j.
* any empty sequence, for example, '', (), [].
* any empty mapping, for example, {}.
* instances of user-defined classes, if the class defines a
`__nonzero__()` or `__len__()` method, when that method returns the integer
zero or bool value False. [1]

All other values are considered true — so objects of many types are always true.

特别是 - 任何非空字符串都是 True

关于python - Python 中的类型 - Google Appengine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2600907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com