String Representation of an Object

Hit a hiccup with trying to print out an object. Not sure what is going on. The debug below is from the script console.

from shared.scheduler.model import ScheduleEntry

new = ScheduleEntry(1,2,3,4,5,6,7,8)
print str(new)

class test:
	def __init__(self):
		self.id = 1
	def __str__(self):
		return "string"
		
t = test()

print t

This is the class for ScheduleEntry:

class ScheduleEntry:
	def __init__(self, id, priority, loadDate, startDate, endDate, recipe, minDosingTime, status):
		self.id = id
		self.priority = priority
		self.loadDate = loadDate
		self.startDate = startDate
		self.endDate = endDate
		self.recipe = recipe
		self.minDosingTime = minDosingTime
		self.status = status
		self.segmentsByDay = {}
		self.newEntry = False	
		
	def __str__(self):
		return "string"

When I print out “new” I just get the representation of the object and not the string as expected but when I print out “t”, I get the string as expected

Console Output

>>> 
<model.ScheduleEntry instance at 0xd>
string
>>> 

You don't say what version of Ignition, but I'm pretty sure your class should be defined with parent object. First line like so:

class ScheduleEntry(object):

See "New Style Classes" in the python documentation:

https://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes

Also, consider not using import. There are corner cases where old code can persist. Use the full path to ScheduleEntry when you construct your instance.

We are using Ignition 7.9.2 which uses Jython 2.5.3. Adding the the parent object didn’t make a difference but by not using the import and using the full path to construct the instance, that fixed the issue.

1 Like