Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'class,': 0.07; 'dynamically': 0.07; 'skip:p 60': 0.07; 'attributes': 0.09; 'name?': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:still': 0.09; 'def': 0.12; 'attribute,': 0.16; 'attribute.': 0.16; 'lambda': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:)?': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'slightly': 0.19; 'header:User-Agent:1': 0.23; 'received:comcast.net': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'properties': 0.29; 'class': 0.32; 'this.': 0.32; 'front': 0.32; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; 'subject:the': 0.34; 'skip:s 30': 0.35; 'done.': 0.35; 'much.': 0.36; "didn't": 0.36; 'should': 0.36; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'delete': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'simply': 0.61; 'name': 0.63; 'different': 0.65; 'here': 0.66; 'complexity': 0.84; 'remark': 0.84; 'seriously,': 0.84; 'subject:man': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ned Batchelder Subject: Re: why the attribute be deleted still in dir(man)? Date: Sat, 23 Aug 2014 20:17:00 -0400 References: <53F91A91.6080205@gmail.com> <53F9276F.9080806@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: c-50-133-228-126.hsd1.ma.comcast.net User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: <53F9276F.9080806@gmail.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1408839434 news.xs4all.nl 2922 [2001:888:2000:d::a6]:50000 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:76918 On 8/23/14 7:44 PM, luofeiyu wrote: > Think for your remark " You didn't delete the name property, which is > part of the class, not the instance." > I fix my codes to get the target done. > > class Person(object): > def addProperty(self, attribute): > getter = lambda self: self._getProperty(attribute) > setter = lambda self, value: self._setProperty(attribute, > value) > deletter = lambda self:self.delProperty(attribute) > setattr(self.__class__, attribute, > property(fget=getter,fset=setter,fdel=deletter,doc="Auto-generated method")) > def _setProperty(self, attribute, value): > setattr(self, '_' + attribute, value.title()) > def _getProperty(self, attribute): > return getattr(self, '_' + attribute) > def delProperty(self,attribute): > delattr(self,'_' + attribute) > delattr(self.__class__, attribute) > > I am so happy . > Seriously, you should listen to the people here advising you to simplify this. This is a lot of complexity to do not very much. Why not just: p = Person() p.foo = 17 What is the point of dynamically defining properties that simply front attributes with a slightly different name? Just use an attribute. -- Ned Batchelder, http://nedbatchelder.com