User avatar
TerryC65
Posts: 178
Joined: Sat May 09, 2015 7:50 am
Location: Wimborne, Dorset, UK

Object Oriented Python V 'Old-fashioned' Code

Mon Nov 19, 2018 11:54 am

Hi,

I have been working on various Raspberry Pi based projects at our local Model Town for over three years now and have always produced non-OOP code. This is partly because I'm an 'old-fashioned' programmer, partly because I was never really a programmer anyway (I was a Systems Engineer before I retired) and partly because all of the projects required embedded routines (if that's got anything to do with it).

Anyway, for the current project the team, until recently, has been larger and my role was confined to requirements capture, overall system design and some hardware design and construction. All of the code was done by another Volunteer. However, this guy has now moved on and is temporarily unavailable to respond to questions, so I thought that I'd ask this one here:

In a device which consists of a Raspberry Pi Zero, a motor Drive Board and an A/D converter is there still an advantage to be gained by writing the code as Object Oriented Python?

The device is a motorised Gate Valve in a water pipe and the code to control the motor and read back the valve position, will be running in a thread, but apart from that no other code will be running in that Pi (every Gate Valve will have it's own Pi). The author of all the code on this system to date has used OOP and I know I will have to write some OOP code going forward; to support new sensors and other devices where there will be lots of bits of code running on a single Pi. In this case however, any GateValve Class that I wrote would only ever be instantiated once, so it seems a bit pointless.

Or is it?

User avatar
MrYsLab
Posts: 435
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Object Oriented Python V 'Old-fashioned' Code

Mon Nov 19, 2018 2:30 pm

In the case your present, there is one major advantage of OOP and that is the avoidance of globals. If you are thinking of converting the OOP code to non-OOP, you might wind up in global purgatory. From a performance point of view, there is no advantage.

If the current code is well organized, it may be easier for the next person in line to maintain the code.

Really the only "tricky" part of OOP, and it is not all that tricky, is playing with inheritance, but that does not seem to be the case here.

User avatar
TerryC65
Posts: 178
Joined: Sat May 09, 2015 7:50 am
Location: Wimborne, Dorset, UK

Re: Object Oriented Python V 'Old-fashioned' Code

Mon Nov 19, 2018 2:48 pm

Thanks for that.

In fact the original author has responded since I posted this Topic (I thought he was away) and largely confirms what you have said. Amongst other things he said:
Also, integrating non-OO code into the framework would be a headache. Generally, you want either entirely OO code, or none, (if you can) because a mix can get very confusing.
His reference to 'the framework' relates to the architecture he chose. All Pi's in the system contain exactly the same code and execute those elements that correspond to the hardware functions that they are responsible for. It's a good system and I certainly don't want to break it.

User avatar
MrYsLab
Posts: 435
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Object Oriented Python V 'Old-fashioned' Code

Mon Nov 19, 2018 3:46 pm

Ah, I didn't get that you wanted to integrate non-OOP into OOP - the original author is correct - doing that is fun for all :D

PhatFil
Posts: 1681
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: Object Oriented Python V 'Old-fashioned' Code

Mon Nov 19, 2018 3:51 pm

you can still write a functional or procedural script in python2/3 while employing OO libraries and their features.

If you can conceptualise the program flow and execution in a more traditional format for a script without creating your own objects go for it.

you can still use OO libs with import statements and use the return and results of the object methods of objects you initialise within your script.

Imho a very good example of this sort of OO/non-OO melding can be found with the arduino ide where you can employ many OO libs and features while wrtiing a pretty vanilla C code file to exploit them.

User avatar
TerryC65
Posts: 178
Joined: Sat May 09, 2015 7:50 am
Location: Wimborne, Dorset, UK

Re: Object Oriented Python V 'Old-fashioned' Code

Mon Nov 19, 2018 3:58 pm

MrYsLab wrote:
Mon Nov 19, 2018 3:46 pm
Ah, I didn't get that you wanted to integrate non-OOP into OOP - the original author is correct - doing that is fun for all :D
In my own defence, I didn't want to integrate non-OOP into OOP, I simply wanted to write code that was efficient. What I hadn't understood was the implications of writing a routine using non-OOP code to run as part of a framework meant I would have to deal with that problem anyway.

We learn something new every day!

Return to “Python”