在 Python 中创建列表 属性
Create a list property in Python
我从 Python 3 开始 OOP,我发现 property
的概念非常有趣。
我需要封装一个私有列表,但是如何将这种范例用于列表?
这是我天真的尝试:
class Foo:
""" Naive try to create a list property.. and obvious fail """
def __init__(self, list):
self._list = list
def _get_list(self, i):
print("Accessed element {}".format(i))
return self._list[i]
def _set_list(self, i, new):
print("Set element {} to {}".format(i, new))
self._list[i] = new
list = property(_get_list, _set_list)
这没有按预期运行,甚至在我尝试以下代码时 python 崩溃。这是我希望 Foo
展示的虚构行为:
>>> f = Foo([1, 2, 3])
>>> f.list
[1, 2, 3]
>>> f.list[1]
Accessed element 1
2
>>> f.list[1] = 12
Set element 1 to 12
>>> f.list
[1, 12, 3]
您的代码中存在一些问题。它们可能不是唯一的问题,但解决它们会让您走得更远:
属性用于新样式 类。它们来自 object
:
class Foo(object):
getter(property
的第一个参数将在没有参数的情况下被调用。所以_get_list
不能有第二个参数i
.同样适用于_set_list
它只能有一个参数,不能有两个。(self
是隐式的,这里不算数。)
import collections
class PrivateList(collections.MutableSequence):
def __init__(self, initial=None):
self._list = initial or []
def __repr__(self):
return repr(self._list)
def __getitem__(self, item):
print("Accessed element {}".format(item))
return self._list[item]
def __setitem__(self, key, value):
print("Set element {} to {}".format(key, value))
self._list[key] = value
def __delitem__(self, key):
print("Deleting element {}".format(key))
del self._list[key]
def __len__(self):
print("Getting length")
return len(self._list)
def insert(self, index, item):
print("Inserting item {} at {}".format(item, index))
self._list.insert(index, item)
class Foo(object):
def __init__(self, a_list):
self.list = PrivateList(a_list)
然后运行这个:
foo = Foo([1,2,3])
print(foo.list)
print(foo.list[1])
foo.list[1] = 12
print(foo.list)
输出:
[1, 2, 3]
Accessed element 1
2
Set element 1 to 12
[1, 12, 3]
我从 Python 3 开始 OOP,我发现 property
的概念非常有趣。
我需要封装一个私有列表,但是如何将这种范例用于列表?
这是我天真的尝试:
class Foo:
""" Naive try to create a list property.. and obvious fail """
def __init__(self, list):
self._list = list
def _get_list(self, i):
print("Accessed element {}".format(i))
return self._list[i]
def _set_list(self, i, new):
print("Set element {} to {}".format(i, new))
self._list[i] = new
list = property(_get_list, _set_list)
这没有按预期运行,甚至在我尝试以下代码时 python 崩溃。这是我希望 Foo
展示的虚构行为:
>>> f = Foo([1, 2, 3])
>>> f.list
[1, 2, 3]
>>> f.list[1]
Accessed element 1
2
>>> f.list[1] = 12
Set element 1 to 12
>>> f.list
[1, 12, 3]
您的代码中存在一些问题。它们可能不是唯一的问题,但解决它们会让您走得更远:
属性用于新样式 类。它们来自
object
:class Foo(object):
getter(
property
的第一个参数将在没有参数的情况下被调用。所以_get_list
不能有第二个参数i
.同样适用于_set_list
它只能有一个参数,不能有两个。(self
是隐式的,这里不算数。)
import collections
class PrivateList(collections.MutableSequence):
def __init__(self, initial=None):
self._list = initial or []
def __repr__(self):
return repr(self._list)
def __getitem__(self, item):
print("Accessed element {}".format(item))
return self._list[item]
def __setitem__(self, key, value):
print("Set element {} to {}".format(key, value))
self._list[key] = value
def __delitem__(self, key):
print("Deleting element {}".format(key))
del self._list[key]
def __len__(self):
print("Getting length")
return len(self._list)
def insert(self, index, item):
print("Inserting item {} at {}".format(item, index))
self._list.insert(index, item)
class Foo(object):
def __init__(self, a_list):
self.list = PrivateList(a_list)
然后运行这个:
foo = Foo([1,2,3])
print(foo.list)
print(foo.list[1])
foo.list[1] = 12
print(foo.list)
输出:
[1, 2, 3]
Accessed element 1
2
Set element 1 to 12
[1, 12, 3]