简体   繁体   中英

Data attribute overrides base class method: Python

I have a class named SearchQuery and I named a data attribute as query (type: String); but the base class has a class method query(). So my SearchQuery class is as follow:

class SearchQuery(ndb.Model):
    query = ndb.StringProperty()

Now when I write:

SearchQuery.query()

I get:

TypeError: 'StringProperty' object is not callable

How do I call the query method? I will change my data member name but before changing I have to copy the data. For copying, query() has to be called.

You can use super

If you have

class Parent(object):
 def query(self):
  print "Hello"

and

class Child(Parent):
 query = "Some string"

and you create a Child object defined like this

c = Child()

Then you can have access to the query function in Parent by using super :

super(Child, c).query()

You don't need to call it at all; SearchQuery.query() is equal to ndb.Query(kind="SearchQuery")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM