Why all those people organize their code the same way?

Why all those people organize their code the same way?


This all day: https://realpython.com/python-application-layouts/


I see a lot of people referencing real python. Is this like the investopedia of the python world? Would you suggest following their courses to start as opposed to freecodeacamp?


I’m the writer of the above article and so I’m biased, but I’d say it is, but more. You have reference style articles, project tutorials, and deep dives into the language itself and how it works under the hood. I haven’t looked at FCC in years, but from what I remember of it the courses aren’t as varied or in-depth as you’d find in RP. We put a lot of work into creating actionable learning paths that have real results (such as a large project).


The Real Python project is great. I listened to the first episode of the podcast where they spoke about decorators. It was nice, because this guy who’d been coding since he was 9 even said that they’re a little challenging to wrap your head around, but that once you understand them, your solid. Hearing someone with so much experience say that was refreshing.


I can definitely second that! I had a project where I created a set of decorators for observability and it took a lot of reading for the flow of them to finally click.


I still haven’t found the best way to implement them for something that I’m doing yet, but I know a day will come. What do you mean by observability though?


Observability refers to knowing what's going on with data and code as it runs using things like logs, metrics, etc. ([here's a much better explanation](https://dzone.com/articles/rethinking-programming-automated-observability)). So with the library I wrote, anyone building code at my company can just use the decorators I wrote to quickly attach standardized logging and other capabilities to their code. For us, this is mostly for AWS lambda functions (not to be confused with [Python lambdas](https://realpython.com/python-lambda/)) and prevents us from having a patchwork of different logging setups, metrics collected, and whatever else.


Thank you for your great work, that was such a helpful article.


honestly, it's the best website i found for python, they literally dig deep into topics rather than just telling how something works, altho to have full access to the site you need to have membership, which means paying monthly whereas freecodecamp is, as the name says, free. :/ i tried their beginner book named "Python Basics" and i'm telling you, it really cleared my fundamentals, it is a paid ebook tho, but this is what i'd recommend to someone starting with python.


I have no idea what investopedia is lol When I need a quick reference on a topic, I often check realpython first because it is usually reliable and clear. But freecodecamp is amazing too, and I might go there if I want a longer-form more classroom type experience on something from the ground up. For instance, this bit on virtual environments in conda from freecodecamp is amazing: https://www.freecodecamp.org/news/why-you-need-python-environments-and-how-to-manage-them-with-conda-85f155f4353c/ Frankly for initially *learning* Python I would use Matthes' book Python Crash Course there is nothing as good. And learning with a book is better because you are more likely to be forced to *type code* as opposed to cut/paste code. Coding is muscle memory largely, and if you want to learn to code you should be typing a ton of code, not cutting/pasting. So learning from online web sites it makes it way too easy to just cut/paste/nod which is really dangerous. Plus you always will have that book on your bookshelf (or whatever people have these days), and can go back to it when you want to refresh what a keyword argument is (and how it differs from a default argument), refresh from your canonical references. It's all in one place instead of spread out over a bunch of different web sites that you will forget/lose over time (the downside obviously is it doesn't change as the programs evolve). I know this is an old-school perspective because....books?! But I find it helpful to have it around for coding languages -- one book to use as a kind of canonical source when I lose my way. :) Plus, it actually teaches Django, which is one heroic move for a beginner book -- pretty much unheard of (also, once you have done a Django app, you have already learned a ton about how to organize projects etc). Plus, unlike the other more popular books (automate the boring stuff) it goes into object-oriented programming. So, for true beginners I'd use a single fulcrum like a book, and use web sites with articles like realpython/freecodecamp/stackoverflow as a reference when needed.


Please excuse my ignorance, but what book are you referring to? I hate Reddit's reply tabs cause it's hard to know what thread level I'm on


Python crash course


As a fellow novice at this for about 2 years... I tend to check if real python has an article on a topic I'm trying to understand before checking the broader internet. Never done any of their courses or paid content however.


There's not a whole lot of article on this subject surprisingly. And it would also be ideal if every one follow the same convention. Most companies will have their own coding style btw so you need to be ready to adapt.


wouldn't be? anyways, I started to get the point about every company has it is own styling convention, yet I think it would be beneficial to learn something on my own before I hopefully get employed.


>I see a lot of people referencing real python. Is this like the investopedia of the python world? It's MUCH better, MUCH MUCH better. Even ignoring the ad-ridden experience of Investopedia, the latter generally provides only high level descriptions and hand wavy arguments. This is OK of you're only interested in am overview of the topic, but absolutely nowhere near enough of you want to get some real knowledge of, say, how to structure an MBS deal. For that you need specific examples to play with. RealPython gives you that kind of practice, real code that you can replicate and extend, or learn and apply from.


Thank you


Thank you "notParticularyAnony". Actually, I am not yet done with the links included in your link, that was very helpful.


A lot of the time there exists standards that everyone follows for a language. For example Python has Pep8. The one thing I will caveat is that when you go to a company they may have their own standards. My company literally has standards that differ between teams we innersource with, so it can very quite easily.


Is there ever a desire internally to resolve those differences between teams or do they exist for GOOD reason? I can see this being a bit of a pain when you need to cross domains for a brief time.


Oh yea it’s a huge pain. It’s literally architects being set in their ways usually. Lots of teams have a pretty set standard that is used throughout but then there will be a couple teams with “old fashioned” architects that like older patterns and don’t want them to change mostly


I think part of the time, it's just old code. I inherited a PowerShell script from a senior engineer, and I was tasked to add onto it to automate it. Basically their piece did a task with data manually exported from our third party HR software. They wanted me to pull data directly from the API and just automatically run his piece with that piece and schedule it in a Runbook. He had written everything in Pascal. We both had the same syntax style as far as structure, but I have a habit of using camelCase for general loose variables that aren't functions or params. It as a case where he was the only one writing PowerShell scripts for larger infra. So there never as a standard, and there really isn't a structure. But I did def try to do Pascal like him, but it didn't make sense to do Pascal everywhere.


I happily deliver your one hundredth upvote for this insightful comment.


I think the Hitchhiker's guide is a [good start](https://docs.python-guide.org/#writing-great-python-code) =)


Thank you for your suggetion, It really seems as a good start.


Because of this, https://www.python.org/dev/peps/pep-0008/ Every language and sub fields in that language have a design philosophy/standard. I never actually read pep8 in detail but my code became more like it over time as I looked at more peoples code and picked up design patterns from "good code".


Actually, It was a fun experience to find out some of my personal conventions are PEP in nature.


Check out Black, a Python format checker: https://pypi.org/project/black/


We use this as a [pre-commit](https://pre-commit.com/) hook. Helps to keep the code style standardized.


Licensing looks all the same because of [this](https://docs.github.com/en/communities/setting-up-your-project-for-healthy-contributions/adding-a-license-to-a-repository)


It sounds to me like you are already along the way with learning them as you have noticed how people organise code and noted this down mentally. You know how to write code, how to organise it, and now it is just a case of expanding this outwards to the file/module level. Licensing is just what license you provide the code under. Mainly this is some sort of free license if it is on github. Commenting is something you just have to learn to do, but comments should add information and not clutter the code. Don't add comments to things that don't need them!


> and how one can learn them? You're learning them by looking at how other people structure their projects.


I just follow the guides that pycharm gives me and I assume they’re correct.


Those are called conventions. Everything typically has a convention you can follow, but are not required to.




Others have pointed out using Black, which is great for code formatting and style; for project organization, check out one of the cookiecutters like [hypermodern python](https://cookiecutter-hypermodern-python.readthedocs.io/en/2021.4.15/) or [tyranosaurous](https://pypi.org/project/tyranosaurous/) or [PyScaffold](https://pyscaffold.org/en/stable/usage.html)


I love hypermodern python. The documentation itself is a solid explanation of how to do…. Everything


sure I will, thank you for taking the time to answer my question.


You should read (or watch) Beyond the Basic Stuff by Al Sweigart. He covers all of this stuff in depth. The books is free to read on his site and he has made a bunch of YouTube videos that go along with it.


Thank you I started now reading this book, I highly suggest it for bypassers, from the very beginning it looks like a promising document [https://inventwithpython.com/beyond/](https://inventwithpython.com/beyond/)


The conventions are to make it easier for *everyone* to read instead of just you. There are design ideas around some of them too. But the operative is to be consistent and readable.


these are called best practices my lil fella


Conventions, not rules. Just as you follow the random capitalisation convention and I follow the en_gb convention.


Remember with good commenting in the code you don't need documentation outside the code.


Comments and docs are for different things. Comments should explain why this (possibly unexpected) code was necessary rather than what it does (which should be evident from the code). Docs might include doctests, background, context, examples, gotchas and known issues for example.


Depends on the code, a small script to move files? Yeah some comments will do it. An API with lots of endpoint will need some documentation


Ofcourse. Wow. I didn't expect to get downvoted for saying that. Naturally you'll want to make Readme files. But for many projects you can make quite fine documenting in the code itself.


Eh, depends on the purpose. Your own small projects might be fine without extra documentation, but that philosophy doesn't scale at all to a commercial level, where you may be working on and with dependencies for internal and external teams, etc... You can tell what the picture on a puzzle will be by looking at the pieces, but having the picture on the box makes it much easier to solve.


Usually languages have a standard. PowerShell functions have approved verbs, and functions and params are Pascal. I ten to do camelCase for any general variable in Powershell. Then Pascal every where else. Python everything is lower case so it uses underscores. Postgres isalso all lowercase. Then there are just general preferences like VariableAlpha = 'poop' VariableCHarlie = pee' Which looks better / cleaner written like VariableAlpha = 'poop' VariableCharlie = 'pee'


You're free to do as you wish, but do note that PEP 8 specifically recommends against using multiple spaces to align variables like that ([link](https://www.python.org/dev/peps/pep-0008/#whitespace-in-expressions-and-statements)).


I was talking about multiple things. The last thing I mentioned was postgres, but you're honing in on the thing before that. In Python, it's a preference, not a rule. The biggest argument is - it can maybe trigger trivial merges. At the end of the day, it isn't a hard rule. If you read this book, it is suggested you line up related things. http://cc2e.com/ They suggest aligning the equal signs in a group of assignment statements to indicate that they're related. So; employee_name = "Andrew Nelson" employee_bdate = "1/1/56" employee_rank = "Senator" current_employee_record = 0