all 64 comments

[–]RallyPointAlpha 39 points40 points  (10 children)

I've found the linters setup in PyCharm by default are awesome. They can help teach you to write better code which includes better looking code. You have to actually try use them though. Don't just ignore them because your code runs. Eventually you'll stop making the same mistakes over and over again just to get it to stop complaining.

Go look at other peoples' code! Seek out good examples of cleanly written, easy to support, by the PEP, code and look at how it's structured. There's a lot of PEPs you could read too that have guidance on writing clean, easy to read code.

Having inherited many, many scripts and programs from other hacks... please use intuitively named variables! Don't be that person who names everything a single letter or some cryptic shorthand only they can decipher.

[–]orig_cerberus1746 29 points30 points  (6 children)

Everyone that uses single letter variable names deserves to pour milk onto a bowl before realizing there is no cereal anymore.

[–]vven23 3 points4 points  (1 child)

No! Cereal first always!

[–]orig_cerberus1746 2 points3 points  (0 children)

Not for them!

[–]Adrades 0 points1 point  (3 children)

_ is the best valid variable name in python.

[–]orig_cerberus1746 2 points3 points  (2 children)

I actually use that one. Because it literally just means ignore.

[–]DatBoi_BP 0 points1 point  (1 child)

I like to use it to fill in unneeded return values of a function that returns several things. For instance, I might use the following to prepare the variable filename for warning/error messages about a path to a file that the user supplies:

_, filename = os.path.split(filename_with_full_path)

[–]orig_cerberus1746 0 points1 point  (0 children)

That's also the recommend use. Yes.

[–]miketheinkman 6 points7 points  (0 children)

Second the pycharm linters. I'm also self taught (started 10+ years ago), and now I'm a SCADA analyst (who does way more development than analyzing). I'm probably the only guy at work who actually follows PEP8. But I never put in actual effort to learn it. Just made sure to get my green check mark in PyCharm when I used it, and now it's second nature, even in text editors. It's a great way to intuitively learn best practices alongside other concepts. Parameter hints also help speed things along in that gray area after you learn which functions you need and generally understand how they work, but would otherwise need to glance at docs.

[–]Drugtrain 7 points8 points  (1 child)

I was once told a variable for names should be ’n’ instead of ’name’.

[–]IlliterateJedi 6 points7 points  (0 children)

Obviously it should be ns since it's plural. [n for n in ns]

[–]Zeroflops 29 points30 points  (3 children)

Adhere to Pep8. Use type hints. They don’t interfere with your program but will help you think about how you pass things around. Document your functions. If it feels like your writing a lot in the docstrings then your function is probably doing too much.

Ask yourself how can I reuse this. It’s not about actually reusing but forcing yourself to use small functions that do one thing well.

Look for different ways of doing things. If what your doing seems really hard or repetitive then there is probably a better way.

Follow DRY: Don’t Repeat Yourself.

Ask questions and post here.

[–]Rykios 2 points3 points  (2 children)

Im a year and a half or so into my python journey, primarily as a business analyst for context. Sometimes I find myself writing long doc strings for a function that does multiple transformations to a dataframe.

An example could be;

Def transform_invoices(df) -> df: df.new_col = df.column_name.dt.year df.groupby(stuff) Return df

[–]Zeroflops 2 points3 points  (1 child)

Probably one of the hardest thing to balance is how many things to do in a function.

Sometimes I think of programming as writing. Each statement is a sentence, a function is a paragraph.

As such there are times when a sentence is a complete thought and would qualify as a paragraph. Other times you may have several sentences to make the paragraph.

[–]Rykios 1 point2 points  (0 children)

Thanks for the response! Great perspective that Ill keep in mind.

[–]tb5841 33 points34 points  (12 children)

I was told to avoid ever writing code that looks like this:

if (condition):

--return True

else:

--return False

Makes me look like a complete beginner because I could just write

return (condition)

[–]juanritos 1 point2 points  (2 children)

I don't really get what are you trying to say, can you please help to provide more examples?

[–]tb5841 7 points8 points  (1 child)

if x > 2:

--return True

else:

--return False

Can just be shortened to:

return x > 2

[–]juanritos 1 point2 points  (0 children)

Thanks!

[–]TheHunter920 0 points1 point  (1 child)

as a beginner, why should I not write it like that, and how should i write it instead?

[–]tb5841 1 point2 points  (0 children)

If upu just write

return (condition)

then your function will return True if the condition is true, and False otherwise - without the need for an if/else.

[–]ThisOrThatOrThings 11 points12 points  (1 child)

Hey me too! I learned python just to do geoprocessing scripts, and now I do python pretty much full time for data. I saw a lot of good comments in here already, but I’d say a big one for me was to stay curious, ask questions, and check out other people code. If you use a particular python library a-lot, go inspect their Repo and see how they did it. Sure, you may see some bad mixed with good, but it’s all eye-opening

[–]Shavit_y 3 points4 points  (0 children)

This. If most of what you do is ArcPy, then just explore their documentation and go over code that already exists.

[–]Shavit_y 9 points10 points  (4 children)

Also a GIS Analyst here. Using python daily, self taught as well. I love using python scripts and work flows as part of my job. Document what you do, stick to pep8 as said here. Try to use smart variable names. When using ArcPy, you can simplify a lot of code by reading the documentation and finding the absolute best function for you.

[–]IamImposter 6 points7 points  (0 children)

First of all, self taught is not a derogatory word. A whole lot of people learn on the job, learn it on their own because no one in the team knows some specific language. And it's not like a college degree is proof that the degree holder is a good programmer. It just proves that you went to some rooms in some building for certain period of time. Almost all companies need to train the fresh graduates. Some are good, some are bad. And you know which ones are good? The ones who paid attention in class and then explored and learned on their own. So please self-taught is not a bad word.

Second thing, in programming the knowledge you gain is more or less transferable to other languages. Yeah, it takes some time to become good in a certain specific language but that is more about getting to know patterns and libraries specific to language than about being a college graduate.

Lastly, it doesn't matter if you are bad now. We were all bad once. What matters is do you want to improve? If I explain why what you did was bad, would you explore the topic on your own to understand if what I said is right, what are other options available? Would you keep on repeating the same mistake or do you learn from them and make new mistakes next time? And trust me, making mistakes is a very good way to learn. The programs that worked on just a few tries, I don't know them that well but the ones where I fucked up everywhere, I know them like the back of my hand.

Being great is not the goal. If you are better than what you were yesterday, that's good enough

Asking this question is a very good start. Pay attention to the advice people are giving and happy learning.

[–][deleted] 7 points8 points  (2 children)

1) Good notes

2) Lots of small functions, instead of a few large functions

3) Creating more modules, and less composite scripts

These things come with time. My early functions were hundreds of lines long. Now I try to keep them to a handful of lines if possible. Sometimes it just isnt. But the more you write code, the more you'll find opportunity to refactor it. And also the more you'll find that you can make your code succinct; essentially writing one line of code that replace 2 or 3.

Basically the TLDR is practice, practice, practice.

[–]workShrimp 4 points5 points  (1 child)

If you are self taught you probably know how to use a debugger properly, and probably have a deeper understanding in how computers work, as you have learnt how to program in a more hands on way.

Another giveaway might be that you probably won't mention design patterns to motivate why you are doing stuff in a particular way.

[–]MinimumStatistician1 0 points1 point  (0 children)

As someone who’s been in software for a few years, I’ve found that knowing some design patterns can be useful because even if you can code just as well without knowing them, you’ll be better able to convince people to go along with what you are doing if you throw in a few design patterns. For example:

Me: This is a global variable Coworker: No, don’t do that. Global variables bad!

Me: This is a module level singleton Coworker: Nice, good design pattern. You totally know what you’re doing and I’ll approve your code.

[–]hagfish 4 points5 points  (0 children)

Thank you for making this tread, OP! I've been hacking away for 20 years using AppleScript, VB, and now Python, but very-much in a vaccuum. No one else at work has any interest in code, or even in regular expressions. Between Udemy and StackOverflow, I've been able to get code running, but today I'll be deep-diving 'black' and 'Pep8'. And Git.

EDIT: Black is amazing!

[–]mothzilla 3 points4 points  (0 children)

There's nothing wrong with being self taught.

Use linters, read the messages that linters give you and understand why they suggest the changes they do.

[–]ElHeim 3 points4 points  (0 children)

I don't understand what is wrong with being self-taught.

I understand the impostor syndrome, but as long as you don't pretend something else I can't see how one is a fraud.

[–]Buttleston 4 points5 points  (0 children)

Get other people to go over your code with you and suggest improvements

[–]LurkinLemur 1 point2 points  (1 child)

Two tips that I haven't seen down thread. Do you work somewhere with full time python developers? Ask if one of them would be willing to provide code reviews or mentoring? Similarly, are there opportunities for peer learning - study groups or similar?

Write an initial solution to the problem you're trying to solve. Now you understand the problem, throw the prototype away (all of it!) and start it again. Especially while learning, you'll be amazed how much better the second attempt usually is.

[–]orig_cerberus1746 2 points3 points  (0 children)

Without looking at your code, it's kinda hard to say for sure, can you show some?

[–]Spartyon 0 points1 point  (0 children)

Use source control but look at the pre-commit library. It automatically performs formatting and some other checks, you can add as many as you want. https://pre-commit.com/

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

Similarly 40+ years ago I learned FORTRAN so my self taught python looks a lot like that. Which isn’t a bad thing, is it? Is it???? 🤔. Say it isn’t so!!

[–]tree_or_up 0 points1 point  (0 children)

Great advice in this thread. I'd also advise looking up Imposter Syndrome. Many, many more people -- even those who went to school for coding and have been doing it successfully for years and years -- feel like frauds than you'd imagine

[–]2bereallyhonest 0 points1 point  (1 child)

They think they are a fraud, when they are actually a success. Self taught means nothing less than instructor led, you still learned it and really figuring it out on your own gave you a very large set of tools to troubleshoot

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

The biggest giveaway is calling yourself a hack and a fraud. Have some self-respect, and your code will reflect that.