[Tutor] method could be a function warning

Mats Wichmann mats at wichmann.us
Wed Dec 22 20:38:36 EST 2021


On 12/22/21 17:59, Phil wrote:
> I've been in the habit of mixing methods and functions within the GUI
> class and to get the functions to work I've had to prefix the call to
> the function with .self and add self as a function argument. The
> function dummy in the following code is an an example of this.

The suggestion (as I recall it's not a "warning" but a "recommendation",
but my memory isn't that great) is that because the method doesn't make
any use of "self", why have it be a method that takes "self" as an
argument? But it's up to you... it's okay to put something that _could_
have been a global function inside the class because it's logically
connected to the things the class does, and not interesting otherwise.
You're certainly allowed to add indications (in the form of comments) to
tell the checker to shut up about this.
> 
> It makes more sense to me, and I think it's correct, to define and call
> the functions outside the GUI class as I've done with my_function below.
> Also, I'm thinking that the GUI class should be in a file of it's own
> because the total number of lines of code exceeds 500 thus making
> scrolling tedious.
> 
> I ask because I'm in the process of tidying up a Conway's Game of Life
> project that I wrote 3 years ago.
> 
> import tkinter as tk
> 
> class Root(tk.Tk):
>     def __init__(self):
>         super().__init__()
> 
>         self.title('test')
>         self.geometry('300x300')
> 
>         self.frame = tk.Frame()
> 
>         self.dummy()
> 
>     def dummy(self):
>         print('hello')
> 
> def my_function():
>     print('my function')
> 
> my_function()
> 
> if __name__ == '__main__':
>     root = Root()
>     root.mainloop()
> 



More information about the Tutor mailing list