擅长:python、mysql、java
<p>Python是强类型的-使用类最接近的方法是使用<code>__slots__</code>限制属性创建,例如:</p>
<pre><code>class Foo(object):
__slots__ = ['bar']
def __init__(self, bar_value):
self.barr = bar_value
the_foo = Foo('bar value')
# AttributeError: 'Foo' object has no attribute 'barr'
</code></pre>
<p>{a1中的更多信息}</p>
<p>TL;DR:<em>可以为这个类变量分配一个string、iterable或带有实例使用的变量名的字符串序列。如果在新样式的类中定义,<code>__slots__</code>为声明的变量保留空间,并阻止为每个实例自动创建<code>__dict__</code>和{<cd4>}。</em></p>