简体   繁体   English

System.Collections.Python 中的通用列表“类型错误:预期类型”错误

[英]System.Collections.Generic lists in Python 'TypeError: type(s) expeected' error

I am trying to run the following piece of code:我正在尝试运行以下代码:

import clr
import sys
#import System.Collections
clr.AddReference("System.Collections")
from System.Collections.Generic import List
from System import String

class MyClass(object):
    def __init__(self, e=""):
        self.title = e


li = List[MyClass]()

list(li)

However, I am getting the error 'TypeError: type(s) expected' and cannot understand how to solve it.但是,我收到错误“TypeError: type(s) expected”并且无法理解如何解决它。

This is related to System.Collections.Generic lists in Python这与 System.Collections.Python 中的通用列表有关

Thanks!谢谢!

In Python.NET you can't use .NET lists to store Python objects in a strongly-typed way unless you do one of the following:在 Python.NET 中,您不能使用 .NET 列表以强类型方式存储 Python 对象,除非您执行以下操作之一:

  • derive your Python class from a .NET type从 .NET 类型派生您的 Python class
class MyClass(System.Object):
   # without __namespace__ - regular Python type
  __namespace__ = "MyNamespace"
  • use List<System.Object> or List<Python.Runtime.PyObject>使用List<System.Object>List<Python.Runtime.PyObject>

  • have a List<X> with a Python to .NET codec for the .NET type X 对于List<X> type X

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Python中使用System.Collections.Generic.List`1 [System.Byte] - Using System.Collections.Generic.List`1[System.Byte] in Python Python 错误类型错误:+= 不支持的操作数类型:'type' 和 'int' - Python error TypeError: unsupported operand type(s) for +=: 'type' and 'int' Python写错误-TypeError:%不支持的操作数类型:&#39;tuple&#39;和&#39;tuple&#39; - Python write error - TypeError: unsupported operand type(s) for %: 'tuple' and 'tuple' 类型错误:不支持 / 的操作数类型:python 中的“str”和“int”错误 - TypeError: unsupported operand type(s) for /: 'str' and 'int' error in python Python错误TypeError:+不支持的操作数类型:&#39;function&#39;和&#39;function&#39; - Python Error TypeError: unsupported operand type(s) for +: 'function' and 'function' Python Panda错误TypeError:/不支持的操作数类型:“ str”和“ int” - Python Panda Error TypeError: unsupported operand type(s) for /: 'str' and 'int' 如何在python的类型提示系统中使用通用(高级)类型变量? - How to use Generic (higher-kinded) type variables in python's type hinting system? Python 错误:TypeError:不支持的操作数类型 -:'int' 和 'function' - Python Error: TypeError: unsupported operand type(s) for -: 'int' and 'function' Python 错误:类型错误:+ 不支持的操作数类型:&#39;float&#39; 和 &#39;str&#39; - Python error : TypeError: unsupported operand type(s) for +: 'float' and 'str' 代码错误Python:TypeError:/:&#39;set&#39;和&#39;int&#39;不支持的操作数类型 - Code Error Python : TypeError: unsupported operand type(s) for /: 'set' and 'int'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM