1

Consider this following code:

class Class:
    def __init__(self):
        self._value = False
    def GetValue(self):
        return self._value
    def SetValue(self, val):
        self._value = val
    Value = property(GetValue, SetValue)

c = Class()
print c._value, c.Value
False False
c.Value = True
print c._value, c.Value
False True

Why isn't the property calling my function ? (I am running Python 2.6.5 on windows)

2 Answers 2

8

You need to use a new style class (inherit from object) for property to work

class Class(object):
    ...
Sign up to request clarification or add additional context in comments.

1 Comment

Descriptors in general, of which property is one.
1

If you only want to access your value as a property, it's much simpler to just use an attribute:

class Class(object):
    def __init__(self):
        self.value = False

c = Class()
c.value                # False
c.value = True
c.value                # True

This isn't Java, you can simply access attributes the easy way. If in the future you find that you need to perform more logic as part of the property access, then you can change the class to use a property.

BTW: Python style is to not use Uppercased names for functions and properties, only lowercased names.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.