ndb.StringProperty 如何等于 python 字符串? [英] How is ndb.StringProperty equals a python string?

查看:23
本文介绍了ndb.StringProperty 如何等于 python 字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 ndb 模型类

I have this ndb Model class

class foo(ndb.Model):
  abc = ndb.StringProperty()

现在当我像这样使用 abc 时:

Now when I used abc like this:

if foo.abc == "a":
  print "I'm in!"

它进入 if 块并打印 I'm in!

It gets into the if block and prints I'm in!

这怎么可能?

我也尝试打印foo.abc,它返回StringProperty('abc')

推荐答案

您必须实例化类的实例才能正确使用属性.

You have to instantiate an instance of class to use properties properly.

class Foo(ndb.Model):
  abc = ndb.StringProperty()

foo = Foo()
foo.abc = 'some val'
print foo.abc  # prints 'some val'
print foo.abc == 'a' # prints False
print Foo.abc == 'a' # prints something not boolean - can't check now.

您收到 "I'm in!" 因为 ndb 属性正在覆盖 __equal__ 运算符并返回一个被视为 True<的非空对象/代码>.这用于进行像 query.filter(foo.abc == 'def')

You are are getting "I'm in!" because ndb properties are overwriting __equal__ operator and returning a non empty object that is treated as True. This is used to make queries like query.filter(foo.abc == 'def')

这篇关于ndb.StringProperty 如何等于 python 字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆