Python Classes

I am curious as to why this does not work.

I have created a class called MyClass in the global script package app.object

class MyClass:

	instancesCreated = 0

	def __init__(self, initialValue=0):
		MyClass.instancesCreated += 1
		self.value = initialValue

and then created a button that uses the class in the event script

x = app.object.MyClass()
print x.instancesCreated
y = app.object.MyClass()
print x.instancesCreated
print app.object.MyClass.instancesCreated

this is the error I receive when I press the button

I am guessing I need to prefix the MyClass with something, but I am not sure what. This is an example staight out of “Jython Essentials”


Try playing with this variation:

[code]class MyClass:
instancesCreated = 0

def __init__(self, initialValue=0):
	import fpmi.gui
	import app.object
	fpmi.gui.messageBox("MyClass __init__()")
	app.object.MyClass.instancesCreated += 1
	fpmi.gui.messageBox("Instances Created: %d" % app.object.MyClass.instancesCreated)[/code]

Whoops, sorry I let this one go stale.

Yeah, this is just a quirk of Python scoping rules. Your logic is sound, but your init function doesn’t actually have “MyClass” in its scope. import it like suggested above.