# Python Classes

**URL:** <https://forum.inductiveautomation.com/t/python-classes/1221>\
**Category:** Archived Topics\
**Created:** [July 15, 2009, 11:02pm UTC](https://forum.inductiveautomation.com/t/python-classes/1221 "2009-07-15T23:02:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan](https://avatars.discourse-cdn.com/v4/letter/j/aeb1de/32.png) [@Jonathan](https://forum.inductiveautomation.com/u/Jonathan)\
**Post date:** [July 15, 2009, 11:02pm UTC](https://forum.inductiveautomation.com/t/python-classes/1221/1 "2009-07-15T23:02:40Z")

</div>

I am curious as to why this does not work.

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

```auto
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

```auto
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

> [@](#):
>
> Traceback (innermost last):  
> File "event:actionPerformed", line 1, in ?  
> File "module:object", line 6, in **init**  
> NameError: MyClass

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"

Thanks,

---

<div class="post-metadata">

**Author:** ![MickeyBob](https://avatars.discourse-cdn.com/v4/letter/m/e19adc/32.png) [@MickeyBob](https://forum.inductiveautomation.com/u/MickeyBob)\
**Post date:** [July 23, 2009, 7:31pm UTC](https://forum.inductiveautomation.com/t/python-classes/1221/2 "2009-07-23T19:31:08Z")

</div>

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]
```

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [July 23, 2009, 7:43pm UTC](https://forum.inductiveautomation.com/t/python-classes/1221/3 "2009-07-23T19:43:48Z")

</div>

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.
