OO approach to decision sequence?

Chinook chinook.nr at tds.net
Sun Jun 26 03:22:13 EDT 2005


On Sun, 26 Jun 2005 00:54:42 -0400, Bengt Richter wrote
(in article <42be2920.217477255 at news.oz.net>):

> On Sat, 18 Jun 2005 03:52:28 -0400, Brian van den Broek 
> <bvande at po-box.mcgill.ca> wrote:
> [...]
>> 
>> Now, the same sort of behaviour where the "if type" testing has been 
>> replaced with code more in keeping with the OOP approach:
>> 
>>>>> class C(object):
>> ... 	def report(self):
>> ... 		print "Found a C"
>> ... 		
>>>>> class D(object):
>> ... 	def report(self):
>> ... 		print "Found a D"
>> ... 		
>>>>> c = C()
>>>>> d = D()
>>>>> for item in (c, d):
>> ... 	item.report()
>> ... 	
>> Found a C
>> Found a D
>>>>> 
>> 
> 
> The OP might want to consider factoring report into a base class, e.g.,
> 
>  >>> class Base(object):
>  ...     def art_name(self):
>  ...         cname = type(self).__name__
>  ...         art = 'an'[:1+(cname.upper() in 'A E F H I L M N O R S X' or
>  ...                   len(cname)>1 and cname.upper()[0] in 'AEIOU')]
>  ...         return art, cname
>  ...
>  >>> class A(Base): pass
>  ...
>  >>> class B(Base): pass
>  ...
>  >>> class F(Base): pass
>  ...
>  >>> class Foo(Base): pass
>  ...
>  >>> class U(Base): pass
>  ...
>  >>> class Uhuh(Base): pass
>  ...
>  >>> items = A(), B(), F(), Foo(), U(), Uhuh()
>  >>> for item in items: print 'Found %s %s' % item.art_name()
>  ...
>  Found an A
>  Found a B
>  Found an F
>  Found a Foo
>  Found a U
>  Found an Uhuh
> 
> Returning info rather than printing to stdout allows you
> to access and use it differently, e.g.,
> 
>  >>> items[3].art_name()
>  ('a', 'Foo')
>  >>> items[3].art_name()[1]
>  'Foo'
> 
> (Don't know if the a/an logic is really general ;-)
> 
> Regards,
> Bengt Richter
> 

Thanks Bengt,

I'm just in the middle of refactoring my perfectly good top down utility to 
an OO approach.  It's going to be a much more abstract and verbose animal, 
but that's OK because it's a learning exercise.  To make use of the 
abstraction I'm providing for output options of either .csv or .html table, 
and I was approaching such with a separate output base class.  I don't have 
the nuances of the language down yet, but you might say I'm taking an AO 
approach to OO refactoring??? :~)  Streatching it a bit probably in calling 
processing and reporting the two main aspects of an AO approach, but it's fun 
and keeps my head busy.  I've got a trial mockup of my decision sequence 
factory pattern working, so at least I'm making progress towards 
incomprehensible abstraction :<))  To avoid duplication I'm using code 
objects with my factory pattern to create applicable class methods for an 
instance.  Yea, I'm overdoing it, but as I said it's a learning exercise.  

I'll study what you supplied in the morning to see if I understand.  That and 
continue putting up firewood for next winter - tiring work in the 90 degree 
weather we're having in northern NE - especially at my age.  

Thanks again,
Lee C





More information about the Python-list mailing list