OMG MY BRAIN HURTS: AttributeError (I can't assign variable to self)

Question: Look at the following code, and tell my why my tiny brain can’t comprehend the error:

Repl link: Actually its on github then ported to Replit via the venv folder:

#...
class Database(abc.MutableMapping):
    """Dictionary-like interface for Repl.it Database.

    This interface will coerce all values everything to and from JSON. If you
    don't want this, use AsyncDatabase instead.
    """

    __slots__ = ("db_url", "sess")

    def __init__(self, db_url: str, backup: str=".config/db_backup.json", backup_mode: int=-1) -> None:
        pdb.set_trace()
        """Initialize database. You shouldn't have to do this manually.

        Args:
            db_url      (str): Database url to use.
            backup      (str): Location of DB backups.
            backup_mode (int): DB backup mode (-1 to disable, 0 for on_modify, n [positive integer] for recurring backup every n seconds).
        """
        print(self)
        self.backup_loc = backup
        self.backup_mode = backup_mode
        self.db_url = db_url
        self.sess = requests.Session()
        if self.backup_mode < -1:
            raise ValueError("backup_mode cannot be less than -1. Accepted values are: -1, 0, n (where n is a positive integer)")
        elif self.backup_mode >= 1:
            self.backup_thread = threading.Thread(target=self.backup)
            self.backup_thread.start()
#...

Error:

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    Database(os.environ["REPLIT_DB_URL"])
  File "/home/runner/Custom-replit-DB-module-test/venv/lib/python3.10/site-packages/repl_it/database/database.py", line 463, in __init__
    self.backup_loc = backup
AttributeError: 'Database' object has no attribute 'backup_loc'

EDIT:
self returns <repl_it.database.database.Database object at 0x7f90ae17ff10>

Solved: abc.MutableMapping is a read_only class, and I had to specify the variable names in __slots__

5 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.