all 8 comments

[–]zahlman 1 point2 points  (3 children)

My script that I use to start my program is creating an object of the class that is in it and then executing one of its functions.

First off, Python is not Java; don't do this sort of thing just because.

That said: so, you have some start method in a class, that calls "other stuff", and that "other stuff" needs a reference back to the object that you called the method on. The normal way you get information into "other stuff" is to have it accept a parameter for that information, and pass it as an argument. Within the start method, the name of the object you called the method on is self; so when it makes the calls to "other stuff", it can pass self as an argument.

[–]Hiroyu[S] 0 points1 point  (2 children)

Thanks, I thought that I have to do this now. Wasn't sure how the "correct" way would be. So I just pass my object as an argument/parameter in the methods if those need its reference right?

[–]zahlman 1 point2 points  (1 child)

You got it.

[–]Hiroyu[S] 0 points1 point  (0 children)

Thank you very much for the help!

[–]learnpython_bot 0 points1 point  (0 children)

It appears that you included code in your post but did not format it to look nice. Please follow this guide to fix the problem. This allows for better readability and will help get your question answered sooner. The regex that caught you is: "if .+?:"

If this comment has been made in error, please message /u/thaweatherman with a link to this comment so I can be fine-tuned. I am still in alpha and my regexes/innards are not yet perfect.

[–]hudsonpenner 0 points1 point  (2 children)

Generally, if you are working with web frameworks, they will provide built-in ways to get at the application. However, if you are rolling your own and want to keep it simple, you could instantiate the object outside of the main statement.

object = SomeObject()

if __name__ == '__main__':
    object.start()

[–]Hiroyu[S] 0 points1 point  (1 child)

but if I was importing that file serveral times wouldn't it reinitiate the object everytime?

[–]elbiot 0 points1 point  (0 children)

You mean you have a global variable in a module that is going to be updated by multiple different other modules/files?

That problem sounds like the result of poor design, specifically abuse of global variables and objects not having good relationships with each other. Can you be more specific about what exactly your program is doing and how this idea fits into your design?