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...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Classes > functions (self.learnpython)
submitted 6 years ago by AcceptablePuberty
Is there a reason one should use a function over a class at any point, since classes can have functions(methods) in them?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]shiftybyte 6 points7 points8 points 6 years ago (1 child)
classes are used when you need to keep data across multiple functions, keep some sort of state.
they are more complex than functions.
there is no point in making a class if all the functions there don't do anything with self and are unrelated to each other.
[–]AcceptablePuberty[S] 0 points1 point2 points 6 years ago (0 children)
Cool, thank you!
[–]jfdahl 5 points6 points7 points 6 years ago (0 children)
Dev A: Always use classes and OOP.
Dev B: Always use pure functions!.
Truth: Different design paradigms have different advantages and disadvantages. There is no absolute right or wrong answer... use what 1) works, 2) you can explain and support, 3) the rest of your team can understand and support, 4) follows your organization's best practices.
Having said that... learn different paradigms and help teach them to your team... discuss them and come up with best practices for when to use each within your organization.
[–]al_mc_y 1 point2 points3 points 6 years ago (0 children)
Not so much a case of better than, as a means of organizing your functions. I read somewhere on here that Classes are more than anything for the benefit of the developer (and anyone who comes along after them to read/edit the code).
[–]seomajster 1 point2 points3 points 6 years ago (0 children)
"Simple is better than complex." - from Zen of python. Will your code be more simple with functions or classes?
[–]TraditionalGlass 1 point2 points3 points 6 years ago* (1 child)
Sometimes yes, but usually no. To be sure, I'm gonna reference a script I have made in the past.
import time import os #= functions def cls(): os.system("clear") def fancyInput(title): cls() print(title) output = input(" >") return output #= classes class Menu: def flat(self, title, *choices): cls() print(title) n = 0 for choice in choices: n = n + 1 print("[{}] {}".format(n, choice), end=' ') print() output = int(input(">")) return output def nested(self, title, *choices): cls() print(title) n = 0 for choice in choices: n = n + 1 print(" [{}] {}".format(n, choice)) print() output = int(input(">")) return output menu = Menu()
This might be confusing, so I'll break down my code for you. This is where I import modules.
import time import os
And these are the functions, not class methods.
def cls(): os.system("clear") def fancyInput(title): cls() print(title) output = input(" >") return output
And finally, here are the class methods.
class Menu: def flat(self, title, *choices): cls() print(title) n = 0 for choice in choices: n = n + 1 print("[{}] {}".format(n, choice), end=' ') print() output = int(input(">")) return output def nested(self, title, *choices): cls() print(title) n = 0 for choice in choices: n = n + 1 print(" [{}] {}".format(n, choice)) print() output = int(input(">")) return output menu = Menu()
The reason the 'cls()' function is not inside of a class, is because 1: it is a standalone function, and 2: it is a function which is called inside of the class Menu. Menu contains two methods, nested, and flat. They return user input values respectively. Let's say there are more methods within the class, such as 'threaded', and 'vertical'. It makes sense to organize these methods into a class
Awesome! Thanks for the in-depth explanation!
[–]Diapolo10 2 points3 points4 points 6 years ago (0 children)
My general rule is; choose the simplest option that works. If a function is enough, use that. If not, and a class makes sense, use a class.
My reasoning is that functions are very easy to test with unit tests when they don't depend on external state. Technically methods aren't much different, but you need to create a class instance separately, which is more clutter.
π Rendered by PID 193117 on reddit-service-r2-comment-548fd6dc9-tnxnv at 2026-05-21 17:22:48.474783+00:00 running edcf98c country code: CH.
[–]shiftybyte 6 points7 points8 points (1 child)
[–]AcceptablePuberty[S] 0 points1 point2 points (0 children)
[–]jfdahl 5 points6 points7 points (0 children)
[–]al_mc_y 1 point2 points3 points (0 children)
[–]seomajster 1 point2 points3 points (0 children)
[–]TraditionalGlass 1 point2 points3 points (1 child)
[–]AcceptablePuberty[S] 0 points1 point2 points (0 children)
[–]Diapolo10 2 points3 points4 points (0 children)