简体   繁体   中英

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.

This is related to System.Collections.Generic lists in 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:

  • derive your Python class from a .NET type
class MyClass(System.Object):
   # without __namespace__ - regular Python type
  __namespace__ = "MyNamespace"
  • use List<System.Object> or List<Python.Runtime.PyObject>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM