Is it bad practice to use Functions in OOP?
oop, python
Solution
Meh, no.
Having a `main()` function is so ubiquitous and required in some languages that using that as a hook to launch is fine - something somewhere has to say 'go'. Though, it is pythonic to implement it as
class YourObjectHere(object):
###blahblahblah your code here
...
...
def main():
MyObj = YourObjectHere(*args, **kwargs)
OtherObj.do_stuff_with_obj(MyObj)
#etc etc etc
...
...
if __name__ == '__main__':
main()
Among other reasons, because this allows you to reuse the code as a module and import it without causing the thing to run every time.
Foolish consistency is consistent, sure, but it's foolish first and foremost. Do what you need to do, but look to the language to ensure that you aren't just duplicating effort. See https://www.youtube.com/watch?v=o9pEzgHorH0 for a great video about when writing a class is a poor idea -- the first rule of thumb typically being, if an object has two methods, and one of them is `__init__()`, it probably doesn't need to be a class.
Problem
I'm new to programming. I recently read that: Your program should have almost all functionality encapsulated in either functions or class methods This makes it seems as though I should not have both functions and methods. I have also read that methods should be short and simple. Well, I've recently made a small program that downloads images from blogs. I used classes and the OOP approach because I need to inherit certain things. However, because the methods should be short and do one thing, my program cannot do much. My question is, if I'm trying to use a pure OOP approach, how is it possible to avoid writing functions? My script follows basically this pattern: ``` class Tumblr(object): def __init__(self, user): self.user = user def get_posts(self): """Use tumblr api to return a user's posts.""" return client['blog']['posts'] def parse_images(self): """Returns images.""" images = [] for post in posts: if 'image' in post: images.append(post['image']) return images def parse_videos(self): """Returns videos.""" def main(): # this is a function, and thus not OOP? ``` I also have other classes for different website APIs, and also a Downloader class that actually downloads the files to disk and to the proper directory. The issue is, right now all I have are these isolated classes and methods. I thought about creating a `main` function that can also use other functions, but again, I don't think this is correct OOP. How can I actually get work done without writing functions? (The textbooks I've read have said functions shouldn't be used in pure OOP, if I am using methods.)