Re: Newbie: Why doesn't this work
- From: ct60@xxxxxxx
- Date: Mon, 31 Dec 2007 10:01:38 -0800 (PST)
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@xxxxxxx)
.
- Follow-Ups:
- Re: Newbie: Why doesn't this work
- From: Gabriel Genellina
- Re: Newbie: Why doesn't this work
- References:
- Newbie: Why doesn't this work
- From: ct60
- Re: Newbie: Why doesn't this work
- From: Tim Chase
- Newbie: Why doesn't this work
- Prev by Date: What's the limit of variables size in pyhton?
- Next by Date: Re: evolution-python
- Previous by thread: Re: Newbie: Why doesn't this work
- Next by thread: Re: Newbie: Why doesn't this work
- Index(es):