Python @property and @setterName.setter

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’

</Console error output>

I don’t think the property getter/setter/deleter decorators were available in Python 2.5.

Those decorators are available in Jython 2.6: docs.python.org/2/whatsnew/2.6. … ge-changes.

You can see the plan to upgrade Jython 2.5 to Jython 2.7 in Ignition here: ideas.inductiveautomation.com/fo … jython-2-7

Instead of using the property decorator you can use the builtin property function. See here: docs.python.org/2/library/funct … l#property

Best,

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:

 set

and the values printed should be 2 and 6. No?

I’m using 7.9.3 on JRE 1.8.0_141

Never mind… I figured out why. My class definition was not inheriting the object class.

class State(object):

Now it’s working fine.[quote=“thosey, post:1, topic:12278”]
I am writing a python class and wish to use getters and setters.
[/quote]

If you define your getters and setters this way you should be fine.

1 Like