By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,550 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,550 IT Pros & Developers. It's quick & easy.

[Classless] Just to be sure...

P: n/a
Hi all,
I just want to be sure that I have really understand what classless
means... Can you look at the following "test" function and tell me if
this is what you would have called classless programmation ?

thanks !
--
Yermat
import types

PropertyType = type(property())

class Prototype(object):
parent = None
def __init__(self, parent=None):
self.parent = parent

def clone(self):
return Prototype(self)

def __hasattr__(self, name):
if name in self.__dict__:
return True
elif self.parent!= None:
return self.parent.__hasattr__(self, name)
else:
return False

def __getattribute__(self, name):
if name in ['__dict__', 'parent']:
return object.__getattribute__(self, name)
if name in self.__dict__:
val = self.__dict__[name]
elif self.parent != None:
val = getattr(self.parent, name)
else:
val = object.__getattribute__(self, name)
if type(val) in [types.FunctionType, types.GeneratorType,
types.UnboundMethodType, types.BuiltinFunctionType]:
return val.__get__(self, self.__class__)
if type(val) == PropertyType:
return val.fget(self)
return val

def __setattr__(self, name, value):
if type(value) == types.MethodType:
value = value.im_func
self.__dict__[name] = value

def __add__(self, other):
return self.__add__(other)

def test():
import random

def show(self):
print '(%s, %s)' % (self.x, self.y)

def addPoint(self, other):
r = self.clone()
r.x = self.x + other.x
r.y = self.y + other.y
return r

point = Prototype()
point.show = show
point.__add__ = addPoint

a1 = point.clone()
a1.x = 3
a1.y = 5

a2 = point.clone()
a2.x = 7
a2.y = 9

print 'a1: ',
a1.show()
print 'a2: ',
a2.show()
def getX(self):
return random.randint(0, 10)

print 'a3:'
a3 = a2.clone()
a3.x = property(getX)

print a3.x
a3.show()
a3.show()
a3.show()

a2.y = 10
print 'a3: ',
a3.show()

p = a3.__add__(a2)
print 'p = a3 + a2: ',
p.show()

print 'a3 + a2: ',
(a3 + a2).show()

def squareDistance(self):
return (self.x ** 2) + (self.y ** 2)

point.squareDistance = squareDistance

a4 = a1.clone()
print 'a4: ',
a4.show()
print a4.squareDistance()

def pointTuple(self):
return (self.x, self.y)

a5 = a3.clone()
a5.tuple = pointTuple
print 'a5: ', a5.tuple()
a5.x = 10
print 'a5: ', a5.tuple()
print 'a3: ',
a3.show()

if __name__=='__main__':
test()

Jul 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.