擅长:python、mysql、java
<p>没有一种内在的方法可以做到这一点。不过,您可以为列表定义一个包装类:</p>
<pre><code>class ListWrapper(list):
# Constructor
__init__(self, **kwargs):
allIsGood = False
# 'kwargs' is a dict with all your 'argument=value' pairs
# Check if all arguments are given & set allIsGood
if not allIsGood:
raise ValueError("ListWrapper doesn't match schema!")
else:
# Call the list's constructor, i.e. the super constructor
super(ListWrapper, self).__init__()
# Manipulate 'self' as you please
</code></pre>
<p>在需要非空列表的任何地方使用<code>ListWrapper</code>。如果您愿意,您可以将模式的定义外部化,并将其作为输入添加到构造函数中。在</p>
<p><em>另外:</em>您可能想看看<a href="https://stackoverflow.com/questions/4093029/how-to-inherit-and-extend-a-list-object-in-python">this</a></p>