I have an object which inherits from ndb.Model (a Google App Engine thing). This object has a property called commentid:
class Comment(ndb.Model):
commentid = ndb.StringProperty()
Reading a bunch of articles, they all say this is the way to implement a property:
@property
def commentid(self):
if not self._commentid:
self._commentid = "1"
return self._commentid
but I get an error saying Comment object has no attribute _commentid. What am I doing wrong?
Edit: Ok obviously I’m a bit confused here. I come from Objective-C, where if you have a property called x then you automatically get a variable called _x in your getters and setters. So I thought this is what was happening here in Python too. But apparently I need to manually set a value for the variable with an underscore prefix.
All I want is to implement a getter where I do some checking of the value before returning it. How would I do this?
Implementing a property like that requires you to define the attribute for your object. What you’re doing there, is defining a class called Comment but you don’t define any attributes for it’s objects, you define them for the class itself.
Let me demonstrate with a small example:
In the above example, I define class
ExampleClassand give it a variablenamewith a valueExample Object. After that, I create an objecta = ExampleClass(), however it does not get the name attribute, cause the attribute is defined for the class itself, not for it’s objects.To fix this problem, you define the name inside
__init__-method, which gets called whenever an object of that class is created.There I define the
ExampleClassagain, but I also define__init__method for it. Init method takes only one parameter,self, which will be automatically given to the function. It’s the object which is being created. Then I setself.name = "Example Class", and since self is the object itself, we set the object’s attributename.Creating the property
To implement setter and getter for your attribute, you add the following:
Also, you should edit the
__init__method to takenameas a parameter too.