0

I am learning OOP with Python and I am trying to figure how to get the accessor and mutator method for class level variables. I have no issues with instance level variables but I am having trouble with the class level ones.

from abc import ABC, abstractmethod

class Book(ABC):
    #Class Variable
    _libraryLoanDuration = 14 
    def __init__(self, bookName, yearPublished, cost):
        self._title = title
        self._yearPublished = yearPublished
        self._cost = cost


    #Accessor for class variable
    @property
    def libraryLoanDuration(cls):
        return cls._libraryLoanDuration

    #Mutator for class variable
    @classmethod
    def libraryLoanDuration(cls, newlibraryLoanDuration):
        cls._libraryLoanDuration = newlibraryLoanDuration


Asri
  • 315
  • 3
  • 13
  • What exactly are the troubles you're having? I am guessing it has to do with the fact that `@property` is for ***instances***. You might be looking for a [`@classmethod`](https://stackoverflow.com/questions/12179271/meaning-of-classmethod-and-staticmethod-for-beginner). But bear in mind that what you're doing is completely not necessary. Getters and setters are considered *not pythnic*. You can just access the class attributes directly: `Book._libraryLoanDuration` – Tomerikoo Apr 02 '21 at 15:37
  • Making all attributes pseudo-private and writing getters and setters is widely considered an antipattern on Python. Using properties is accepted in case where you need them. Which means that you need special code for handling the value. – Klaus D. Apr 02 '21 at 15:40

0 Answers0