2

On classes you can create properties:

class foo():
    _bar = None
    @property
    def bar(self):
       return self._bar
    #----------------------------------------------------------------------
    @bar.setter
    def (self, value):
        """"""
        self._bar = value

Which is awesome, but I was wondering if it was possible to extend @property and setter outside the class setting.

So in a module I could have:

_bar = None
@property
def bar():
   return _bar
#----------------------------------------------------------------------
@bar.setter
def (self, value):
  _bar = value

That way I can have this operation bar on the module level:

>>> print(mymodule.bar)
None
>>> mymodule.bar = "Fish"
>>> print(mymodule.bar)
Fish
code base 5000
  • 3,812
  • 13
  • 44
  • 73
  • 1
    Possible duplicate of [How do I define a Python property \*outside\* of a class definition?](https://stackoverflow.com/questions/7948691/how-do-i-define-a-python-property-outside-of-a-class-definition) – Aran-Fey Apr 06 '18 at 11:33
  • You can't. Properties only work in combination with a class. – Aran-Fey Apr 06 '18 at 11:34
  • 1
    [This question](https://stackoverflow.com/questions/880530/can-modules-have-properties-the-same-way-that-objects-can) is also relevant. – Paulo Almeida Apr 06 '18 at 11:35
  • 1
    This is the best question today, _bar = None. ;-) – deceze Apr 06 '18 at 11:39

0 Answers0