r/badcode • • Apr 24 '21

python very efficient and scalable code

Post image
1.0k Upvotes

110 comments sorted by

View all comments

63

u/RoLoLoLoLo Apr 24 '21

Since it imports tkinter with *, ugh, but that's another story, the functions are probably used for controls without inputs (like simple buttons).

The first immediately obvious improvement is deduplicating and outsourcing all logic into a helper function chosetag(tag) and just calling the helper with the corresponding string.

The second improvement, which a lot of beginners don't realize, is the fact that they can skip making special functions for every button by just using a lambda expression in the parameters for their buttons. So instead of action=chosetagComedy you can just use action= lambda : chosetag('Comedy')

2

u/Undertilted Apr 25 '21

I’m curious, what’s the problem of importing things with *? Is this just an issue with tkinter, or do you mean in general? The only thing I can think of would be that it might be confusing to see where certain methods come from, as opposed to say importing tkinter as t and doing t.func(), but I’m curious if there’s a different reason or not.

5

u/Monmine Apr 25 '21

I guess it's because it can conflict with other libraries if two identical names are found. It also makes the code harder to understand because you can't easily tell where the function was defined.