Object-Oriented Programming (OOP) in Python – A Good Idea? 🚨🐍



► Object-Oriented Programming in Python: The 7 Best Resources

Got this Python question from Kevin over Twitter: “Hello Dan, are you for or against object-oriented programming in Python and also in general? If against, why?”

In this video I share some of my thoughts on OOP in Python and whether or not you should invest time into learning OOP skills as a Python developer.

Tweet your Python and software dev career questions at me (@dbader_org) or…

19 Comments

  1. I am going through this exact phase now, I haven't got much clue of OOP, but I am starting to learn to try and implement it, because I have to use it at work. All I would say OOP is really good for isolating functionality for specific things within your program (I.e. the objects properties and methods), but what I have begun to hate about it is the fact that it usually gets spread out into several Scripts, which can be hard to debug by others, especially when they call objects within an object call, e.g. object1 calls method of object2 > then within object2 anther call to method of object3 and going on and on.. can make things hard because most times you need to understand each of overall objects to better understand what the method is doing.

  2. I think OOP is applicable to projects or solutions where systems need to be running in memory for long periods of time…such as the background code running for web applications. Also objects carry data and methods (business logic) inside them which makes it easy to pass them through to other functions and procedures, rather then passing through file handles, database connections..etc.

  3. 4:07 Sometimes you have to try things and see. And then decide that it doesn’t work so well the way you first wrote it, so you have to change it. This is where you have to be ruthless about chucking away old code.

    Of course, having a good version-control system like Git helps. It gives you the confidence to try things in different, possibly risky, ways, because you know you can always change your mind and go back.

  4. The opposite of “procedural” is “functional”, not “object-oriented”. Most “object-oriented” languages are in fact “procedural” as well. Python does OO and procedural, but it also has functional constructs, e.g. list comprehensions.

    Python also does functions as first-class objects, which is a rarity in OO languages. But then, in Python, everything is an object. E.g.

    (3).__add__(4)

    returns 7.

  5. Hi Dan,
    I have 2 years experience of programming, and I know how to implement Python and some stuff, but still I can't build apps.
    I'm a self paced learner
    I don't have CS BC.

    Any advice?

  6. I'm not intending to be rude, and I know people have mentioned this –but as a native English speaker, the term "procedural" is "pro-see-jur-uhl". On the whole, I wanted to say though that your English is quite good, which is saying something because our spelling/pronunciation is really fucked up. Sometimes "d" becomes a "j", and our vowels are all fucked up. Each of these depends on the dialect of English and often times even the word. But since this word is actually quite important for your field, I figured I'd leave a comment. =P Keep up the good work!

  7. Good video again thank you Daniel.
    I've just started to lear python as a ""first"" programming language last summer, so i'm pretty much new at python and programming in general. What you described was exactly what happend to me: no mention of OOP until late in my ""training"" and than it did hit me in the face like a metheorit hit the dinosaurs.

    since i'm new i probably gona say something wrong now, but the way i understand it: if i start to have many functions with many "global" in it, i think writing classes with attributes and methods makes my life much easier. And who ever can possibly read thoes code lines life easier as well.

    Also GUI (ie with tkinter) for some reason makes more sense to me in OOP. But i can not explain the reason.

  8. Great video again, Dan! I‘m very much into web scraping/data science and have never felt the need for OOP. Still there is this nagging feeling that I‘m missing out on something by not using OOP in these areas. Would be nice to hear more on this topic from you!

  9. I DON'T HAVE A QUESTION, BUT A SUGGESTION HOW YOU COULD HELP NEW PYTHON PROGRAMERS. I KNOW FROM A PREVIOUS OCCUPATION. THERE IS NO PRERSPECTIVE. I SUGGEST TAKE 5 OR 6 PROGRAMS AND EXPLAIN THERE STRUCTURE, WHY YOU DID CERTAIN THINGS. SOME DO'S AND DON'TS. I KNOW THIS IS A LOT OF WORK.

  10. I would love you to make an OOP series with Python. I know on your site you delve into OOP with your iterator, generator and magic class' tutorials!

    As a beginning programmer OOP doesn't seem useful until you start coding large apps; 50 to 100 lines or more of code. So if you find your program has 3 or more functions then OOP will make a lot of sense. I see OOP as a quick way housing your functions in one place(a class) and creating an instance(object) to use those functions(now known as methods) at any point in your program. It cleans up your code because the syntax of the dot notation and makes debugging A HELL OF A LOT SIMPLER!!! Instead of looking/scanning 100+ lines of code you'll just have at most 30 lines of code to look thru and quickly find what's throwing an exception because you'll quickly find that a method threw the exception and you can quickly edit it and be done with it. Instead of rewriting the whole program. I find with OOP and its advantage is it makes the class and it's methods fit the program whereas your program has to fit the functions if you take the procedural/functional programming route. Protip: Learn Pytest and you will know what I'm talking about when I say making the methods fit the program. Because with Pytest you're testing to see if your methods will throw an exception and how to debug it before it throws an exception when the program runs.

    Again I want to stress if you're writing a little script or a program that's less than 50 lines. Use functional programming. If you find yourself writing more than 50 lines and more than 3 functions. Do yourself a favor and write it in OOP or you're going to have a long day(s) of debugging the code.

Leave a Reply

Your email address will not be published.


*