Is it bad practice to mix OOP and procedural programming in Python (or to mix programming styles in general)

Viewed 2881

I have been working on a program to solve a Rubik's cube, and I found myself unsure of a couple of things. Firstly, if I have a bunch of functions that have general applications (e.g., clearing the screen), and don't know whether or not I should change them to methods inside of a class. Part of my code is OOP and the other part is procedural. Does this violate PEP8/is it a bad practice? And I guess in broader terms, is it a bad idea to mix two styles of programming (OOP, functional, procedural, etc)?

1 Answers

I would say it's not bad at all, if the problem can be solved more easily that way and that your language supports it.

Programming paradigms are just different approaches to solving problems:

  • Procedural says "What are the steps that need to be performed to solve this problem?"
  • Functional says "What values should be transformed and how should they be transformed to solve this problem?"
  • OOP says "What objects need to interact with one another and what messages are needed to be sent between them to solve this problem?"

When you divide up your problem into smaller ones, you might find that some parts of the problem can be solved more easily in a functional way, other parts a procedural way. It's totally possible to have such a problem.

Python is mainly procedural and quite OOP, and has functional features (namely functools). It's not as functional as Haskell and not as OOP as C#, but it allows you to use those paradigms to some extent.

Related