# Which usage of isinstance is correct?

**URL:** <https://forum.inductiveautomation.com/t/which-usage-of-isinstance-is-correct/17535>\
**Category:** Ignition\
**Created:** [March 28, 2018, 7:39pm UTC](https://forum.inductiveautomation.com/t/which-usage-of-isinstance-is-correct/17535 "2018-03-28T19:39:05Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmisztur2](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmisztur2/32/4243_2.png) [@cmisztur2](https://forum.inductiveautomation.com/u/cmisztur2)\
**Post date:** [March 28, 2018, 7:39pm UTC](https://forum.inductiveautomation.com/t/which-usage-of-isinstance-is-correct/17535/1 "2018-03-28T19:39:05Z")

</div>

```auto
x = {}

```

```auto
isinstance(x,type(dict))

```

or

```auto
isinstance(x,dict)

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 28, 2018, 7:45pm UTC](https://forum.inductiveautomation.com/t/which-usage-of-isinstance-is-correct/17535/2 "2018-03-28T19:45:39Z")

</div>

The latter:

```auto
x = {}
print type(x)
print isinstance(x, dict)
print isinstance(x, type(dict))

```

Yields:

```auto
<type 'dict'>
True
False

```

---

<div class="post-metadata">

**Author:** ![cmisztur2](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmisztur2/32/4243_2.png) [@cmisztur2](https://forum.inductiveautomation.com/u/cmisztur2)\
**Post date:** [March 29, 2018, 1:05am UTC](https://forum.inductiveautomation.com/t/which-usage-of-isinstance-is-correct/17535/3 "2018-03-29T01:05:30Z")

</div>

That’s what I thought. The latter is correct when used in components and script console.  
But from a project module, the first one works and the second does not.

In a project module:

```auto
isinstance(x,dict)

```

Yields:

```auto
TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 29, 2018, 1:24am UTC](https://forum.inductiveautomation.com/t/which-usage-of-isinstance-is-correct/17535/4 "2018-03-29T01:24:02Z")

</div>

Is there any chance you’ve accidentally used ‘dict’ as a variable name in that module? That would replace the built-in name.

---

<div class="post-metadata">

**Author:** ![cmisztur2](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmisztur2/32/4243_2.png) [@cmisztur2](https://forum.inductiveautomation.com/u/cmisztur2)\
**Post date:** [March 29, 2018, 1:38am UTC](https://forum.inductiveautomation.com/t/which-usage-of-isinstance-is-correct/17535/5 "2018-03-29T01:38:34Z")

</div>

It does not look like it. Should a call to locals() help identify this?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 29, 2018, 1:48am UTC](https://forum.inductiveautomation.com/t/which-usage-of-isinstance-is-correct/17535/6 "2018-03-29T01:48:10Z")

</div>

> [@cmisztur2](#):
>
> Should a call to locals() help identify this?

Yes, or just "print dict"

---

<div class="post-metadata">

**Author:** ![cmisztur2](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmisztur2/32/4243_2.png) [@cmisztur2](https://forum.inductiveautomation.com/u/cmisztur2)\
**Post date:** [March 29, 2018, 3:10pm UTC](https://forum.inductiveautomation.com/t/which-usage-of-isinstance-is-correct/17535/7 "2018-03-29T15:10:41Z")

</div>

Hmm… Ok, removing line #2 fixed the issue.  
print dict was returning a dictionary of all the modules in ‘project.states’.  
Below function is used as a decorator.

```auto
from project.states import utility as states
#from project.states import *

def managementLock(func):
	def fake(*args, **kwargs):
		arg1,arg2='n/a','n/a'
		if isinstance(args[0],dict): arg1,arg2=args[0]['Source'],args[0]['Target']
		else: arg1,arg2=args[0],args[1]
		project.activity.notify(project.activity.LOCK_IS_BOTTLENECK,[arg1,arg2])
		return [False, "%s,%s locked by management" % (arg1,arg2)]
		
	def inner(*args, **kwargs):
		print locals()
		print dict
		if isinstance(args[0],dict): arg1,arg2=args[0]['Source'],args[0]['Target']
		else: arg1,arg2=args[0],args[1]
		if isLocked(arg1,arg2):
			return fake(*args, **kwargs)
		else:
			return func(*args, **kwargs)

	return inner

```

![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/a/abada543d0a3a788d881b754424d5d801dfbd4b0.png)

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 29, 2018, 6:41pm UTC](https://forum.inductiveautomation.com/t/which-usage-of-isinstance-is-correct/17535/8 "2018-03-29T18:41:11Z")

</div>

That would suggest that somewhere at the top level of project.states, something is assigning to dict.

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [March 30, 2018, 6:17am UTC](https://forum.inductiveautomation.com/t/which-usage-of-isinstance-is-correct/17535/9 "2018-03-30T06:17:52Z")

</div>

In general, try to avoid `from x.y.z import *`. You can break your code in unexpected ways.

I just searched my code base, and didn’t need to use `import *` once.

---

<div class="post-metadata">

**Author:** ![cmisztur2](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmisztur2/32/4243_2.png) [@cmisztur2](https://forum.inductiveautomation.com/u/cmisztur2)\
**Post date:** [March 30, 2018, 11:36am UTC](https://forum.inductiveautomation.com/t/which-usage-of-isinstance-is-correct/17535/10 "2018-03-30T11:36:55Z")

</div>

Agreed, shame on me. Last year I ran into some other issue with import \*. I forgot what problems it caused, but yeah.

Found my other import \* issue…  
[http://forum.inductiveautomation.com/t/jython-import-behavior-in-custom-component-script/15688](http://forum.inductiveautomation.com/t/jython-import-behavior-in-custom-component-script/15688)

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 30, 2018, 11:39am UTC](https://forum.inductiveautomation.com/t/which-usage-of-isinstance-is-correct/17535/11 "2018-03-30T11:39:01Z")

</div>

> [@cmisztur2](#):
>
> some other issue with import \*

Another reason to avoid it is that it doesn't work with java imports.
