Test variable type

Hello,

Is it possible to check if a variable is :

  • dict or DotReferenceJythonMap
  • list or JsonifiableArrayList
    ?
    Thanks

A script can do it:

isinstance(your_object, (dict, DotReferenceJythonMap))

You'll need to import the exotic types you want to test against.

edit:
You might want to use parents of some classes, for example AbstractJythonMap instead of DotReferenceJythonMap. Your call.

Thanks
I've already tried isinstance() but I have a message : DotReferenceJythonMap is not defined.

How can I call DotReferenceJythonMap and AbstractJythonMap ?

As I said:

from com.inductiveautomation.ignition.common.script.abc import AbstractJythonMap

You'll need to search the javadocs to find exactly what you need to import.

1 Like

Better question: why do you feel you need to do this? Duck typing is a better bet than relying on internal class specifics.

1 Like

@PGriffith I don't understand "Duck typing". Do you have an example ?

I try to test PyLong variable too.

Do you mean that below ?

try:
	project_id = value*1
except:
	project_id = None
	
res = device.getList({'project_id':project_id})

if it quacks like a duck, walks like a duck, etc... then you can pass it to any function that accepts a duck.
Or something.

You should try to answer this:

:yawning_face:
SO ...
In my scripts or bindings, I test my variables to deal with unexpected errors. But in Ignition you have a lot of specific variables like JythonMap, PyLong, ...

That's where duck typing comes in.

Let's say your function takes an object and tries to call its .foo() method.
Instead of checking that the object is the type you expected, you can check if it has a method called foo
Basically, you're not asking "are you a duck ?", you're asking "can you quack ?"

4 Likes