4

I have been reading a tutorial book for python and it briefly discussed properties. From what I understand, when a class attribute is requested, python directs the request to the property method that returns the attribute so that code can be run before the attribute is accessed. But why is this necessary, when in the example below, the attribute v being returned cannot even be accessed with dot notation?

@property
def value(self):
    if self.is_face_up:
        v = BJ_Card.RANKS.index(self.rank) + 1
        if v > 10:
            v = 10
    else:
        v = None
    return v 
Isaac Johnson
  • 89
  • 1
  • 4
  • 2
    _"the attribute being returned cannot even be accessed with dot notation"_ What do you mean? `YourClass().value` works just fine. – Aran-Fey Aug 25 '17 at 14:02
  • Yes I realise now that you can use dot notation on the method- treating it like the variable v. When I wrote the question, I was confused because I couldn't get the variable v by typing MyClass.v but now I know that that is not necessary as you can just invoke the method. – Isaac Johnson Aug 25 '17 at 14:59
  • I know this is an old question, but I don't think I should open a new question for this. Why can I not just define `self.value = 10` (or the appropriate correct value) directly? (Then I'd have an attribute which can be accessed with dot notation from without the class.) Why is it desirable to use the `@property` decorator, so that I can write a method which acts like an attribute? It seems like extra work. (I also ping @DietrichEpp who answered this question.) – NerdOnTour Mar 07 '22 at 14:50

1 Answers1

13

The attribute cannot be accessed with dot notation inside the function because that would recursively call the property getter and cause a stack overflow:

class A:
    @property
    def x(self):
        return self.x # StackOverflow

However, the whole point of the @property is exactly to make it accessible with dot notation. The following are equivalent:

# Without @property
class A:
    def x(self):
        return 3

a = A()
print(a.x()) # prints 3

# With @property
class A:
    @property
    def x(self):
        return 3

a = A()
print(a.x) # prints 3
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415