Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #76914 > unrolled thread
| Started by | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| First post | 2014-08-23 17:22 -0600 |
| Last post | 2014-08-23 17:22 -0600 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: why the attribute be deleted still in dir(man)? Ian Kelly <ian.g.kelly@gmail.com> - 2014-08-23 17:22 -0600
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2014-08-23 17:22 -0600 |
| Subject | Re: why the attribute be deleted still in dir(man)? |
| Message-ID | <mailman.13365.1408836196.18130.python-list@python.org> |
[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 top | Article view | comp.lang.python
csiph-web