Hide
Know what you're getting – Unlike many sites, all our code is clearly licensed. Join Siafoo Now or Learn More

LEVEL UP!

over 7 years ago

I've just entered a realm of understanding about OOP that I had never conceived of before. I just did some object recursion type things that I didn't even think were possible! But now that I've done them and demonstrated them to myself, I have to wonder how I ever did any useful coding without this functionality.

I used to think objects were really restrictive because methods inside of one object, in my viewpoint, weren't able to do modifications to another object - especially not the 'calling' object. Like if I have a GUI class, and then I have a Settings class. The GUI class puts the Settings class in a variable in itself, and calls some methods in it. Well, things in the Settings class may necessitate changing things in the GUI class. I always thought that this required doing returns of codes and having the GUI 'listen' for a change to be made... I knew I was missing something, but I wasn't sure what. It felt like too much work compared to the ease and convenience that OOP is always touted to be.

This is the way I thought about OOP for years. Tonight, I was messing around with some rewritten code for the app, and I wanted to use the Tkinter IntVar, StringVar, etc. variable classes with the objects I made that held data about sessions, subsessions and lines in a log. But I also wanted this Session class to be separate from the GUI class. Therefore, the Session class didn't come with a Tk() object to get stuff it needs from, so it can't use the IntVar by itself. I wanted so badly to figure out how to make the Session class aware of the GUI class - up till this point, the GUI class was aware of the Session class, but not vice versa.

Then I realized I could just pass the GUI object to the Session class, and put that reference into a variable in the Session class. I took it a step further when I made the classes that Session would use to create Subsessions and LogLines, and kept passing the GUI object down to each class's __init__ method.

Not only was I able to make Session, Subsession and LogLine aware of GUI, but I was able to, from within LogLine, make changes to the GUI object! Just to make sure that it was making changes to the original object and not a copy of the object, I had the original GUI object print the value of an attribute that LogLine had changed. It worked!

If programming is a roleplaying game, I've just leveled up about 5 times over...

Keywords oop