[Tutor] method could be a function warning

Alan Gauld alan.gauld at yahoo.co.uk
Wed Dec 22 20:29:16 EST 2021


On 23/12/2021 00: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 function dummy is a method not a function.
A method in OOP is how an object responds to a message.
In this case the message is Rootinstance.dummy() and
the method for handling that message is Root.dummy

In practical terms (in Python) a function defined inside
a class which takes as its firs parameter an instance of the
same class is a method.

The function my_function() is not a method since it is
defined outside the class and does not take an instance
as its first parameter.

> 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. 

Functions are defined outside the class, methods are
defined inside. Functions or methods can be called
from inside or outside the class.


> 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.

That's a style issue but once you get beyond a few hundred
lines it's usually better to split it into a module.

However, your Root class is probably only ever going to
have a single instance which represents the application itself.
As such I don't know what you are going to put in any higher level
file. Won't it just consist of an import and instantiation of Root?
(essentially just the last 3 lines below) which seems a bit pointless.

> I ask because I'm in the process of tidying up a Conway's Game of Life 
> project that I wrote 3 years ago.

I wouldn't expect that to even take 500 lines in total.
I did a (mostly non-OOP*) curses version of life for my recent
book and it was only 98 lines in total, a GUI shouldn't
be much bigger! Even if you add an interactive start screen
which mine didn't have, it shouldn't be more than 200
lines tops.

What is making up all that code?

(*)It had a Cell class with a few attributes only and
5 functions including a main().

> 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()
> 


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list