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

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

问题描述

我有这个ndb模型类

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

现在当我使用 abc 这:

  if foo.abc ==a:
print我在!

它进入if块并打印我进来了!



这可能怎么样?



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

解决方案<

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

foo = Foo()
foo.abc ='some val'
print foo.abc#prints'some val '
print foo.abc =='a'#打印False
print Foo.abc =='a'#打印一些不是布尔值的 - 现在不能检查。

您正在获取我在!,因为ndb属性覆盖 __等于__ 运算符并返回一个被视为 True 的非空对象。这用于使查询像 query.filter(foo.abc =='def')


I have this ndb Model class

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

Now when I used abc like this:

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

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

How's this possible?

I also tried printing foo.abc, it returned 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.

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天全站免登陆