I am trying to build a database class in python, where i can add tables and rows and run other methods.
I'm facing an issue where when I add a table, it's not getting saved in the global dictionary. My code is as follows;
class DataBase():
def __init__(self):
self.data = {}
def addTable(self, tableName):
self.data[tableName] = {}
When I run that, then add a table and call on the dictionary, I still get an empty dict (as below)
tableName = "table"
DataBase().addTable(tableName)
DataBase().data
#output
{}
Can anyone help explain why this is?
CodePudding user response:
Try the following code:
tableName = "table"
x = DataBase()
x.addTable(tableName)
The problem with your code is that when you did DataBase().addTable(tablename) what python did is initialize a new object because of DataBase() and added a table to it instead of the one you wanted it to add to.
CodePudding user response:
you have to initialize the class
myDataBase = Database()