OOP & Abstract Classes

A

Adam Gaskins

Hi all,

-- Non critical info--
I am a fairly seasoned PHP developer (don't shoot, I'm changing teams!:) who
is admittedly behind the curve with OOP. Like most who learned PHP, I
started doing web app backend stuff, but I have moved to full blown windows
apps in the last 6 months using Winbinder. As you may know Winbinder is
essentially abandoned, and programming windows apps with Winbinder or even
GTK-PHP is less than ideal. I also deal heavily in serial/rs-232
communications which is a pain in PHP. Long story short, I'm tired of doing
things in such a hackish manner and want to write applications that are
cross platform (I'd like to get our production dept on linux eventually) and
truely object oriented.
-- END non critical stuff--

So I was beginning to learn OOP for PHP, and it seemed to me that abstract
classes were just right for my application. In my application I must
communicate with several peices of test equipment that communicate via
RS-232. Most use SCPI instructions, some do not and require low level
communication.

The way I understand abstract classes is that I could have a class that has
all my abstract methods such as 'init', 'getMeasurement', 'setPressure',
etc... then I could use this common interface to to control my different
pieces of hardware (after I write a library for each of them).

Is this a valid application for abstract classes? Or am I making life more
complicated than it need be?

Now, I have read that Pythons OOP implimentation is so much better and more
complete then PHP's, but I cant find anything about abstract classes except
this:
http://norvig.com/python-iaq.html

....which says it doesn't exist in Python and you can only do this hack to
make it work (and I don't particularly understand how to impliment it from
this article).

This makes me suspect I am making things more comlicated than they need to
be. Could someone help me understand the proper way to impliment a set
classes/methods to deal with several peices of similar and not so similar
hardware? Not looking for anyone to write code for me, just help
understanding some of these OOP concepts and how they would apply to my
situation.

Thanks!
-Adam
 
U

Ulrich Eckhardt

Adam said:
Long story short, I'm tired of doing things in such a hackish manner
and want to write applications that are cross platform (I'd like to
get our production dept on linux eventually) and truely object
oriented.

Adam, there is one notion here that I seriously dislike: you sound as if OOP
was a goal by itself for you. The point is that OOP is a tool, not a goal.
So, if it helps you do the things you really want, go ahead and use it.
Otherwise, just don't.
The way I understand abstract classes is that I could have a class that
has all my abstract methods such as 'init', 'getMeasurement',
'setPressure', etc... then I could use this common interface to to control
my different pieces of hardware (after I write a library for each of
them).

Is this a valid application for abstract classes? Or am I making life more
complicated than it need be?

I think that the term is rather "abstract base class" (ABC,
a.k.a. "interface"). Note that in Python, where you don't have to use
prototypes or declaration, you often don't do that. Instead you use "duck
typing", i.e. you simply create different classes for the different
hardware and if all of them support 'init' and 'setPressure' instances are
interchangeable ("Liskov substitution principle", IIRC) without knowing
anything about each other.
This makes me suspect I am making things more comlicated than they need to
be. Could someone help me understand the proper way to impliment a set
classes/methods to deal with several peices of similar and not so similar
hardware?

Just write a class for each piece and adhere to a common interface and
you're done.

;)

Uli
 
M

Mike Driscoll

Hi all,

-- Non critical info--
I am a fairly seasoned PHP developer (don't shoot, I'm changing teams!:) who
is admittedly behind the curve with OOP. Like most who learned PHP, I
started doing web app backend stuff, but I have moved to full blown windows
apps in the last 6 months using Winbinder. As you may know Winbinder is
essentially abandoned, and programming windows apps with Winbinder or even
GTK-PHP is less than ideal. I also deal heavily in serial/rs-232
communications which is a pain in PHP. Long story short, I'm tired of doing
things in such a hackish manner and want to write applications that are
cross platform (I'd like to get our production dept on linux eventually) and
truely object oriented.
-- END non critical stuff--

So I was beginning to learn OOP for PHP, and it seemed to me that abstract
classes were just right for my application. In my application I must
communicate with several peices of test equipment that communicate via
RS-232. Most use SCPI instructions, some do not and require low level
communication.

The way I understand abstract classes is that I could have a class that has
all my abstract methods such as 'init', 'getMeasurement', 'setPressure',
etc... then I could use this common interface to to control my different
pieces of hardware (after I write a library for each of them).

Is this a valid application for abstract classes? Or am I making life more
complicated than it need be?

Now, I have read that Pythons OOP implimentation is so much better and more
complete then PHP's, but I cant find anything about abstract classes except
this:http://norvig.com/python-iaq.html

...which says it doesn't exist in Python and you can only do this hack to
make it work (and I don't particularly understand how to impliment it from
this article).

This makes me suspect I am making things more comlicated than they need to
be. Could someone help me understand the proper way to impliment a set
classes/methods to deal with several peices of similar and not so similar
hardware? Not looking for anyone to write code for me, just help
understanding some of these OOP concepts and how they would apply to my
situation.

Thanks!
-Adam

I've never used (or heard of) the Abstract type...and the guy who
wrote the FAQ was being a jerk. It looks like he was just throwing in
an undefined variable name just to make his Python program break while
taking a pot shot at people who use that sort of thing. Whatever.

According to wikipedia, dynamic languages don't implement Abstract as
they can accomplish the same thing via duck typing:
http://en.wikipedia.org/wiki/Abstract_class . The way it describes
the class, it made me think of decorators as well.

Hopefully someone who has used Abstract classes will jump in here and
give you more information about whether or not they matter in Python.

Mike
 
M

Marco Mariani

Mike said:
I've never used (or heard of) the Abstract type...and the guy who
wrote the FAQ was being a jerk.

Who, Peter Norvig?

(from wikipedia)

Peter Norvig is an American computer scientist. He is currently the
Director of Research (formerly Director of Search Quality) at Google Inc.

He is a Fellow and Councilor of the American Association for Artificial
Intelligence and co-author, with Stuart Russell, of Artificial
Intelligence: A Modern Approach, now the standard college text. He
previously was head of the Computational Sciences Division (now the
Intelligent Systems Division) at NASA Ames Research Center, where he
oversaw a staff of 200 scientists performing NASA's research and
development in autonomy and robotics, automated software engineering and
data analysis, neuroengineering, collaborative systems research, and
simulation-based decision-making. Before that he was Chief Scientist at
Junglee, where he helped develop one of the first Internet comparison
shopping services; Chief designer at Harlequin Inc.; and Senior
Scientist at Sun Microsystems Laboratories.

etc. etc.


Yes, I usually look up in wikipedia before calling anyone a jerk :) :)
 
A

Adam Gaskins

Any idea why I didn't see this reply on my ng? I only see the reply from
Marco. Can't help but wonder if there is more that is not getting through
here.

Would someone mind forwarding me any other replies?

FWIW I'm using news.east.cox.net.

Thanks,
-Adam
 
P

Peter Otten

Adam said:
So I was beginning to learn OOP for PHP, and it seemed to me that abstract
classes were just right for my application. In my application I must
communicate with several peices of test equipment that communicate via
RS-232. Most use SCPI instructions, some do not and require low level
communication.

The way I understand abstract classes is that I could have a class that
has all my abstract methods such as 'init', 'getMeasurement',
'setPressure', etc... then I could use this common interface to to control
my different pieces of hardware (after I write a library for each of
them).

Is this a valid application for abstract classes? Or am I making life more
complicated than it need be?

Now, I have read that Pythons OOP implimentation is so much better and
more complete then PHP's, but I cant find anything about abstract classes
except this:
http://norvig.com/python-iaq.html

...which says it doesn't exist in Python and you can only do this hack to
make it work (and I don't particularly understand how to impliment it from
this article).

It is basically a non-implementaton. If you don't understand it you should
not mess with abstract base classes and work your way through an
introductory python textbook.

That said, the page is outdated; Python 2.6 has some support for abstract
classes. They even fail a bit earlier, when the class is instantiated
instead of when the method is called. Example:

from abc import ABCMeta, abstractmethod, abstractproperty

class AbstractDevice:
__metaclass__ = ABCMeta

@abstractmethod
def init(self):
pass

@abstractproperty
def pressure(self):
pass


class Compressor(AbstractDevice):
def init(self):
print "starting compressor"

def set_pressure(self, value):
print "setting pressure to", value
pressure = property(fset=set_pressure)

c = Compressor()
c.init()
c.pressure = 42

a = AbstractDevice() # fails with TypeError

This makes me suspect I am making things more comlicated than they need to
be. Could someone help me understand the proper way to impliment a set
classes/methods to deal with several peices of similar and not so similar
hardware? Not looking for anyone to write code for me, just help
understanding some of these OOP concepts and how they would apply to my
situation.

Personally I would just write the class I need

class Compressor(object):
def init(self):
print "starting compressor"

def set_pressure(self, value):
print "setting pressure to", value
pressure = property(fset=set_pressure)

and forget about the bureaucracy. Instead I recommend that you learn about
unit tests and write a few tests to ensure your classes work as specified.

Peter
 
A

Adam Gaskins

Wow, thanks Nick! This is just what I was looking for!

Thanks to Peter as well. And as for your suggestion that I probably
shouldn't mess with things I don't understand and learn the basics first...
well, that is probably sound advice, but I figured out years ago that I
learn things best by a) getting in over my head b) panicking a little c)
sorting it all out, and fianlly d) (in the case of programming) refactoring
to make it all proper later. It may be bss aackwards but it works for me :)

I am going to work through these examples in the morning, thanks again guys!
 
T

Terry Reedy

Adam said:
Wow, thanks Nick! This is just what I was looking for!

I agree that Nick's semi-abstract class is what you need.
After doing some subclasses, you may discover that there is more common
code that you can factor out and push to the base class. You may also
find it convenient to derive a mock subclass that returns simulated data
so you can test user code initially without involving (or waiting on)
real devices.
 
A

alex23

I've never used (or heard of) the Abstract type...and the guy who
wrote the FAQ was being a jerk. It looks like he was just throwing in
an undefined variable name just to make his Python program break while
taking a pot shot at people who use that sort of thing. Whatever.

The IAQ's entry on Abstract classes predates the introduction of
Abstract Base Classes to Python 2.6/3.0. Rather than being a "jerk",
the example shows how to produce similar behaviour without code AS
WELL as demonstrating a more extended approach. (You're not breaking
your program if the behaviour is intentional...)
Hopefully someone who has used Abstract classes will jump in here and
give you more information about whether or not they matter in Python.

Abstract classes are one mechanism for defining interfaces, but as
we've covered it's not the only approach that can be taken. I've
always been happy defining abstract methods using 'raise
NotImplementedError', but clearly the ABC approach is perceived as
having value otherwise it would never have been introduced into
2.6/3.0.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top