473,320 Members | 2,000 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

[Classless] Just to be sure...

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
0 1189

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: David MacQuigg | last post by:
On 27 Apr 2004 16:34:56 -0700, has.temp2@virgin.net (has) wrote: >David MacQuigg <dmq@gain.com> wrote in message news:<bniq80hiib0gauiltuntk9jvia2getbnj4@4ax.com>... > >> Example of Simplified...
99
by: Jim Hubbard | last post by:
It seems that Microsoft not only does not need the classic Visual Basic developer army (the largest army of developers the world has ever seen), but now they don't need ANY Windows developer at a...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
16
by: John Rivers | last post by:
http://www.15seconds.com/Issue/030812.htm?voteresult=1 poor guy worked his heart out, just to make a page control and then they published it ha ha ha ha ha to "help" others
66
by: mensanator | last post by:
Probably just me. I've only been using Access and SQL Server for 12 years, so I'm sure my opinions don't count for anything. I was, nevertheless, looking forward to Sqlite3. And now that gmpy...
3
by: Jon L | last post by:
Hi, I'm hoping someone can help me with this problem. I'm not sure whether the problem lies with the software or with my understanding of the language. I'm using the Microsoft.XMLDOM object...
11
by: sterling.mckay | last post by:
Just started to teach myself C C++ programming... I have been very interested with computers for a while now and have a nac or so I thought for how they work ... hardware I am ok with ... I can...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.