How is ndb.StringProperty equal to a python string?

I have a ndb model class

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

Now that I have used abc as follows:

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

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

How is this possible?

I also tried to print foo.abc , it returned StringProperty('abc')

+3
python google-app-engine app-engine-ndb
source share
1 answer

You need to instantiate the class to use the properties correctly.

 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 get "I'm in!" because the ndb properties overwrite the __equal__ and return a non-empty object that is treated as True . This is used to create queries of type query.filter(foo.abc == 'def')

+2
source share

All Articles