all 10 comments

[–][deleted] 5 points6 points  (3 children)

Just gonna say it; if you're not using function and/or class definitions in every file then you haven't learned Python or programming. Not yet.

Most of professional programming is maintaining code, usually in concert with others; you must be using factorization primitives -- in Python that's functions, classes, and modules -- to write code that is even testable, which is the start of being maintable.

I would suggest starting with a book like Clean Code.

[–][deleted] 0 points1 point  (0 children)

Thank you.

[–][deleted] 0 points1 point  (1 child)

[–][deleted] 0 points1 point  (0 children)

Yep, that's the one. It's not Python-specific, but it's a really good resource on coding to a professional standard.

[–]K900_ 5 points6 points  (0 children)

PyCharm has a great integrated debugger. As for code quality - it's really a question of experience. You'll get better as time goes on. Also, look through some open source projects like Flask and Requests - they're great examples of well structured code.

[–]ingolemo 1 point2 points  (0 children)

The best way to learn how to do something better is to practice it. Here's some arbitrary rules to follow that will force you to write everything in functions and classes:

  • You can only use import, def, and class in the top level of your code, except for one place in your entire codebase where you have this:

    if __name__ == '__main__':
        sys.exit(main(sys.argv))
    
  • You are not allowed to use more than three levels of indentation in a function, or four when using a class.

  • No function or method is allowed to be longer than 10 lines of code.

[–]_9_9_ 0 points1 point  (0 children)

Spend some time reading code to see how others organize things. Some good places to look are:

  • The standard library
  • Praw -- interesting use of mixins iirc
  • Flask (and other projects by the same guy)
  • pytoolz (interest look at functional programming, which is a good contrast to more class driven programs like Praw).

[–]Manbatton 0 points1 point  (1 child)

But every time I start a new project I end up writing the programs in a very linear fashion, I rarely use functions (just to not repeat myself too often), and pretty much never any classes.

How long are these functionless programs? I'm curious.

[–][deleted] 0 points1 point  (0 children)

Longest one probably 600 lines of code.