如何在 Python 中将实例列表类型取消分类为普通列表类型
How to unclass an instance list type into normal list type in Python
我有以下 class:
class Point:
""" Given a list/vector create a class as single Point."""
def __init__(self, coords, reference=None):
self.coords = coords
self.n = len(coords)
self.reference = reference
def __repr__(self):
return str(self.coords)
用于将列表类型的数字转换为Point
class实例。
In [64]: t1 = [10.715430492165567, 66.9063251413503]
In [65]: type(t1)
Out[65]: list
In [66]: t1p = Point(t1) # Here we create the Point instance to t1
In [67]: type(t1p)
Out[67]: instance
我想做的是取消class t1p
以便它再次像t1
一样成为普通列表。如何实现?
在您的 Point
class:
中实施方法 __iter__
class Point:
[..]
def __iter__(self):
for coord in self.coords:
yield coord
然后您可以遍历您的 Point 对象:
for x in t1p:
print x
或者只是将其传递给列表构造函数
t1 = list(t1p)
我有以下 class:
class Point:
""" Given a list/vector create a class as single Point."""
def __init__(self, coords, reference=None):
self.coords = coords
self.n = len(coords)
self.reference = reference
def __repr__(self):
return str(self.coords)
用于将列表类型的数字转换为Point
class实例。
In [64]: t1 = [10.715430492165567, 66.9063251413503]
In [65]: type(t1)
Out[65]: list
In [66]: t1p = Point(t1) # Here we create the Point instance to t1
In [67]: type(t1p)
Out[67]: instance
我想做的是取消class t1p
以便它再次像t1
一样成为普通列表。如何实现?
在您的 Point
class:
__iter__
class Point:
[..]
def __iter__(self):
for coord in self.coords:
yield coord
然后您可以遍历您的 Point 对象:
for x in t1p:
print x
或者只是将其传递给列表构造函数
t1 = list(t1p)