Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Practices of the Python Pro - Procedural vs. OOP

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dane

I was wondering where you draw the line between a python program that is procedural versus one that employs OOP. Most of my work professionally is in the in the operations/devops space and I find that procedural is fine most of the time. That said, I like OOP in python and have attempted a bit of domain-driven design, but I can't say I know exactly when to put on the OOP hat. What are your thoughts?

Cheers,
Greg
 
Bartender
Posts: 1259
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Greg,

I'm not a great Python expert , but as far as I know Python doesn't force you to use OOP. If the context you're working in doesn't benefit from OOP - as looks like be your case, you can use it within a procedural approach.
Most scripts I can think of are , indeed, procedures and I can't figure how they could have benefits from an OOP approach.
 
Greg Horie
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Claude

Thanks for the response.

I use Python a fair amount and in most cases I choose a procedural approach (I build a lot of command line tools). The part I'm wondering about is when do folks choose to cross over. I've used an OOP approach when I feel like there is a domain I'd like to model and there are a few obvious objects that will interact in this domain. I'm wondering if others have a more concrete approach to making this choice.
 
Claude Moore
Bartender
Posts: 1259
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, OOP besides any "philosophical" consideration solves - or tries to solve - a common problem procedural programming style tends to raise when a system grows , i.e messy code.
This doesn't imply that system build without recurring to OOP are messy. But working with objects helps programmers to achive separation of concerns better, favourites code cleanliness, encapsulation of implementation details and so on.
I think that a concrete approach is most often the best approach you can use. When you feel that a problem can be solved more naturally with a pure procedural Python script, well, do it.
And whenever you feel that organizing your code in classes will be beneficial, well, do it.
It's called experience, and many times it's more valuable than abstract principles.
 
Greg Horie
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Claude.

That's how I make my choice these days - an experience-based decision. I agree with all your points about encapsulation and code cleanliness, but these can be achieved with procedural programming. So I circle back to asking how developers make the choice. We can both rely on experience, but others may have a more concrete approach.
 
Claude Moore
Bartender
Posts: 1259
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, added a flag to this thread, let's see if someone else out there wants to join the discussion.
 
Author
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the great discussion here Greg and Claude. Sorry I didn't see it sooner—I don't seem to get emailed about the goings on in the forum lately!

I concur with the idea that a lot of this ends up being "by feel." The experience you describe is based on recognition of patterns lurking in your code that start to feel like a class. I think newer developers can also overcorrect for this after learning OOP, modeling everything under the sun into a class hierarchy. Eventually you find that middle ground. Python is a fortunate language to fall into because you can mix the two styles fairly seamlessly!

I don't think I've got any more concrete process than that, except maybe thinking about it from the developer experience perspective. I always find myself thinking, "How would I want to gain entry to these features? What API would be the most fluid for me in my other code?" That tends to uncover a lot of opportunity one way or the other.

Cheers,

Dane
 
Greg Horie
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the feedback Dane.

I've started on your book. I hope this will help me develop the intuition you've mentioned. Nice read so far!

Cheers,
Greg
 
Dane Hillard
Author
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope so as well! Keep me posted
 
Yup, yup, yup. Tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic