[Python] How necessary is bringing outside objects into inside of my python class, especially...

Discussão em 'Python' iniciado por Stack, Março 3, 2025.

  1. Stack

    Stack Membro Participativo

    I'm making a program in python using tkinter and I'm wondering if the complexity of passing in sqlite3.cursors and such things into the _ _init_ _ function of my class is necessary?

    I get that it would be necessary if I were to make these classes into an import but I don't think there is really any reason to do that

    Here is an example of what i mean.

    class myWindow(tk.TopLevel):
    def __init__(self, dbcursor, *arg, *kwargs):
    self.dbcursor = dbcursor

    results = self.dbcursor.execute(query, etc.)
    # more database querying stuff with self.dbcursor


    Is this really necessary if I'm not making this into its own import? If this is the only time it's going to be used, wouldn't it be also fine to just use an outside variable? For example

    dbcursor = sqlite3.connect("some database.db").cursor()

    class myWindow(tk.TopLevel):
    def __init__(self, *arg, *kwargs):
    results = dbcursor.execute(query, etc.)
    # continue using the outside dbcursor


    Is the added complexity, possible more memory taken up, and the added unreadability necessary? Wouldn't it be fine to not pass things in considering my program is standalone and doesn't have to be portable into other programs?

    Also, if using the outer dbcursor is alright, then would i have to use the global keyword? This also applies to things that aren't just the database cursor.

    This applies to outside functions, variables, objects, etc.

    With this being a Gui program the classes are already quite large so if i can cut down on the bloat that would be great

    Continue reading...

Compartilhe esta Página