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

  • Home
  • SEARCH
  • 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 8692703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:22:12+00:00 2026-06-13T00:22:12+00:00

I am new to using properties, so I put together a simple test as

  • 0

I am new to using properties, so I put together a simple test as shown below. In my tests, I made two classes “Test1” and “Test2” where each is meant to hold one value. I am attempting to use a property to govern access to the pseudo-hidden “val” attribute. This current test does not restrict any inputs or outputs of the “val” attribute as this program was only meant as a proof of concept. The two test classes shown below yield the same results and are supposed to represent the different methods to construct a property. The example uses for properties I am referring to are found on the python docs here.

As per the documentation:

If then c is an instance of C, c.x will invoke the getter, c.x = value will invoke the setter and del c.x the deleter.

where C is their test class. I thought that by setting the value the way I did would change _val and leave val as a property. However it seems to me that my method of accessing the properties setter is actually replacing the property with the integer 5 unless I am mistaken. I hope someone can clarify my confusion.

class Test1:
    def __init__(self):
        self._val = 0

    def setVal(self,newVal):
        self._val = newVal
    val = property(lambda self: self._val, setVal, None, "Property for value")

    def __str__(self):
        return "Value: {}".format(self.val)

class Test2:
    def __init__(self):
        self._val = 0

    @property
    def val(self):
        return self._val

    @val.setter
    def setVal(self,newVal):
        self._val = newVal

    def __str__(self):
        return "Value: {}".format(self.val)

def verify(a):
    print("\nCheck with {}".format(a.__class__.__name__))
    print("Value check:",a.val)
    a.val = 5
    print("Value after a.val = 5 is:",a.val)
    print("The actual value is:",a._val)

def main():
    verify(Test1())
    verify(Test2())

if __name__ == '__main__':
    main()
  • 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-13T00:22:13+00:00Added an answer on June 13, 2026 at 12:22 am

    From documentation:

    property([fget[, fset[, fdel[, doc]]]])

    Return a property attribute for new-style classes (classes that derive from object).

    Descriptors are only invoked for new style objects or classes. Your using old-style classes. Inherits from a base type object:

    class Test1(object):
        # your code
    
    class Test2(object):
        def __init__(self):
            self._val = 0
        
        @property
        def val(self):
            return self._val
        
        @val.setter
        def val(self,newVal): # should be named as property
            self._val = newVal
        
        def __str__(self):
            return "Value: {}".format(self.val)
    

    This work fine:

    >>> verify(Test1())
    
    Check with Test1
    ('Value check:', 0)
    ('Value after a.val = 5 is:', 5)
    ('The actual value is:', 5)
    

    Read more about difference between new-style classes and classic classes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using new style classes in Python 2.6 and am having trouble with __getattr__
I am using JSF and EJB as two separate project in my application. Below
try { Properties props = new Properties(); props.put("mail.smtp.starttls.enable", "true"); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.auth",
I have a List of classes in my collection like List<MyClass> test = new
I'm new using the excellent stuff of css twitter bootstrap and having a problem,
I'm new using Yii framework. I show a list a checkbox. But it's not
I still new using Asp.net with vb.net Protected Sub login_Click(ByVal sender As Object, ByVal
New at using admob within Android. Is it possible to show ads with certain
when using new Date,I get something like follows: Fri May 29 2009 22:39:02 GMT+0800
Very new to using Raphael.js I'm looking at the tutorials and I am able

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.