Re: Newbie: Why doesn't this work



A couple items of note:

class Person:

This should be "class Person(object)" to take advantage of some
of the features that new-style classes offer...particularly in
this case.

def __init__(self, fName="", lName=""):
self.__fName = fName
self.__lName = lName

def __getattr__(self, attr):
if attr == "name":
return self.__fName + " " + self.__lName

def __setattr__(self, attr, value):
# this assumes that value is a tuple of first and last name
if attr == "name":
self.__fName, self.__lName = value

if the attr isn't "name", no default behavior gets called here.
The common way, with new-style classes is to add

else:
parent(Person, self).__setattr__(attr, value)

Do be aware that this has some odd behaviors when what you put in
and what you get out are different types:

p1.name = ("Joe", "Smith")
p2.name = p1.name
Traceback (most recent call last):
File "x.py", line 22, in ?
p2.name = P.name
File "x.py", line 13, in __setattr__
self.__fName, self.__lName = value
ValueError: too many values to unpack

(slightly munged traceback as it actually came from the test
input file rather than the interactive prompt)

-tim



.