Why can not initialize the class?

Larry Martell larry.martell at gmail.com
Fri Aug 22 10:36:00 EDT 2014


On Fri, Aug 22, 2014 at 10:26 AM, luofeiyu <elearn2014 at gmail.com> wrote:
> System:win7+python34.
>
>     class Contact(object):
>         def __init__(self, first_name=None, last_name=None,
>                      display_name=None, email=None):
>             self.first_name = first_name
>             self.last_name = last_name
>             self.display_name = display_name
>             self.email = email
>         def print_info(self):
>             print(self.display_name, "<" + self.email + ">"  )
>         def set_email(self, value):
>             if '@' not in value:
>                 raise Exception("This doesn't look like an email address.")
>             self._email = value
>         def get_email(self):
>             return self._email
>         email = property(get_email, set_email)
>
>     contact = Contact()
>
> The error message is :
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "<stdin>", line 7, in __init__
>   File "<stdin>", line 11, in set_email
> TypeError: argument of type 'NoneType' is not iterable
>
> What is wrong with the code?

The 'in' operator requires an iterable. When you do 'self.email =
email' set_email gets called and value is None.



More information about the Python-list mailing list