This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author abarry
Recipients abarry
Date 2015-08-20.01:16:07
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1440033369.04.0.888403008868.issue24897@psf.upfronthosting.co.za>
In-reply-to
Content
This is an issue that came up quite often when creating code where you want the class' namespace to hold the instance attributes. I've often seen (and written) code like this:

class Foo:
  def __init__(self):
    self._x = 42
  @property
  def x(self):
    return self._x

As an attempt to populate the class namespace with what should normally be available on the instance. In all my projects now I use my own custom decorator to get around that.

class attribute:
  def __init__(self, func):
    self.func = func
  def __get__(self, instance, owner):
    if instance is None:
      return self
    return self.func.__get__(instance, owner)

This permits instances to override attributes set as such, like this:

class Bar:
  def __init__(self):
    self.x = 42
  @attribute
  def x(self):
    pass # placeholder

I figured I might as well suggest the idea. I'm not attached to the name, and it's more for completion's sake rather than hard necessity.
History
Date User Action Args
2015-08-20 01:16:09abarrysetrecipients: + abarry
2015-08-20 01:16:09abarrysetmessageid: <1440033369.04.0.888403008868.issue24897@psf.upfronthosting.co.za>
2015-08-20 01:16:08abarrylinkissue24897 messages
2015-08-20 01:16:07abarrycreate