擅长:python、mysql、java
<p>您需要一个<a href="https://docs.python.org/3.8/library/functions.html#property" rel="nofollow noreferrer">^{<cd1>}</a>:</p>
<pre><code>class Dummy:
def __init__(self, x):
self.x = x
@property
def x(self):
return self._x
@x.setter
def x(self, value):
if value is None:
raise Exception("x cannot be None")
self._x = value
d = Dummy(8)
d.x = 16
d.x = None # Raises
</code></pre>