[New-bugs-announce] [issue24897] Add new attribute decorator (akin to property)?

Emanuel Barry report at bugs.python.org
Thu Aug 20 03:16:09 CEST 2015


New submission from Emanuel Barry:

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.

----------
messages: 248869
nosy: ebarry
priority: normal
severity: normal
status: open
title: Add new attribute decorator (akin to property)?
versions: Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24897>
_______________________________________


More information about the New-bugs-announce mailing list