Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8878003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:36:27+00:00 2026-06-14T19:36:27+00:00

I have an object which inherits from ndb.Model (a Google App Engine thing). This

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:36:28+00:00Added an answer on June 14, 2026 at 7:36 pm

    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:

    class ExampleClass:
        name = "Example Object"
    
    a = ExampleClass() # Init new instance of ExampleClass
    print(a.name) # a doesn't own an attribute called "name"
    print(ExampleClass.name) # --> "Example Object"
    

    In the above example, I define class ExampleClass and give it a variable name with a value Example Object. After that, I create an object a = 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.

    class ExampleClass:
        def __init__(self):
            self.name = "Example Class"
    
    a = ExampleClass() # Init new instance of ExampleClass
    print(a.name) # --> "Example Class"
    print(ExampleClass.name) # --> ERROR: Exampleclass.name doesn't exist
    

    There I define the ExampleClass again, 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 set self.name = "Example Class", and since self is the object itself, we set the object’s attribute name.

    Creating the property

    To implement setter and getter for your attribute, you add the following:

    class ExampleClass:
        def __init__(self):
            self.name = "Example Class"
        
        @property
        def name(self):
            if not self._name:
                pass #blabla code here
            return self._name
    
        @name.setter
        def name(self, value):
            #blabla more code
            self._name = value
    

    Also, you should edit the __init__ method to take name as a parameter too.

    def __init__(self, name="Example Object"):
        self.name = name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my program, I have a class called Object, which inherits from UIView. I
I have an instance of a class called AccessData, which inherits from DbContext. So
I have a custom list which inherits from Generic.List<T> like this: public class TransferFileList<T>
I have a Django project whereby every model inherits from a common Object model
I have an NSOperationQueue to which i have added my custom class's objects(inherits from
I have this object which is an instance of a superclass. I want to
I have an object which contains models for my ASP.NET MVC web app. The
I have an object which has several properties that are set when the object
Suppose I have abstract class A and abstract class B which inherits from A,
I have an ASP.NET GridView which has columns that look like this: | Foo

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.