Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #76914

Re: why the attribute be deleted still in dir(man)?

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 <ian.g.kelly@gmail.com>
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&gt;': 0.22; '&gt;&gt;&gt;': 0.24; "shouldn't": 0.24; '&gt;': 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 <ian.g.kelly@gmail.com>
Date Sat, 23 Aug 2014 17:22:32 -0600
Subject Re: why the attribute be deleted still in dir(man)?
To Python <python-list@python.org>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.13365.1408836196.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On Sat, Aug 23, 2014 at 4:49 PM, luofeiyu <elearn2014@gmail.com> 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 "<stdin>", line 1, in <module>
>   File "<stdin>", line 4, in <lambda>
>   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__',
>  '__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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: why the attribute be deleted still in dir(man)? Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-23 17:22 -0600

csiph-web