Newbie: Why doesn't this work
ct60 at aol.com
ct60 at aol.com
Mon Dec 31 13:01:38 EST 2007
Thanks you Gabriel and Timm for your thoughtful responses. I am very
appreciative.
I had heard about the properties function, but wanted to understand
the old syntax first before I tried that. Thanks to your responses, I
was able to see what the problem was.
Here is a solution I came up with:
class Person():
def __init__(self, fName="", lName=""):
self.__fName = fName
self.__lName = lName
def __getattr__(self, attr):
if attr == "name":
return self.__fName + " " + self.__lName
else:
return self.__dict__[attr]
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
else:
self.__dict__[attr] = value
P = Person("Joe", "Smith")
print P.name
P.name = ("Jane", "Doe")
print P.name
This works as expected printing "Joe Smith" and then "Jane Doe".
To be honest, I think the above old syle (I guess) method is pretty
cool and elegant.
Thanks again and have a GREAT NEW YEAR!!
Chris (ct60 at aol.com)
More information about the Python-list
mailing list