4

I've got a main class that holds the program's mainloop and other variables and such.

class main:
    def __init__(self):
       blah = 'blah'
       blah2 = 'blah'
       blahhandler = BlahHandler.BlahHandler(self)
       blahhandler2 = BlahHandler.BlahHandler2(self)

    def mainloop(self):
       < IRRELEVANT JUNK >

But is passing 'self' to the blahhandlers a bad idea, so the handlers can do stuff such as..

#In class 'BlahHandler'
self.main.blahhandler2.doBlah()
print(self.main.blah)

Or should I just pass the blahhandler2 directly to the blahhandler rather than passing the whole main class? Sorry if this is incomprehensible just because it all sounds like 'blah'.

Thanks!

Name McChange
  • 2,750
  • 5
  • 27
  • 46
  • "I've got a main class that holds the program's mainloop and other variables and such." This reflects a complete misunderstanding of the purpose of classes. – Karl Knechtel Jun 06 '12 at 00:20
  • @KarlKnechtel Isn't this REQUIRED in Java? I'm actually trying to code in a Java'y way so I can later switch to it. – Name McChange Jun 06 '12 at 02:20

1 Answers1

3

There is nothing wrong with passing your main object to BlahHandler as you are currently doing.

Which one is better depends entirely one what your objects actually represent. Here are a few questions to get you started:

  • Does it make sense for a BlahHandler to have its own reference to a BlahHandler2, or when a BlahHandler is refererring to a BlahHandler2 is it always referring to the BlahHandler2 that it's main owns?
  • Should a BlahHandler really have a reference to a main, or are the attributes to main that should be hidden from BlahHandler?

Ask questions like these about structure early on in development, just because what you currently have works and isn't bad practice, doesn't mean it makes the most sense (but it might!).

Don't worry about passing "the whole main class" from a speed or memory perspective, all you are passing is a reference.

Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
  • I didn't figure it would affect speed or memory, I just thought that passing it would be some sort of forbidden bad coding practice. Thanks for clearing it up! – Name McChange Jun 05 '12 at 23:40
  • I agree that this isn't necessarily bad practice, however, from a memory perspective, you are (likely) creating a cyclic reference which means that your objects can't be garbage collected without you intervening to break the cycle. Most of the time, that's probably not a big deal -- a few kilo-bytes here and there, but I thought it was worth mentioning. – mgilson Jun 06 '12 at 03:47
  • @mgilson According to Google, that only happens when you override a class's __del__ function. – Name McChange Jun 06 '12 at 13:56
  • @SuperDisk -- Really? that's interesting. Do you have a link -- I'd like to learn something new today :) – mgilson Jun 06 '12 at 14:07
  • 1
    @Mgilson http://stackoverflow.com/questions/2428301/should-i-worry-about-circular-references-in-python Here you go. According to Ned, the cycle will be stopped when Python detects it. – Name McChange Jun 06 '12 at 14:12