Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3a.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; 'attribute': 0.07; 'class,': 0.07; 'modifying': 0.07; 'skip:p 60': 0.07; 'defines': 0.09; 'instance.': 0.09; 'instances.': 0.09; 'subject:still': 0.09; 'def': 0.12; "'__doc__',": 0.16; '23,': 0.16; 'attribute,': 0.16; 'attribute.': 0.16; 'lambda': 0.16; 'silly': 0.16; 'storing': 0.16; 'subject:)?': 0.16; 'sat,': 0.16; 'do,': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'aug': 0.22; 'email addr:gmail.com>': 0.22; '>>>': 0.24; "shouldn't": 0.24; '>': 0.26; 'class.': 0.26; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; "skip:' 10": 0.31; '"",': 0.31; 'file': 0.32; 'class': 0.32; 'skip:m 30': 0.32; '(most': 0.33; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; 'subject:the': 0.34; 'skip:s 30': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'instances': 0.36; "didn't": 0.36; 'method': 0.36; 'skip:& 10': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'recent': 0.39; 'skip:& 20': 0.39; '12,': 0.39; 'bad': 0.39; 'structure': 0.39; 'delete': 0.39; 'to:addr:python.org': 0.39; 'even': 0.60; 'skip:\xc2 10': 0.60; "you're": 0.61; 'name': 0.63; 'design.': 0.68; "'person'": 0.84; 'subject:man': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=yWJ6q8MZdxjYFzzNakVCK8Ms05i+nPpd3n5zeNmW5D0=; b=asQei9VsjIex9dmLGon5IFjxUfWu5r0ZrvexXtWEjb7MwoZtSkoMuywzLkF1g5Q3vA WPYezS/LKP1yRS8cVEjZ8Wfzj65DYcYGs8vMNrPdn5ACoUDzitp8vLMcMaOT+v04KvzV glIcHqSgqSD6As3PfMvVgD9EVSg5zmaQF+AapUe5XWNkzgPsvSKoIb32KgbatfanqIio fp99teQ93dMLP/v8oTYrsxbFqZVKLv3Xt1m8I6qLY5UNNPYm+z1JKSbFqI4bcyI8/ZzO szveRmvEjexKePWkpJtT46/z0da9eI6umtQvrzAYhxMVULUesrBBDlSSYzMKW5DWBLfz wGyA== X-Received: by 10.70.131.12 with SMTP id oi12mr16834644pdb.116.1408836192846; Sat, 23 Aug 2014 16:23:12 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <53F91A91.6080205@gmail.com> References: <53F91A91.6080205@gmail.com> From: Ian Kelly Date: Sat, 23 Aug 2014 17:22:32 -0600 Subject: Re: why the attribute be deleted still in dir(man)? To: Python Content-Type: multipart/alternative; boundary=001a11c3d0ccd24b150501543c4f 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: 111 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1408836196 news.xs4all.nl 2932 [2001:888:2000:d::a6]:36773 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:76914 --001a11c3d0ccd24b150501543c4f Content-Type: text/plain; charset=UTF-8 On Sat, Aug 23, 2014 at 4:49 PM, luofeiyu wrote: > > 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) Unless you're going to have the property actually do something, this is silly and useless. Just use a normal attribute. Even if this is really what you want to do, it's bad design. The class defines the instances. You shouldn't have an instance method modifying the structure of other instances of the class. > >>> man.delProperty("name") > >>> man.name > Traceback (most recent call last): > File "", line 1, in > File "", line 4, in > File "", line 12, in _getProperty > AttributeError: 'Person' object has no attribute '_name' > >>> dir(man) > ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__form > at__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', > '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__ > repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref_ > _', '_getProperty', '_setProperty', 'addProperty', 'delProperty', 'name'] You deleted the _name attribute where you're storing the value of the name property. You didn't delete the name property, which is part of the class, not the instance. --001a11c3d0ccd24b150501543c4f Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On Sat, Aug 23, 2014 at 4:49 PM, luofeiyu <elearn2014@gmail.com> wrote:
>
= > class Person(object):
> =C2=A0 =C2=A0 =C2=A0 =C2=A0def addProper= ty(self, attribute):
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 getter =3D lambda sel= f: self._getProperty(attribute)
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 setter =3D lambda self, value: self._setProperty(attribute, v= alue)
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 deletter =3D= lambda self:self.delProperty(attribute)
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 setattr(self.__class_= _, attribute, property(fget=3Dgetter,fset=3Dsetter,fdel=3Ddeletter,doc=3D&q= uot;Auto-generated method"))
> =C2=A0 =C2=A0 =C2=A0 =C2=A0def _s= etProperty(self, attribute, value):
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0setattr(self, '_' + attribute, value.title())
> =C2=A0 =C2=A0 =C2=A0 def _getProperty(self, attribute):
> =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return getattr(self, '_' + attr= ibute)
> =C2=A0 =C2=A0 =C2=A0 def delProperty(self,attribute):
>= ; =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 delattr(self,'_' + attr= ibute)

Unless you're going to have the property actually do something, th= is is silly and useless. =C2=A0Just use a normal attribute.

<= /div>
Even if this is really what you want to do, it's bad design. = The class defines the instances. You shouldn't have an instance method = modifying the structure of other instances of the class.

> >>> man.delProperty("name")
=
> >>> man.name
&g= t; Traceback (most recent call last):
> =C2=A0 File "<stdin&g= t;", line 1, in <module>
> =C2=A0 File "<stdin>", line 4, in <lambda>
&g= t; =C2=A0 File "<stdin>", line 12, in _getProperty
> = AttributeError: 'Person' object has no attribute '_name'> >>> dir(man)
> ['__class__', '__delattr__', '__dict__', '= __dir__', '__doc__', '__eq__', '__form
> at__= ', '__ge__', '__getattribute__', '__gt__', '= ;__hash__', '__init__', '__le__',
> =C2=A0'__lt__', '__module__', '__ne__', '_= _new__', '__reduce__', '__reduce_ex__', '__
>= repr__', '__setattr__', '__sizeof__', '__str__'= ;, '__subclasshook__', '__weakref_
> _', '_getProperty', '_setProperty', 'addProper= ty', 'delProperty', 'name']

You delet= ed the _name attribute where you're storing the value of the name prope= rty. You didn't delete the name property, which is part of the class, n= ot the instance.
--001a11c3d0ccd24b150501543c4f--