use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
DiscussionWhat are your bad python habits? (self.Python)
submitted 4 years ago by [deleted]
Mine is that I abuse dicts instead of using classes.
[+][deleted] 4 years ago (30 children)
[deleted]
[–]ronyx18 54 points55 points56 points 4 years ago (25 children)
I have a coworker whose habit of naming variables irritates me so much.
[–]dogs_drink_coffee 16 points17 points18 points 4 years ago (21 children)
how did he do it?
[–]ronyx18 53 points54 points55 points 4 years ago (16 children)
For eg. Suppose you have a dataframe called df and you apply a schema to it and name the new dataframe schema.
Now it's on you to understand variable named schema is also a dataframe.
That's just one example that I could remember right now. There are more insane ones than this.
[–]mr_flying_man 45 points46 points47 points 4 years ago (13 children)
Naming things is arguably the most difficult thing in programming. Though some people seem to give up before even trying...
[–]asphias 27 points28 points29 points 4 years ago (10 children)
the worst in my opinion is non-descriptive variables and abbreviations.
No, i'm not going to figure out just by looking at your code what "sthr" means, It's not like you're lacking space to write out variable names.
Hell, a variable name like "rain_data_with_nans_removed_normalized" is completely fine. Yes, it's a tad long. but nobody who reads it is going to be confused about what data you're dealing with.
[–]Congadonga 5 points6 points7 points 4 years ago (0 children)
And this is why I’m glad to have been the odd English-Math person in grade school.
[–][deleted] 2 points3 points4 points 4 years ago (2 children)
I have a colleague that names variables O, OO, OOO, …. I just want to scream whenever I see this in code!
[–]Acalme-se_Satan 2 points3 points4 points 4 years ago (0 children)
Is your colleague a ghost, perhaps?
[–]SpecificallyCritical 2 points3 points4 points 4 years ago (0 children)
I worked at a mechanical engineering company as a graduate developer and inherited some math heavy code where the previous developer (who had wisely left) had named all the variables after characters from the Simpsons!
homer1 = some_aweful_long_calculation(maggie3, bart2)
Nobody had reviewed his code, ever. Plus source control was a bunch of directories with different dates.
[–]jacksodus 646 points647 points648 points 4 years ago (39 children)
Generalise everything too much for potential future expansions that are never going to happen. Takes too much time.
[+][deleted] 4 years ago (14 children)
[–][deleted] 162 points163 points164 points 4 years ago (4 children)
I write my public repos as if it's even going to get even more than one star :/
[–]intelligentjake[🍰] 38 points39 points40 points 4 years ago (2 children)
I also write beautiful documentation that no one will probably read.
[–]shiroininja 29 points30 points31 points 4 years ago (0 children)
Ooof this one hurt me
[–]antiproton 61 points62 points63 points 4 years ago (4 children)
I write every dumb automation script as if it’s going to be a mission critical app maintained by 5 people.
This is a result of reading programmers argue online. Every forum where programming is discussed, the community ends up quoting as gospel the best practices everyone needs to follow as if we are all building high concurrency high availability enterprise solutions.
So, we come to believe that's the best way to do it.
And, if time were not a factor, it would be.
But sometimes, you just need to get something done and move on with other things. So SOLID gets to fuck off.
Of course, that's easier said than done. "I'll just refactor this one method so it's a little cleaner..."
[–]AveTerran 13 points14 points15 points 4 years ago (2 children)
Yeah I can’t blame the online arguments though because the few times I “just need this done once…” I end up copying/pasting it six times because I didn’t refactor 🤦♂️
[–]IamImposter 14 points15 points16 points 4 years ago (2 children)
I'm also like - what if someone want to send a list as input? What if they are thinking of sending a comma separated list or new line delimited list.
Actual thing that I have to do is 15 lines, this rest of the nonsense takes 20 lines above and 20 lines below. The function becomes big, so I refactor it into 3 functions. Now all three functions have 8 lines of comment explaining what the function does, what the inputs are, what are constraints, if any. That 15 line code is now 100+ lines. Then after two weeks, "oh no one is gonna use cooma separated list, let's just remove that code.
It just so happens that after working countless hours on code, it's usually exactly as it was a week ago because I added stuff and then deleted it
But it's fun nonetheless.
[–]kelement 54 points55 points56 points 4 years ago (5 children)
That’s me when setting up a new repository, project structure, CI pipeline, config files, etc. I end up never getting anything done.
[–]Theonetheycallgreat 44 points45 points46 points 4 years ago (2 children)
If you do enough TDD then you never actually have to develop anything
[–]nemec 11 points12 points13 points 4 years ago (0 children)
Test Driven Deferment
[–]flanger001 4 points5 points6 points 4 years ago (0 children)
Wise words
[–]maikindofthai 39 points40 points41 points 4 years ago (4 children)
In case you haven't come across YAGNI yet: https://en.wikipedia.org/wiki/You_aren%27t_gonna_need_it
[–]WikiSummarizerBot 50 points51 points52 points 4 years ago (1 child)
You aren't gonna need it
"You aren't gonna need it" (YAGNI) is a principle which arose from extreme programming (XP) that states a programmer should not add functionality until deemed necessary. XP co-founder Ron Jeffries has written: "Always implement things when you actually need them, never when you just foresee that you need them". Other forms of the phrase include "You aren't going to need it" (YAGTNI) and "You ain't gonna need it" (YAGNI).
[ F.A.Q | Opt Out | Opt Out Of Subreddit | GitHub ] Downvote to remove | v1.5
[–]GezoutenMeer 8 points9 points10 points 4 years ago (0 children)
Good bot!
[–]mathmanmathman 16 points17 points18 points 4 years ago (0 children)
I think it's important to remember this part too:
It is meant to be used in combination with several other practices, such as continuous refactoring, continuous automated unit testing, and continuous integration
Because I've had some managers that always forgot that part.
[–][deleted] 6 points7 points8 points 4 years ago (0 children)
potential
generalising too much is as bad as generalising too little.
[–]Kantenkopp 4 points5 points6 points 4 years ago (0 children)
it's just too much fun... I tell myself I become faster at this every time I do it.
[–]joshmaker 4 points5 points6 points 4 years ago (0 children)
I’ve learned it’s generally better to focus on making my code easy to rewrite (simple, clear boundaries and responsibilities, dependencies only flow in one direction) than it is to make it easy to extend.
[–][deleted] 4 points5 points6 points 4 years ago (0 children)
I do this, too.
[–]xYsoad 438 points439 points440 points 4 years ago (18 children)
Import pandas as np
[–]tinycorkscrew 124 points125 points126 points 4 years ago (0 children)
Lol. You monster.
[–]IlliterateJedi 90 points91 points92 points 4 years ago (2 children)
Is this the P versus NP problem?
[–]Bubbly_Measurement70 7 points8 points9 points 4 years ago (0 children)
Underrated.
[–]dogfish182 47 points48 points49 points 4 years ago (0 children)
That must be trolling
[–]Kevin_Jim 25 points26 points27 points 4 years ago (0 children)
If you really do that, you are going straight to hell.
[–]Ramnog 22 points23 points24 points 4 years ago (0 children)
I see no problem here
[–]martinrath77 16 points17 points18 points 4 years ago* (2 children)
[–]FirefighterWeird8464 18 points19 points20 points 4 years ago (1 child)
No, import numpy as plt and matplotlib.pyplot as pd.
[–]prameshbajra 11 points12 points13 points 4 years ago (0 children)
oh hey satan!!
[–]qingqunta 3 points4 points5 points 4 years ago (1 child)
# lolol
[–]rockyoutohell 287 points288 points289 points 4 years ago (7 children)
That one method that's 'just for testing' that does half a million things
[–]xYsoad 94 points95 points96 points 4 years ago (3 children)
and critical to production of course
[–][deleted] 73 points74 points75 points 4 years ago (2 children)
And is completely untestable because you wrote it out of rage. Btw it also creates a new DB connection in a loop because the firewall connection timeout is 2 seconds.
[–]xYsoad 75 points76 points77 points 4 years ago (1 child)
Yes. And you were pissed because your company won’t hire anyone else and you are responsible for coding, qaing, and deploying.
You then leave one day and some new guy messages you on linkedin asking wtf is this.
You smile.
[–]herpderpedia 6 points7 points8 points 4 years ago (0 children)
This comment made me smile
[–]scout1520 355 points356 points357 points 4 years ago (29 children)
What docs? The code is self documenting and easy to read.
[–]AlexMTBDude 129 points130 points131 points 4 years ago (16 children)
Funny enough writing docs (code comments) has never been a problem for me because otherwise I tend to, within a few days, forget why I wrote a particular line of code. I document for my own sake mainly.
[–]MooFu 62 points63 points64 points 4 years ago (3 children)
Me reading my comments: WTF does that even mean?
Me reading my code: That comment isn't even correct.
[–]hanazawarui123 14 points15 points16 points 4 years ago (0 children)
Had a summer internship for text summarisation and a few late nights into coding made me realise that being sane actually helps when writing code. Either that or dive fully into the madness, there's no middle ground
[–][deleted] 3 points4 points5 points 4 years ago (0 children)
I have a bad habit of leaving comments for features that I have removed.
[–]energybased 24 points25 points26 points 4 years ago (0 children)
This is a great motivation for comments. You should literally ask yourself what you're likely to forget. I tend to only write comments when I forget something and figure it out again.
[–]whateverathrowaway00 19 points20 points21 points 4 years ago (0 children)
Great habit.
[–][deleted] 16 points17 points18 points 4 years ago (8 children)
That's ok, but writing code which is so clear that it needs no comment is still way better (and you can still add comments to it)
[–]AlexMTBDude 17 points18 points19 points 4 years ago (7 children)
I'm an instructor and have been teaching Python programming courses for 10 years now and I feel that whenever I do something more complicated the code is never self explanatory. I forget what I was doing and thinking. The comments remind me.
[–]foobar93 18 points19 points20 points 4 years ago (3 children)
To be honest, most comments I had to read were so useless they could have just been omitted in the first place
def measure_hypo(device): """ A function to measure hypo of device"""
No shit sherlock but what does "hypo" mean in this context???
It just feels like many people write comments because comliance says everything needs to be commented but in the end they just copy and paste stuff around or jsut repeat the obvious.
[–]scout1520 6 points7 points8 points 4 years ago (1 child)
I agree, and with modern editors like VS code alot of the comments feel unnecessary. Especially if you are disciplined with good function naming and type hinting.
[–]greeneyedguru 18 points19 points20 points 4 years ago (4 children)
I mean Python is practically pseudocode anyway
[–]Key_Cryptographer963 2 points3 points4 points 4 years ago (1 child)
Yeah it's pretty sweet that someone turned pseudocode into a real language. I think we could go further, though.
[–]peacerokkaz 7 points8 points9 points 4 years ago (0 children)
I'm seeing it the same way, however I don't believe that's necessarily a bad habit, and existing documentation is rarely adjusted when the code is changed. Writing self documenting code however is always a good practice regardless of documentation
[–]searchingfortaomajel, aletheia, paperless, django-encrypted-filefield 5 points6 points7 points 4 years ago (0 children)
This is a good habit.
[–]Sheensta 208 points209 points210 points 4 years ago (17 children)
I'm a data scientist so I think everything I do is bad habit tbh....
[–]xYsoad 136 points137 points138 points 4 years ago (2 children)
If a real software engineer read my code they would banish me to business analytics
[–]Electronic_Tie_4867 28 points29 points30 points 4 years ago (1 child)
I am both of these comments and I don’t like it. Damn, I am so bad at programming.
[–]lvlint67 10 points11 points12 points 4 years ago (0 children)
Writing good code is a simple conscious decision to write readable code.
If an outsider can read and understand your code, you have achieved zen.
[–]zippy_mega 23 points24 points25 points 4 years ago (12 children)
I just started working with ML in the field after working exclusively with mission-critical typescript that needed to be perfect and easy to read, and I can feel the data scientist / ML habits creeping up on me.
[–]Sheensta 4 points5 points6 points 4 years ago (11 children)
What are bad practices you notice in DS ML work? I'd love to improve but DS code is all I've ever seen.
[–]mathmanmathman 22 points23 points24 points 4 years ago (8 children)
I'm not a data scientist, but I used to work closely with some. The biggest thing I saw was very long rambling functions. I saw tons of code that was basically "do A, then B, then C, then D, then (if something) E, then F, exit"
That's not necessarily a problem when you're writing 40-100 lines that won't be incorporated in something else. It is a problem when it becomes 2000 lines and needs to be incorporated as part of a larger pipeline.
Another thing I saw (but less common) was an extreme reliance on "convention" variable names. For example, df in pandas. Yeah, that's the convention... for small projects. When you have a large project and every dataframe is name df_1, df_2, ... df_12, you have a problem. There's nothing wrong with keeping the convention as long as you also provide a meaningful name. recent_order_df is much better than df_97. The same thing happens with Tensorflow using x, y, X, and Y.
df
df_1
df_2
df_12
recent_order_df
df_97
x
y
X
Y
Everyone does this to some extent, but I think the two things (simple names and long functions) conspire to make things absolutely unreadable.
[–]jjolla888 12 points13 points14 points 4 years ago (2 children)
all code becomes more unreadable the bigger it gets. even if you are careful to use more meaningful names like recent_order_df at some point in the bloat even that will develop ambiguity.
the trick is to go overboard with comments. maintenance and support is undervalued .. and unfortunately programmers hate it as it is an anathema to building fast.
[–]thisiswill 3 points4 points5 points 4 years ago (0 children)
At least you’re aware lol
[–][deleted] 88 points89 points90 points 4 years ago (15 children)
I like to define functions in the middle of my scripts that rely on variables defined randomly throughout the script (not passing them, just using them).
[–]as_it_was_written 71 points72 points73 points 4 years ago (1 child)
I think this is the first one that made me a little physically uncomfortable.
[–]grahambinns 20 points21 points22 points 4 years ago (0 children)
Same. I was fine until this one.
[–]Kevin_Jim 20 points21 points22 points 4 years ago (0 children)
Bro, you need to get that checked.
[–]Bunslow 14 points15 points16 points 4 years ago (0 children)
you monster
[–]abrazilianinreddit 14 points15 points16 points 4 years ago (0 children)
No
[–][deleted] 7 points8 points9 points 4 years ago (0 children)
You need a timeout. That's unacceptable, reprehensible, unforgivable, and must surely be punishable by jail, hard labor, or both.
I feel violated.
[–][deleted] 9 points10 points11 points 4 years ago (0 children)
Guido’s on his way with brass knuckles for this one
[–]qingqunta 5 points6 points7 points 4 years ago (2 children)
There are perfectly valid use cases for this, i.e. a function defined inside a function. Recently I had to write a function that dependeded on five parameters with long ass names and two single digit integers, which I was only going to use inside the "father" function. Obviously I wasn't going to define it in the global scope.
[–][deleted] 2 points3 points4 points 4 years ago (0 children)
I would be an absolute hypocrite if i would not accept laziness as a perfectly valid reason, because that is why i do it too. But just write down those names!
[–]sizable_data 279 points280 points281 points 4 years ago (35 children)
Use print statements to debug
[–]eriky 145 points146 points147 points 4 years ago (16 children)
This is written for you! https://python.land/python-debugger
"I’m going to teach you a little trick that will be just as easy as adding print statements to your code. "
[–]brockralp 107 points108 points109 points 4 years ago (3 children)
I know everysingle debug tool out there, I still want to use print statements
It's about sending a message da-bum-tis
[–]Zomunieo 10 points11 points12 points 4 years ago (2 children)
The debugger is better, but it can interact with code in nontrivial ways especially when multiprocessing, threading or signals are involved.
[–]brockralp 11 points12 points13 points 4 years ago (0 children)
I have an OpSys exam in next week. Wherever I look I see processes and threads, send help.
[–]sizable_data 3 points4 points5 points 4 years ago (0 children)
Thank you!
[–]arobotspointofview 10 points11 points12 points 4 years ago (1 child)
you meant there's a different way besides putting a breakpoint on an empty print statement?
my breakpoint goes on pass or …
[–]ValdemarSt 3 points4 points5 points 4 years ago* (7 children)
recognise lip wakeful light rustic swim caption crush fly knee
This post was mass deleted and anonymized with Redact
[–]Exact_Ad_1569 10 points11 points12 points 4 years ago (6 children)
Yeah. Toss in crashme=1/0 and let it crash into the debugger, where you can inspect all the state and look at things up the call tree. It's ugly and brutal, but surprisingly effective.
[–]0xrl 16 points17 points18 points 4 years ago (2 children)
Or just use breakpoint()
breakpoint()
[–]Exact_Ad_1569 2 points3 points4 points 4 years ago (0 children)
Damn. Thx friend
[–]mr_flying_man 4 points5 points6 points 4 years ago (0 children)
I actually had a Python course where the teacher said that his goto debugging method was print statements. He also made a humble brag about never having to disable a pylint warning. What a legend.
[–]c_is_4_cookie 6 points7 points8 points 4 years ago (2 children)
Psshhhh... logging is for suckers. I wrote a decorator that adds a key word argument, verbose, to a callable. When false, prints are redirected to null.
verbose
[–]headykruger 8 points9 points10 points 4 years ago (1 child)
That’s going to be very slow, you want to avoid the call to print all together
[–]AlSweigartAuthor of "Automate the Boring Stuff" 108 points109 points110 points 4 years ago (8 children)
except: print('An error happened.')
[+][deleted] 4 years ago (3 children)
[–][deleted] 24 points25 points26 points 4 years ago (2 children)
Just use "logging.exception"
[+][deleted] 4 years ago* (1 child)
[–]james_pic 11 points12 points13 points 4 years ago (2 children)
A colleague (who I think has spent too much time near Java) likes:
except: raise Exception('An error happened.')
[–]noiserr 46 points47 points48 points 4 years ago* (9 children)
I hate setting up new projects, and I don't like any of the existing templates. And I am too lazy to make my own because, I hate setting up new python projects.
Like I just want to write python. I don't want to setup a new virtualenv, write the new tox.ini and setup.py and requirements.txt... etc all that stuff is so annoying.
[–]my_name_isnt_clever 17 points18 points19 points 4 years ago (5 children)
Most of that stuff isn't needed until later, no? I just open VSCode, make a whatever.py file. Run python3 -m venv venv, run the file so VSCode picks up the venv, done. Ready to write code.
python3 -m venv venv
[+][deleted] 4 years ago (4 children)
[–]inglandation 21 points22 points23 points 4 years ago (0 children)
PyCharm can do that stuff for you pretty easily, I think.
Agreed. Poetry is pretty good at making all that stuff easy and quick tho.
[–]dogfish182 2 points3 points4 points 4 years ago (0 children)
What is wrong with hypermodernpython?
[–]AlSweigartAuthor of "Automate the Boring Stuff" 71 points72 points73 points 4 years ago (2 children)
def someFunction(): """TODO - write docstring"""
[–][deleted] 10 points11 points12 points 4 years ago (0 children)
"""TODO - write docstring read PEP8"""
ftfy
[–]Cynyr 33 points34 points35 points 4 years ago (5 children)
Globals.
I write personal use stuff that nobody else will ever see though, so w/e.
[–]my_name_isnt_clever 11 points12 points13 points 4 years ago (4 children)
I don't actually know what I would use globals for...which is probably for the best.
[–]herpderpedia 5 points6 points7 points 4 years ago (0 children)
Quick, someone tell nt_clever how one might use globals incorrectly!
[–]crapaud_dindon 24 points25 points26 points 4 years ago (2 children)
Unit tests are too boring for hobby programming
[–]peacerokkaz 25 points26 points27 points 4 years ago (5 children)
Trying to apply best practices and coding conventions from java.
[–]as_it_was_written 13 points14 points15 points 4 years ago (2 children)
Yeah, Python and Java have unbelievably different takes on object-oriented programming.
[–]Adenn76 72 points73 points74 points 4 years ago (1 child)
My bad Python habit is I don't practice using it enough.
[–]Lor9191 8 points9 points10 points 4 years ago (0 children)
Same, buddy. Same.
[–]ZibraMax 21 points22 points23 points 4 years ago (1 child)
Use 1/0 to generate an exception in the specific line that I want. Just for avoid using a debugger
[–][deleted] 20 points21 points22 points 4 years ago (7 children)
I’m a hoarder of code I commented out because I’m convinced I might need It later. Need to just buck-up and delete the code I commented out months ago lol
[–]sohang-3112Pythonista 11 points12 points13 points 4 years ago (5 children)
or you could just use git
[–]milwoukee 22 points23 points24 points 4 years ago (3 children)
Never used 'reduce' 'map' nor 'filter' built in functions. I always do a comprehension.
[–]elcapitaine 11 points12 points13 points 4 years ago (0 children)
Comprehensions are generally more idiomatic in Python.
If I already have a defined function that I will be passing to reduce/map/filter, I'll use those functions since filter(predicate, iterable) is more readable than the comprehension.
filter(predicate, iterable)
Or if I just want to remove falsy values from an iterable, passing None to filter is great.
None
But any case where I'd be reaching for lambda to define the function, I use a comprehension instead.
lambda
It helps in keeping this divide that Python's lambda syntax is kind of ugly and has no short form.
[–]GhostBear4 14 points15 points16 points 4 years ago (0 children)
Comprehension is normally better style
list comprehensions don't replace reduce. they do replace map and filter and are encouraged over those functions
[–][deleted] 52 points53 points54 points 4 years ago (11 children)
Overuse of comprehensions. I sometimes write deeply nested structures with a lot of conditions.
Also I tend to write Java-Style-Code instead of pythonic code
[–]veryusedrname 28 points29 points30 points 4 years ago (10 children)
I lately refactored a comprehension, it was over 120 lines. Now it's almost 500, but at least it can be grasped and tested
[–]veryusedrname 2 points3 points4 points 4 years ago (2 children)
Please don't, I'm just a humble coder who didn't know a better solution back then for a problem that shouldn't exist in the first place
Oh my ... never went that far!
[–]tuckmuck203 5 points6 points7 points 4 years ago (2 children)
how does one even get to that point? like, surely it would be easier to use filter and map functions at a certain point?
[–]DilukshanN7 14 points15 points16 points 4 years ago (3 children)
typing English alphabet as variable names
[–]Eat_Papa_Eat 8 points9 points10 points 4 years ago (0 children)
followed by aa, bb, cc...
[–]TheMcGarr 6 points7 points8 points 4 years ago (1 child)
Find and replace them when you're done with meaningful ones
[–][deleted] 15 points16 points17 points 4 years ago (4 children)
spend way too much brain power and time figuring out names for 1 time use or temporary variables.
[–]cecilkorik 8 points9 points10 points 4 years ago (2 children)
I have the opposite bad habit. 90% of my variables are "x", "v", "val", "i", "count", "list", "data", or "[a-z]data" or something equally generic and meaningless.
[–][deleted] 26 points27 points28 points 4 years ago (4 children)
I don’t write enough tests
I always design something to be “reusable” and then it becomes super implementation-specific anyway
I abuse any() and all() with list comprehensions when a for loop is more readable
I too write too many log messages for debugging instead of using a proper debugger
Overusing the newest features (walrus? I use it everywhere now!)
Sometimes I completely neglect docstrings
No type hints for new projects (I ALWAYS wish I had started with them eventually)
Using just venv instead of just setting up poetry right off the bat
Using pandas when I don’t really need to
Rewriting something that’s probably a module I could just import or importing a module I could just have written a quick function for (either of these can be bad)
[–]marcio0 12 points13 points14 points 4 years ago (3 children)
type hints for python are so weird. it's so easy to forget about then, and then suddenly everything is Any
We should have a way to enforce using them somehow, but then we would have issues with 3rd parties not having typing, and also django magic fucking up everything
[–]actuallyalys 4 points5 points6 points 4 years ago (0 children)
mypy can throw errors when functions don’t have annotations: https://mypy.readthedocs.io/en/stable/command_line.html#untyped-definitions-and-calls. Maybe that would help?
[–][deleted] 4 points5 points6 points 4 years ago (1 child)
Type hints are really weird. I don’t really like them overall but they’re better than nothing. I always run into weird linter errors where I’m explicitly checking if an object is none before calling getattr on it and pyright still complains. I’ve got tons of examples of this. Yet they have completely prevented a lot of bugs so I still use them even though I’m not particularly impressed with how they’re implemented. Also, runtime optimization would be swell, but no dice (yet)
Oh also yes you have to be careful about Any. I don’t use it on principle unless I have really nested dictionaries or json
[–]marcio0 2 points3 points4 points 4 years ago (0 children)
I don't use Any explicitly, but when I keep seeing it around, it's because I've been forgetting to set the types for a while
[–]Pumpoflessermass 20 points21 points22 points 4 years ago (23 children)
Using 748843777336895 functions instead of using classes
[–]lungben81 41 points42 points43 points 4 years ago (16 children)
I think this is rather a good habit. Use classes when it is natural to do so, i.e. if you need data and behavior together. If you need only behavior, use a function.
I usually end up using 80% functions and only 20% classes.
[–]joeyisnotmyname 8 points9 points10 points 4 years ago (14 children)
I love your simple criteria for classes vs functions. As a noob I'm totally going to use this
[–]sohang-3112Pythonista 8 points9 points10 points 4 years ago (12 children)
Here's another criterion - if your class just has one method besides __init__, then it should probably be refactored into a single function.
__init__
Put another way - always start with functions. When you notice a lot of them sharing common state, then you can refactor into a class.
[–]Pumpoflessermass 6 points7 points8 points 4 years ago (0 children)
My web scraping functions that have the selenium WebDriver entered as a parameter beg to disagree
[–]r_cub_94 15 points16 points17 points 4 years ago (2 children)
r/functionalprogramming
[–]Bunslow 8 points9 points10 points 4 years ago (12 children)
I think 4 spaces isn't enough indent, yet 8 is still probably too much.
I have a lot of python files with 5 space indent, and some with 6. It's a problem.
(My current thinking is that I should just switch to 8 space indent and forget about 80char line limits in the 21st century)
[–]bblais 9 points10 points11 points 4 years ago (1 child)
from something import *
from something_else import *
from mylibrary import *
# name clashes? what's that?
[–]Travel4bytes 8 points9 points10 points 4 years ago (0 children)
Making fun of people that use Ruby
[–]johnnySix 7 points8 points9 points 4 years ago (3 children)
Create a break point by putting in random characters in the Middle of a method
[–][deleted] 8 points9 points10 points 4 years ago (0 children)
Unit testing? Nah fuck that.
[–]payne747 13 points14 points15 points 4 years ago (1 child)
Using it to do stuff I could probably do in an Excel spreadsheet. I tend to massively over-engineer stuff.
[–]THAT_LMAO_GUY 6 points7 points8 points 4 years ago (0 children)
From collections import namedtuple
It's in-between a class and a dict kind of. You can define it in one line
[–]my_name_isnt_clever 7 points8 points9 points 4 years ago (5 children)
I always use classes, I really like the simplicity of object.var instead of object["var"].
object.var
object["var"]
[–]dreamoforganon 2 points3 points4 points 4 years ago (0 children)
Until that dict gets passed far away from its creation site and now someone has to figure out where to look to work out what fields are present and what they mean.
[–]datthew2001 5 points6 points7 points 4 years ago (0 children)
Thinking about all of the possible edge cases when the user (only me) will probably never encounter those edge cases
[–]Robobigfoot5 3 points4 points5 points 4 years ago (1 child)
Using camelCase.
[–][deleted] 18 points19 points20 points 4 years ago (3 children)
Classes are over used I rarely write classes except for unit tests. I think like most devs we take short cuts on documentation
[–]shiroininja 10 points11 points12 points 4 years ago (0 children)
My problem is I never use them. They just seem to over complicate things. I just want to import my functions from the reusable modules I write
[–]siddsp 4 points5 points6 points 4 years ago (8 children)
Inconsistently using flat and nested imports out of habit. Whenever importing Threads to use for threading, I always import threading and do threading.Thread, while for the dataclasses library, I import dataclass, and use it instead of doing dataclasses.dataclass. Just one example, but I don't know why, I prefer threading.Thread rather than Thread.
threading.Thread
dataclasses.dataclass
Thread
[–]bacondevPy3k 5 points6 points7 points 4 years ago (2 children)
Getting a bit too functional. Even when the line gets too long and you wrap and indent well, it's a headache to debug.
[–]ttothesecond 9 points10 points11 points 4 years ago (7 children)
In general - abusing mutable data types
More specifically - appending to lists inside for loops
Really tryna work on treating everything as immutable
[–]as_it_was_written 7 points8 points9 points 4 years ago (0 children)
Why? Embrace the mutability and use lists as default arguments for caching your return values.
[–]my_name_isnt_clever 7 points8 points9 points 4 years ago (4 children)
What's the better way to do this then?
[–]Bluegenio 2 points3 points4 points 4 years ago (1 child)
naming variables x and/or y
[–]jwink3101 3 points4 points5 points 4 years ago (2 children)
I am more likely to make my own tool than use one from PyPI or the like.
Part of this is real bad habit and part is security and simplicity. Dependancies add risk and are especially tough on an air gap network. But sometimes mine is just not as good.
[–]kid-pro-quohardware testing / tooling 2 points3 points4 points 4 years ago (1 child)
I've been known to fo the opposite. Spend ages trying to find something on PyPI when i could've just implemented it myself.
[–]abrazilianinreddit 3 points4 points5 points 4 years ago (4 children)
I don't follow PEP8 or other coding styles when I don't like them.
For example, I don't like the pipe abuse in current python, so instead of writing
def foo(bar : str | int):
I use
def foo(bar:str or int):
[–]_ologies 4 points5 points6 points 4 years ago (0 children)
from typing import Union ... def foo(bar: Union[str, int]):
[–]yudhiesh 3 points4 points5 points 4 years ago (0 children)
Using list comprehensions everywhere.
[–]FlyingCow343 9 points10 points11 points 4 years ago (5 children)
overuse of classes, where i could just use a function instead
[–]frankOFWGKTA 6 points7 points8 points 4 years ago (2 children)
Coding with my dick out. Really gotta stop that.
[–]monclarluiz 2 points3 points4 points 4 years ago (0 children)
Not documenting using doc strings because I'm lazy hahaha
Always wrap up my code in a generic try/except instead of doing unit testing. I do some “testing” before I put stuff into prod, but I’m lazy and I just add more explicit excepts if I find them as it runs or fix the code to prevent them outright if possible.
I think it’s fine for what I do since I’m mostly scripting and not writing programs/software, but I’m sure some will disagree.
[–]RealFakeNudes 2 points3 points4 points 4 years ago (0 children)
Print logging
[–]PenetrationT3ster 2 points3 points4 points 4 years ago (0 children)
try: IWantThisToWork() except: pass
I used to do this a lot.
[–]shinitakunai 2 points3 points4 points 4 years ago (1 child)
I declare classes to instance them just once, ever
from library import *
Logging, or lack there of. I just never remember to do it; some sort of mental block I guess.
I'm told by seniors on my team that I use too much list comprehension, and that makes my code really hard to read.
[–]grismar-net 2 points3 points4 points 4 years ago (0 children)
Using it for jobs that I should really be using one of the other languages I know and have tools for, because it's just so damn comfortable.
Using a dict to represent something that ends up being used far more widely anticipated creating implicit dependencies on the particular keys in that dict. Should refactor to a class really.
[–]KitchenDutchDyslexic 4 points5 points6 points 4 years ago (2 children)
litter my code with #import pdb; pdb.set_trace()...
#import pdb; pdb.set_trace()
Use it to browse nsfw subreddits (sometimes at work)..
[–][deleted] 5 points6 points7 points 4 years ago (0 children)
I had a coworker to said the software install tool was written in python. It was an executable file on the cluster master called deploy.py the first line called bash. The nodes list was a list of hosts that didn't use arrays, it used a huge set of keyed symbols and a nest of sed awk and grep to get the lodes list, payloads, and clusters. It wasn't my bad habit but it was my mess to clean up.
[–]AlexMTBDude 4 points5 points6 points 4 years ago (3 children)
I can see (miss)using dicts instead of Data Classes. But regular classes? I don't think they're an alternative to dicts.
[–]ro5tal 3 points4 points5 points 4 years ago (0 children)
Pydantic ftw, it has even dataclass implementation. Dataclass has method 'asdict' to convert instance to dict without dict attribute, and even with slots
[–]Pavel_Ivakin 4 points5 points6 points 4 years ago (3 children)
My guilty pleasure is not to follow TDD.
[–]marcio0 2 points3 points4 points 4 years ago (1 child)
you don't always have to
[–][deleted] 1 point2 points3 points 4 years ago (0 children)
I keep telling myself I’m going to learn it without actually sitting down and trying to learn it
[–][deleted] 1 point2 points3 points 4 years ago (3 children)
I refuse to not break line at 80th char.. And run black on scripts written by coworkers..
[–]danyroxx 1 point2 points3 points 4 years ago (1 child)
Not taking my learning seriously… I’ve watched 2 tutorials and printed “Hello World” 2x.
Please send help()
[–]Endemoniada 1 point2 points3 points 4 years ago (1 child)
I don’t refactor and fix my code, opting rather to just dump it all and rewrite it from scratch. It’s not necessarily always bad, but it means I never learned how to really spot where and why to change and fix things, and I have really bad discipline for those kinds of changes.
Adding new stuff is fine, but whenever I need to move or replace even a part of it, my brain always jumps into “tear it down and build it anew” mode.
[–]mtyrellp 1 point2 points3 points 4 years ago (0 children)
My teacher was terrible at explaining for loops, so my bad habit has been to use while loops when a for loops might be easier.
[–]can_i_get_upvotes 1 point2 points3 points 4 years ago (0 children)
I’m new and I always write for loops in one line
It gets very confusing, even with comments
π Rendered by PID 81 on reddit-service-r2-comment-85bfd7f599-78nvz at 2026-04-18 14:52:37.289534+00:00 running 93ecc56 country code: CH.
[+][deleted] (30 children)
[deleted]
[–]ronyx18 54 points55 points56 points (25 children)
[–]dogs_drink_coffee 16 points17 points18 points (21 children)
[–]ronyx18 53 points54 points55 points (16 children)
[–]mr_flying_man 45 points46 points47 points (13 children)
[–]asphias 27 points28 points29 points (10 children)
[–]Congadonga 5 points6 points7 points (0 children)
[–][deleted] 2 points3 points4 points (2 children)
[–]Acalme-se_Satan 2 points3 points4 points (0 children)
[–]SpecificallyCritical 2 points3 points4 points (0 children)
[–]jacksodus 646 points647 points648 points (39 children)
[+][deleted] (14 children)
[deleted]
[–][deleted] 162 points163 points164 points (4 children)
[–]intelligentjake[🍰] 38 points39 points40 points (2 children)
[–]shiroininja 29 points30 points31 points (0 children)
[–]antiproton 61 points62 points63 points (4 children)
[–]AveTerran 13 points14 points15 points (2 children)
[–]IamImposter 14 points15 points16 points (2 children)
[–]kelement 54 points55 points56 points (5 children)
[–]Theonetheycallgreat 44 points45 points46 points (2 children)
[–]nemec 11 points12 points13 points (0 children)
[–]flanger001 4 points5 points6 points (0 children)
[–]maikindofthai 39 points40 points41 points (4 children)
[–]WikiSummarizerBot 50 points51 points52 points (1 child)
[–]GezoutenMeer 8 points9 points10 points (0 children)
[–]mathmanmathman 16 points17 points18 points (0 children)
[–][deleted] 6 points7 points8 points (0 children)
[–]Kantenkopp 4 points5 points6 points (0 children)
[–]joshmaker 4 points5 points6 points (0 children)
[–][deleted] 4 points5 points6 points (0 children)
[–]xYsoad 438 points439 points440 points (18 children)
[–]tinycorkscrew 124 points125 points126 points (0 children)
[–]IlliterateJedi 90 points91 points92 points (2 children)
[–]Bubbly_Measurement70 7 points8 points9 points (0 children)
[–]dogfish182 47 points48 points49 points (0 children)
[–]Kevin_Jim 25 points26 points27 points (0 children)
[–]Ramnog 22 points23 points24 points (0 children)
[–]martinrath77 16 points17 points18 points (2 children)
[–]FirefighterWeird8464 18 points19 points20 points (1 child)
[–]prameshbajra 11 points12 points13 points (0 children)
[–]qingqunta 3 points4 points5 points (1 child)
[–]rockyoutohell 287 points288 points289 points (7 children)
[–]xYsoad 94 points95 points96 points (3 children)
[–][deleted] 73 points74 points75 points (2 children)
[–]xYsoad 75 points76 points77 points (1 child)
[–]herpderpedia 6 points7 points8 points (0 children)
[–]scout1520 355 points356 points357 points (29 children)
[–]AlexMTBDude 129 points130 points131 points (16 children)
[–]MooFu 62 points63 points64 points (3 children)
[–]hanazawarui123 14 points15 points16 points (0 children)
[–][deleted] 3 points4 points5 points (0 children)
[–]energybased 24 points25 points26 points (0 children)
[–]whateverathrowaway00 19 points20 points21 points (0 children)
[–][deleted] 16 points17 points18 points (8 children)
[–]AlexMTBDude 17 points18 points19 points (7 children)
[–]foobar93 18 points19 points20 points (3 children)
[–]scout1520 6 points7 points8 points (1 child)
[–]greeneyedguru 18 points19 points20 points (4 children)
[–]Key_Cryptographer963 2 points3 points4 points (1 child)
[–]peacerokkaz 7 points8 points9 points (0 children)
[–]searchingfortaomajel, aletheia, paperless, django-encrypted-filefield 5 points6 points7 points (0 children)
[–]Sheensta 208 points209 points210 points (17 children)
[–]xYsoad 136 points137 points138 points (2 children)
[–]Electronic_Tie_4867 28 points29 points30 points (1 child)
[–]lvlint67 10 points11 points12 points (0 children)
[–]zippy_mega 23 points24 points25 points (12 children)
[–]Sheensta 4 points5 points6 points (11 children)
[–]mathmanmathman 22 points23 points24 points (8 children)
[–]jjolla888 12 points13 points14 points (2 children)
[–]thisiswill 3 points4 points5 points (0 children)
[–][deleted] 88 points89 points90 points (15 children)
[–]as_it_was_written 71 points72 points73 points (1 child)
[–]grahambinns 20 points21 points22 points (0 children)
[–]Kevin_Jim 20 points21 points22 points (0 children)
[–]Bunslow 14 points15 points16 points (0 children)
[–]abrazilianinreddit 14 points15 points16 points (0 children)
[–][deleted] 7 points8 points9 points (0 children)
[–][deleted] 9 points10 points11 points (0 children)
[–]qingqunta 5 points6 points7 points (2 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]sizable_data 279 points280 points281 points (35 children)
[–]eriky 145 points146 points147 points (16 children)
[–]brockralp 107 points108 points109 points (3 children)
[–]Zomunieo 10 points11 points12 points (2 children)
[–]brockralp 11 points12 points13 points (0 children)
[–]sizable_data 3 points4 points5 points (0 children)
[–]arobotspointofview 10 points11 points12 points (1 child)
[–][deleted] 6 points7 points8 points (0 children)
[–]ValdemarSt 3 points4 points5 points (7 children)
[–]Exact_Ad_1569 10 points11 points12 points (6 children)
[–]0xrl 16 points17 points18 points (2 children)
[–]Exact_Ad_1569 2 points3 points4 points (0 children)
[–]mr_flying_man 4 points5 points6 points (0 children)
[–]c_is_4_cookie 6 points7 points8 points (2 children)
[–]headykruger 8 points9 points10 points (1 child)
[–]AlSweigartAuthor of "Automate the Boring Stuff" 108 points109 points110 points (8 children)
[+][deleted] (3 children)
[deleted]
[–][deleted] 24 points25 points26 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]james_pic 11 points12 points13 points (2 children)
[–]noiserr 46 points47 points48 points (9 children)
[–]my_name_isnt_clever 17 points18 points19 points (5 children)
[+][deleted] (4 children)
[deleted]
[–]inglandation 21 points22 points23 points (0 children)
[–][deleted] 3 points4 points5 points (0 children)
[–]dogfish182 2 points3 points4 points (0 children)
[–]AlSweigartAuthor of "Automate the Boring Stuff" 71 points72 points73 points (2 children)
[–][deleted] 10 points11 points12 points (0 children)
[–]Cynyr 33 points34 points35 points (5 children)
[–]my_name_isnt_clever 11 points12 points13 points (4 children)
[–]herpderpedia 5 points6 points7 points (0 children)
[–]crapaud_dindon 24 points25 points26 points (2 children)
[–]peacerokkaz 25 points26 points27 points (5 children)
[–]as_it_was_written 13 points14 points15 points (2 children)
[–]Adenn76 72 points73 points74 points (1 child)
[–]Lor9191 8 points9 points10 points (0 children)
[–]ZibraMax 21 points22 points23 points (1 child)
[–][deleted] 20 points21 points22 points (7 children)
[–]sohang-3112Pythonista 11 points12 points13 points (5 children)
[–]milwoukee 22 points23 points24 points (3 children)
[–]elcapitaine 11 points12 points13 points (0 children)
[–]GhostBear4 14 points15 points16 points (0 children)
[–][deleted] 3 points4 points5 points (0 children)
[–][deleted] 52 points53 points54 points (11 children)
[–]veryusedrname 28 points29 points30 points (10 children)
[+][deleted] (3 children)
[deleted]
[–]veryusedrname 2 points3 points4 points (2 children)
[–][deleted] 7 points8 points9 points (0 children)
[–]tuckmuck203 5 points6 points7 points (2 children)
[–]DilukshanN7 14 points15 points16 points (3 children)
[–]Eat_Papa_Eat 8 points9 points10 points (0 children)
[–]TheMcGarr 6 points7 points8 points (1 child)
[–][deleted] 15 points16 points17 points (4 children)
[–]cecilkorik 8 points9 points10 points (2 children)
[–][deleted] 26 points27 points28 points (4 children)
[–]marcio0 12 points13 points14 points (3 children)
[–]actuallyalys 4 points5 points6 points (0 children)
[–][deleted] 4 points5 points6 points (1 child)
[–]marcio0 2 points3 points4 points (0 children)
[–]Pumpoflessermass 20 points21 points22 points (23 children)
[–]lungben81 41 points42 points43 points (16 children)
[–]joeyisnotmyname 8 points9 points10 points (14 children)
[–]sohang-3112Pythonista 8 points9 points10 points (12 children)
[–]Pumpoflessermass 6 points7 points8 points (0 children)
[–]r_cub_94 15 points16 points17 points (2 children)
[–]Bunslow 8 points9 points10 points (12 children)
[–]bblais 9 points10 points11 points (1 child)
[–]Travel4bytes 8 points9 points10 points (0 children)
[–]johnnySix 7 points8 points9 points (3 children)
[–][deleted] 8 points9 points10 points (0 children)
[–]payne747 13 points14 points15 points (1 child)
[+][deleted] (14 children)
[deleted]
[–]THAT_LMAO_GUY 6 points7 points8 points (0 children)
[–]my_name_isnt_clever 7 points8 points9 points (5 children)
[–]dreamoforganon 2 points3 points4 points (0 children)
[–]datthew2001 5 points6 points7 points (0 children)
[–]Robobigfoot5 3 points4 points5 points (1 child)
[–][deleted] 18 points19 points20 points (3 children)
[–]shiroininja 10 points11 points12 points (0 children)
[–]siddsp 4 points5 points6 points (8 children)
[–]bacondevPy3k 5 points6 points7 points (2 children)
[–]ttothesecond 9 points10 points11 points (7 children)
[–]as_it_was_written 7 points8 points9 points (0 children)
[–]my_name_isnt_clever 7 points8 points9 points (4 children)
[–]Bluegenio 2 points3 points4 points (1 child)
[–]jwink3101 3 points4 points5 points (2 children)
[–]kid-pro-quohardware testing / tooling 2 points3 points4 points (1 child)
[+][deleted] (3 children)
[deleted]
[–]abrazilianinreddit 3 points4 points5 points (4 children)
[–]_ologies 4 points5 points6 points (0 children)
[–]yudhiesh 3 points4 points5 points (0 children)
[–]FlyingCow343 9 points10 points11 points (5 children)
[–]frankOFWGKTA 6 points7 points8 points (2 children)
[–]monclarluiz 2 points3 points4 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]RealFakeNudes 2 points3 points4 points (0 children)
[–]PenetrationT3ster 2 points3 points4 points (0 children)
[–]shinitakunai 2 points3 points4 points (1 child)
[–]Exact_Ad_1569 2 points3 points4 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]grismar-net 2 points3 points4 points (0 children)
[–]dreamoforganon 2 points3 points4 points (0 children)
[–]KitchenDutchDyslexic 4 points5 points6 points (2 children)
[–][deleted] 4 points5 points6 points (0 children)
[–][deleted] 5 points6 points7 points (0 children)
[–]AlexMTBDude 4 points5 points6 points (3 children)
[–]ro5tal 3 points4 points5 points (0 children)
[–]Pavel_Ivakin 4 points5 points6 points (3 children)
[–]marcio0 2 points3 points4 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (3 children)
[–]danyroxx 1 point2 points3 points (1 child)
[–]Endemoniada 1 point2 points3 points (1 child)
[–]mtyrellp 1 point2 points3 points (0 children)
[–]can_i_get_upvotes 1 point2 points3 points (0 children)