Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Python programing general question about structure?

When i write something in python i find that it is many lines of control flow statements, comments, variable declartion etc. There are harldy any functions and none of my own modules are being imported. Its just a wall of code it seems. How is the best way to structure code in terms of breaking up control loops and functions into seperate modules and importing them into a main program? Should a main program just be made up of functions, that rely on imported modules? Is there someplace where i see some extensive code written in a nice way?

2 Answers

Relevance
  • 3 years ago
    Favorite Answer

    There is no single format to follow. It highly depends on what the purpose of the code is.

    If it's just a small, single purpose, script then it's fine that you don't have a main function, or any functions at all.

    But in general, try to think about how your code might be able to be reused in the future. Say your program needs to perform a task over and over again then it might make sense to make that task into a separate function.

    Or if you have a central data structure, and functions that operate on that structure, you might consider making a class, and adding all those functions to your class. Now your program simply needs to declare an instance of that class, and then operate on it. This also gives you further flexibility as you can have 2 separate instances that have their own data.

  • Brian
    Lv 6
    3 years ago

    Thank you

Still have questions? Get your answers by asking now.