检查Plone PythonScript中的类型

时间:2013-11-08 18:24:36

标签: python plone zope

我正在尝试在Plone Products.PythonScript中检查变量的类型。 我试过这段代码:

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

不幸的是,'list'和'type'在PythonScript中受到限制。我收到了这个错误:

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

有没有可能检查我的变量的类型?

3 个答案:

答案 0 :(得分:3)

Python脚本可以使用特殊函数same_type()来解决在类型上设置的限制:

if same_type(var, []):

我们使用文字空列表表示法,而不是list类型本身(因为它已被重新分配)。

答案 1 :(得分:0)

我不认为您的问题与描述的一致。

该错误似乎表明您已将变量list分配给其他内容(您不应该这样做)。

例如:

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

编辑:根据Plone website Zope 2“Python Scripts”是过时的技术,您不应再使用Python脚本通过Zope管理界面编写任何Plone代码。而是创建一个加载项并在其中创建Zope 3浏览器视图。

答案 2 :(得分:-1)

基于http://developer.plone.org/functionality/expressions.html#python-expression,这个怎么样?你可以尝试像

expression = Expression("if isinstance(var, list): myflag=True")
expression_context = getExprContext(self.context)
value = expression(expression_context)