Python子类tuple对象,能够在内部重新实例化自身
我理解可变v的概念。Python中的不可变对象,没有问题。虽然不能直接修改任何不可变对象的内在值,但可以使用不同的值重新实例化不可变对象的任何实例。我想做的是在tuple的子类上构建一个内部函数,它可以以受控的方式重新分配自己的值。这可能是我似乎找不到的基本功能,如果有任何帮助,我将不胜感激 例如,这是我希望能够做到的,但这显然不起作用Python子类tuple对象,能够在内部重新实例化自身,python,tuples,mutable,Python,Tuples,Mutable,我理解可变v的概念。Python中的不可变对象,没有问题。虽然不能直接修改任何不可变对象的内在值,但可以使用不同的值重新实例化不可变对象的任何实例。我想做的是在tuple的子类上构建一个内部函数,它可以以受控的方式重新分配自己的值。这可能是我似乎找不到的基本功能,如果有任何帮助,我将不胜感激 例如,这是我希望能够做到的,但这显然不起作用 class myTuple(tuple): def __new__(self): initialValue = [1, 2, 3]
class myTuple(tuple):
def __new__(self):
initialValue = [1, 2, 3]
return super(myTuple, self).__new__(self, initialValue)
def resetMyself(self):
newValue = [4, 5, 6]
self = tuple(newValue)
结果如下
>>> foo = myTuple()
>>> print foo
(1, 2, 3)
>>> foo.resetMyself()
>>> print foo
(4, 5, 6)
通过在本网站上阅读大量对此类问题的回复,我知道你们中的一些人可能倾向于回答“为什么要这样做?”但让我们用更直接的答案来节省回复空间,包括可能的“你不能这样做,没有办法,没有方法”,如果真是这样的话
非常感谢大家
编辑,谢谢下面的答案,这是我最后得到的
class semiImmutableList(list):
def __setitem__(self, *args):
raise TypeError("'semiImmutableList' object doesn't support item assignment")
__setslice__ = __setitem__
def __delitem__(self, *args):
raise TypeError("'semiImmutableList' object doesn't support item deletion")
__delslice__ = __delitem__
def append(self, *args):
raise AttributeError("'semiImmutableList' object has no attribute 'append'")
def extend(self, *args):
raise AttributeError("'semiImmutableList' object has no attribute 'extend'")
def insert(self, *args):
raise AttributeError("'semiImmutableList' object has no attribute 'insert'")
def remove(self, *args):
raise AttributeError("'semiImmutableList' object has no attribute 'remove'")
def pop(self, *args):
raise AttributeError("'semiImmutableList' object has no attribute 'pop'")
def __init__(self):
x = [1, 2, 3]
super(semiImmutableList, self).__init__(x)
def resetMyself(self):
super(semiImmutableList,self).append(5)
您可以看到上述内容的任何改进/调整,请发布。似乎可以合并AttributeError的重复?如果需要可变元组,请使用列表 编辑: 试试这个
class FrankenList(object):
def __init__(self, init=None):
self.__data = init or []
def __getitem__(self, key):
return self.__data[key]
def __repr__(self):
return repr(self.__data)
def __str__(self):
return str(self.__data)
很简单,你所要做的就是整理一个列表
class ImmutableList(object):
def __init__(self, *args):
self.__values = args; # internally we store the values in a list
# make imuList[0] = 2 raise an error, just like a tuple would
def __setitem__(self, index, value):
raise TypeError('ImmutableList does not support item assignment')
# del imuList[0] should also raise
def __delitem__(self, index, value):
raise TypeError('ImmutableList does not support item deletion')**
# make our imuList indexable, also catch the normal index error and raise one
# that tells that this is an immutable list, will make it easier to debug :)
def __getitem__(self, index):
try:
return self.__values[index]
except IndexError:
raise IndexError('ImmutableList index out of range')
# the usual stuff
def __repr__(self):
return repr(self.__values)
def __str__(self):
return str(self.__values)
# create a new imulist
e = ImmutableList(1, 2, 3, 4)
# works!
print e[0]
# raises an error
e[0] = 5
# raises another error
print e[9]
现在您所要做的就是修改类内的self.\u值。最后一个建议是,由于Python不支持,仍然可能从外部处理self.\u值
通过直接从列表中进行子类化,您可以采取进一步措施来防止操纵\uuu值,但这需要更多的工作,您仍然可以通过使用列表来摆弄这些值。uuu setitem(imListInstance,0,5)
等等。我希望外部具有不变性。那么,我可以让我的列表实例公开不可变,同时保留在内部修改自己的能力吗?只要您没有提供任何方法,如uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu setitem、uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。可以创建具有不同值的新对象并将其绑定到同一名称,但不能更改不可变对象(因此术语)。“重新实例化”的不可变对象是另一个对象。是的,我的措辞很糟糕。如果x=(1,2,3),它永远不能改变,但x可以成为另一个对象,包括另一个简单重新分配的元组。。。x=(3,4,5)。x是x,但引用的第一个元组对象x只是消失了,没有更改。def resethilf(self):super(semimmutablelist,self)。append(5):为什么使用super而不是将self传递给列表?我想知道你的例子失败是否是因为你没有返回自我,不知何故(我们有关于如何做的解释,而不是为什么它不起作用)谢谢你的帮助!有了这个和网络上的几个其他来源,我最终得到了添加为问题编辑的代码,它只是子类列表,并关闭(希望)所有内置的不变性。