I am writing a python class and wish to use getters and setters. I am unsure why this is failing with the object has no attribute ‘setter’. Was this not implemented in Jython?
Ignition platform is trial V 7.8.3
Class code
Test Class
class Test(object):
‘Common base class for all test objects’
# Hidden variable
__iCount = 0
# Instantiate the class.
def init(self):
self.__iCount += 1
def del(self):
print class_name, “destroyed”
# Class Methods.
def count(self):
return self.__iCount
@property
def myProp(self):
return self.__x
@myProp.setter
def myProp(self, value):
self.__x = value
calling code
test = project.hosey.classes.Test()
test.myProp(12)
print test.myProp()
ERROR [ActionAdapter-AWT-EventQueue-2] Error executing script for event: actionPerformed on component: Button 1.
Traceback (most recent call last):
File “event:actionPerformed”, line 1, in
ImportError: Error loading module classes: ‘property’ object has no attribute ‘setter’
Thank you. I have decided to use this as a work around.
So far it appears to do exactly what I am expecting it to do.
# Test Class
class Test(object):
'Common base class for all test objects'
# Hidden variable
__iCount = 0
# Instantiate the class.
def __init__(self):
self.__iCount += 1
def __del__(self):
print class_name, "destroyed"
# Class Methods.
def count(self):
return self.__iCount
def myProp(self, value = None):
if value is not None:
self.__x = value
print "setter called"
print "Method called"
return self.__x
I’m trying to use the property function as follows:
class State:
def __init__(self, value):
print 'init'
self._x = value
def getValue(self):
print 'get'
return self._x
def setValue(self, value):
print 'set'
self._x = value + 1
x = property(getValue, setValue)
state = State(1)
print state.x
state.x = 5
print state.x
when I run the code I get the following output:
init
get
1
5
when I assign a value to state.x shouldn’t the setter function setValue be called? because it clearly isn’t. Otherwise the output should have some lines with: