I am new to python coming from a java background so I am having a lot of head scratching moments. One of them is avoiding side effects when importing scripts. I started using Flask for a project and I have a piece of code that looks like this:
db = SQLAlchemy()
class Users(db.Model, UserMixin):
user_id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(50), nullable=False, unique=True)
password = db.Column(db.String(80), nullable=False)
def get_id(self):
return self.user_id
now, here db is just smack dab in the start of the script just sitting there menacingly. I don't really like that, not sure if it's just something normal in python? Right now I started doing this:
def get_user_descriptor(db: SQLAlchemy):
class Users(db.Model, UserMixin):
user_id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(50), nullable=False, unique=True)
password = db.Column(db.String(80), nullable=False)
root_folder = db.Column(db.String(50), nullable=False, unique=True)
def get_id(self):
return self.user_id
return Users
and from there I use it as I see fit. This, however, also feels a little weird.
Is it just me? And if it's not, what is an appropriate way to deal with this?