Question

I'm trying to check a variable for its type in a Plone Products.PythonScript. I tried this code:

if isinstance(var, list):
    do(sth)

Unfortunately, 'list' and 'type' are restricted in a PythonScript. I got this error:

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

Is there any possibility to check my variable for its type?

Was it helpful?

Solution

Python Script can use a special function same_type() to work around the restrictions set on types:

if same_type(var, []):

where we use the literal empty list notation, not the list type itself (since that has been reassigned).

OTHER TIPS

I don't think your issue is as described.

That error seems to indicate that you've assigned the variable list to something else (which you shouldn't do).

eg:

>>> l = range(4)
>>> list = 'something'
>>> isinstance(l,list)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types
>>>

EDIT: According to the Plone website, Zope 2 "Python Scripts" are outdated technology and you should no longer write any Plone code through Zope Management Interface using Python scripts. Instead, create an add-on and create Zope 3 browser views in it.

How about this, based on http://developer.plone.org/functionality/expressions.html#python-expression ? You could try sth like

expression = Expression("if isinstance(var, list): myflag=True")
expression_context = getExprContext(self.context)
value = expression(expression_context) 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top