Perhaps you'd be better off using a standard property?  Within your Person class, you can define a property 'name' to handle what you're trying to do:<div><br class="webkit-block-placeholder"></div><div><div>
Python 2.5 (r25:51918, Sep 19 2006, 08:49:13) </div><div>[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin</div><div>Type "copyright", "credits" or "license()" for more information.</div>
<div><br class="webkit-block-placeholder"></div><div>    ****************************************************************</div><div>    Personal firewall software may warn about the connection IDLE</div><div>    makes to its subprocess using this computer's internal loopback
</div><div>    interface.  This connection is not visible on any external</div><div>    interface and no data is sent to or received from the Internet.</div><div>    ****************************************************************
</div><div>    </div><div>IDLE 1.2      </div><div>>>></div><div><br class="webkit-block-placeholder"></div></div><div><div>>>> class Person(object):</div><div><span class="Apple-tab-span" style="white-space:pre">
        </span>def __init__(self, fname, lname):</div><div><span class="Apple-tab-span" style="white-space:pre">            </span>self.fname = fname</div><div><span class="Apple-tab-span" style="white-space:pre">           </span>self.lname = lname
</div><div><span class="Apple-tab-span" style="white-space:pre">  </span>def get_name(self):</div><div><span class="Apple-tab-span" style="white-space:pre">          </span>return '%s %s' % (self.fname, self.lname)</div><div>
<span class="Apple-tab-span" style="white-space:pre"> </span>def set_name(self, name):</div><div><span class="Apple-tab-span" style="white-space:pre">            </span>self.fname, self.lname = name</div><div><span class="Apple-tab-span" style="white-space:pre">
        </span>name = property(get_name, set_name)</div><div><br class="webkit-block-placeholder"></div><div><span class="Apple-tab-span" style="white-space:pre">      </span></div><div>>>> p = Person('first', 'last')
</div><div>>>> <a href="http://p.name">p.name</a></div><div>'first last'</div><div>>>> <a href="http://p.name">p.name</a> = ('first2', 'last2')</div><div>>>> <a href="http://p.name">
p.name</a></div><div>'first2 last2'</div><div>>>> </div><div><br> </div><div>I found <a href="http://users.rcn.com/python/download/Descriptor.htm#properties">http://users.rcn.com/python/download/Descriptor.htm#properties
</a> to be a pretty good reference.</div><div><br class="webkit-block-placeholder"></div><div>Thanks,</div><div><br class="webkit-block-placeholder"></div><div>Jeff</div><div><br class="webkit-block-placeholder"></div><br>
<div><span class="gmail_quote">On 12/31/07, <b class="gmail_sendername"><a href="mailto:ct60@aol.com">ct60@aol.com</a></b> <<a href="mailto:ct60@aol.com">ct60@aol.com</a>> wrote:</span><blockquote class="gmail_quote" style="margin:0;margin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi Python Community:<br><br>Despite my new-ness to Python  I have alreadhy been able to do some (I<br>think) amazing things.  It is a truly elegant and smart language.<br><br>Yet, I can not seem to get a handle on something simple.
<br><br>I would like to make a class which has private varaiables fName and<br>lName.  It should have a property "name" which can get or set a name.<br>Something like as follows:<br><br>class Person:<br>    def __init__(self, fName="", lName=""):
<br>        self.__fName = fName<br>        self.__lName = lName<br><br>    def __getattr__(self, attr):<br>        if attr == "name":<br>            return self.__fName + " " + self.__lName<br><br>    def __setattr__(self, attr, value):
<br>        # this assumes that value is a tuple of first and last name<br>        if attr == "name":<br>            self.__fName, self.__lName = value<br><br><br>P = Person()<br><br><a href="http://P.name">P.name
</a> = ("Joe", "Smith")<br><br>print <a href="http://P.name">P.name</a><br><br>This fails with the following note:<br><br>>>><br>Traceback (most recent call last):<br>  File "C:\Python\testObject.py", line 20, in <module>
<br>    print <a href="http://P.name">P.name</a><br>  File "C:\Python\testObject.py", line 8, in __getattr__<br>    return self.__fName + " " + self.__lName<br>TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
<br><br>I don't understand why this fails.  I thought perhaps I need to make<br>the __getattr__ function like this<br><br>    def __getattr__(self, attr):<br>        if attr == "name":<br>            return self.__fName + " " + self.__lName
<br>        elif attr == "__fName":<br>            return self.__fName<br>        elif attr == "__lName":<br>            return self.__lName<br><br>But that still fails.<br><br>Can someone please tell me what I am doing wrong?
<br><br>Thansk in advance,<br><br>Chris (<a href="mailto:ct60@aol.com">ct60@aol.com</a>)<br>--<br><a href="http://mail.python.org/mailman/listinfo/python-list">http://mail.python.org/mailman/listinfo/python-list</a><br></blockquote>
</div><br> </div>