python without OO

Peter Maas peter at somewhere.com
Wed Jan 26 08:10:47 EST 2005


Davor schrieb:
> so initially I was hoping this is all what Python is about, but when I 
> started looking into it it has a huge amount of additional (mainly OO) 
> stuff which makes it in my view quite bloated now.

So you think f.write('Hello world') is bloated and file_write(f,'Hello world')
is not? This is the minimum amount of OO you will see when using Python. But
I guess you will use OO in the long run to *avoid* bloated code:

--------------snip---------------

print  "*** Davor's evolution towards an OO programmer ***"

print '\n*** Step 1: OO is evil, have to use atomic variables:'

name1 = 'Smith'
age1 = 35
sex1 = 'male'
name2 = 'Miller'
age2 = 33
sex2 = 'female'

print name1, age1, sex1, name2, age2, sex2

print '\n*** Step 2: This is messy, put stuff in lists:'

p1 = ['Smith', 35, 'male']
p2 = ['Miller', 33, 'female']

for e in p1:
     print e

for e in p2:
     print e

print '\n*** Step 3: Wait ..., p[2] is age, or was it sex? Better take a dict:'

p1 = dict(name = 'Smith', age = 35, sex = 'male')
p2 = dict(name = 'Miller', age = 33, sex = 'female')

for e in p1.keys():
     print '%s: %s' % (e, p1[e])

for e in p2.keys():
     print '%s: %s' % (e, p2[e])

print '\n*** Step 4: Have to create person dicts, invoice dicts, ...better use dict templates:'

class printable:
     def __str__(self):
         '''magic method called by print, str() ..'''
         ps = ''
         for e in self.__dict__.keys():
             ps += '%s: %s\n' % (e, str(self.__dict__[e]))
         return ps

class person(printable):
     def __init__(self, name, age, sex):
         self.name = name
         self.age = age
         self.sex = sex

class invoice(printable):
     def __init__(self, name, product, price):
         self.name = name
         self.product = product
         self.price = price

per = person(name = 'Smith', age = 35, sex = 'male')
inv = invoice(name = 'Smith', product = 'bike', price = 300.0)

print per
print inv

--------------snip---------------

Either your program is small. Then you can do it alone. Or you will
reach step 4.

-- 
-------------------------------------------------------------------
Peter Maas,  M+R Infosysteme,  D-52070 Aachen,  Tel +49-241-93878-0
E-mail 'cGV0ZXIubWFhc0BtcGx1c3IuZGU=\n'.decode('base64')
-------------------------------------------------------------------



More information about the Python-list mailing list