擅长:python、mysql、java
<p>在我的评论中,你需要确定新邮件是否在<code>first.last@email.com</code>中,然后设置<code>first</code>和<code>last</code>属性。你知道吗</p>
<p>虽然我不会使用电子邮件作为属性,如果你是基于名称创建它,你应该改变名称本身。你知道吗</p>
<pre><code>class Employee:
def __init__(self, first, last):
self.first = first
self.last = last
def email(self):
return f"{self.first}.{self.last}@email.com".lower()
def __repr__(self):
return f"<Employee | {self.first} {self.last}>"
john = Employee('John', 'Smith')
print(john)
#<Employee | John Smith>
john.first = "Joe"
print(john)
#<Employee | Joe Smith>
</code></pre>
<p>否则,如果你想为电子邮件设置一个setter,那么我建议使用它来设置第一个和最后一个属性,但是你不需要返回值,因为你只是在设置你已经知道的电子邮件。我会使用<code>re</code>库来检查电子邮件的格式是否正确。这是一个非常粗略的例子:</p>
<pre><code>@email.setter
def email(self, new_email):
try:
self.first, self.last = re.search('(?P<first>\w+).(?P<last>\w+)@\S+', email).group('first', 'last'))
except AttributeError:
raise ValueError("Email must be in "first.last@email.com")
</code></pre>