Python structure question - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Python structure question (/thread-1183.html) |
Python structure question - PickyBiker - Dec-11-2016 I'm struggling with the overall structure of a python program. The following code works (as-is), but I'm not sure this is the way to let a main program function access class elements. If I move the class Test to a test.py file and then import that file it doesn't work so there must be a better way. The import being used is: import test What am I doing wrong? class Test(object): def __init__ (self): pass class a(object): def __init__ (self): print("a") class b(object): def __init__ (self): print("b") def main(): t = Test() t.a() t.b() print("Done") if __name__=="__main__": main() RE: Python structure question - metulburr - Dec-11-2016 (Dec-11-2016, 03:34 PM)PickyBiker Wrote: I'm struggling with the overall structure of a python program. The following code works (as-is), but I'm not sure this is the way to let a main program function access class elements. If I move the class Test to a test.py file and then import that file it doesn't work so there must be a better way.You still need to create an object like you did in main(). Also note that the content in your if condition (calling main) if __name__ == '__main__' only runs when this scripts is ran directly, not imported. The creation of objects will normally be in the script you import this from, not inside the module itself. test is the name of your class. If you name this file "my_module" you would import it as import my_moduleand then used as my_object = my_module.test()or import the class only from my_module import testand then used as my_object = test()see here for more info http://python-forum.io/Thread-Basic-Modules-part-2 RE: Python structure question - PickyBiker - Dec-11-2016 Ah, yes. Don't just import the module name, I have to reference the class as well. In that case, just changing t = test() to t = test.test makes it work. But I also see your recommendation to change the file name also makes it less confusing. I'm sure the python light will go on soon... right now it's just a dull glow for me. Thank you. RE: Python structure question - metulburr - Dec-11-2016 i actually use the class name as the module name too. However the standard name convention dictates that the class name would be capitalized and the module name not. obj = test.Test()You know right from the bat, that test is the module, and Test is the class just be naming convention. PEP 8 https://www.python.org/dev/peps/pep-0008/#class-names RE: Python structure question - PickyBiker - Dec-11-2016 Quote:The creation of objects will normally be in the script you import this from, not inside the module itself. When I move t = module.test to the module file I changed it to t = test Then main() was changed to look like this: import module def main(): s = module.t s.a() s.b() print("done") if __name__=="__main__": main()Is that what you meant? |