Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python property decorator not working, why?

For some reason the 'obj._max_value' and 'obj._current_value' are not getting set. I have looked at many tutorials and it seems that I am doing it correctly. Does anyone know why it is not working?

See code: https://gist.github.com/matthew-campbell/5561562

(Python 2.7)


Update:

class Progress():

  @property
  def progress_bar_length(self):
    return self._progess_bar_length

  @progress_bar_length.setter
  def progress_bar_length(self, length):
    self._progress_bar_length = length

  @progress_bar_length.deleter
  def progress_bar_length(self):
    del self.progress_bar_length
like image 382
Matt Campbell Avatar asked May 11 '13 21:05

Matt Campbell


People also ask

What does property () do in Python?

Python's property() is the Pythonic way to avoid formal getter and setter methods in your code. This function allows you to turn class attributes into properties or managed attributes. Since property() is a built-in function, you can use it without importing anything.

What is property decorator Python?

The @property Decorator In Python, property() is a built-in function that creates and returns a property object. The syntax of this function is: property(fget=None, fset=None, fdel=None, doc=None) where, fget is function to get value of the attribute. fset is function to set value of the attribute.

How do decorators work in Python?

Decorators dynamically alter the functionality of a function, method, or class without having to directly use subclasses or change the source code of the function being decorated. Using decorators in Python also ensures that your code is DRY(Don't Repeat Yourself).

Which of the following is true about property decorator in Python?

b. property decorator is used either for setting or getting an attribute.


1 Answers

The property decorator doesn't work with old-style classes. Inherit your class from object to get a new-style class:

class Progress(object):
    # ...
like image 168
user4815162342 Avatar answered Sep 27 '22 17:09

user4815162342