3

I wanted to get a better understanding of the difference between an:

  1. Attribute
  2. Method
  3. Property

I have created a class call Patient and I have labelled what believe are the methods and attributes:

enter image description here

Where in this code is the Property and if the property is something completely different and I have not included it in my code then using my example how can I include a property?

Also what is a Property of a Class?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 1
    property is something that is specifically made with the `@property` decorator. basically it makes that attribute not writable (read-only) and also you don't need to call it to access it, although it is defined as a method, it also has to `return` something. for example in your case you could do `p1.patient_state = None` and it would override that method and replace that as an attribute to the value of `None`, that you couldn't do if that method was created using `@property` decorator – Matiiss Oct 23 '21 at 13:40
  • 1
    @Matiiss: it's possible to create properties without `@property`, using the decorator is just easier than creating your own descriptors. Saying "specifically made" suggests there's only one way to do it. – sj95126 Oct 23 '21 at 13:46
  • I've removed a part of your post because it was a separate question and posts should focus on one question only. For the removed part there is already [this Q&A](https://stackoverflow.com/questions/4109552/python-class-definition-syntax). – mkrieger1 Oct 23 '21 at 13:48
  • One thing to keep in mind is that while "property" has a specific meaning in Python, more generally in OOP it can mean the same thing as attribute or member – juanpa.arrivillaga Oct 23 '21 at 14:34
  • 2
    Does this answer your question? [How does the @property decorator work in Python?](https://stackoverflow.com/questions/17330160/how-does-the-property-decorator-work-in-python) – fourjr Oct 24 '21 at 10:38

1 Answers1

0

There are no properties in your code. You can read this SO Answer to learn more about properties.

In short, they are typically marked by an @property decorator and therefore you can define getters and setters.

By default, every instance attribute has a getter and setter. Getting it simply returns its value and setting it sets the value. Properties allow you to modify the behaviour of these getters and setters and change the value before it is returned/set.

Brackets when creating a class are typically used for inheritance. Blank brackets are the same as not including anything.

Thus, class Person() is functionally identical to class Person.

This means that the class is simply derived from object (you can read more about that here), but you can take it as it is not derived from any parent class.

fourjr
  • 329
  • 2
  • 9
  • 1
    Do you feel that this question is ultimately a duplicate of https://stackoverflow.com/questions/17330160/how-does-the-property-decorator-work-in-python/17330273#17330273 – JonSG Oct 23 '21 at 14:22
  • @JonSG yes after the question was recently edited, it seems to be solely a duplicate – fourjr Oct 24 '21 at 10:38