def test():
...
should have the same indentation as if __name__==__main__:
and it should work.
So try this ---
class Vector:
def __init__(self, start=[]):
self.data = start
def __add__(self, value):
res = []
for x in self.data: res.append(x+value)
return Vector(res)
__radd__ = __add__
def __mul__(self, value):
res = []
for x in self.data: res.append(x*value)
return Vector(res)
__rmul__ = __mul__
def sum(self, start=0):
return reduce(lambda x, y: x+y, self.data, start)
def prod(self, start = 1):
return reduce(lambda x, y: x * y, self.data, start)
def __repr__(self):return 'self.data'
def test():
x = Vector([2,4,6])
print x+3, 3+x
print x*4, 4*x
print x.sum(), x.prod()
y = Vector([1,2,3])
print x+y
print x*y
if __name__ == '__main__':test()
-----Original Message-----
From: Srividya Visweswarababu [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, March 13, 2002 1:39 PM
To: [EMAIL PROTECTED]
Subject: Vector Example - help...!!
Hi,
I was trying to run the Vector example code from
oreilly book, but is giving me error: test is not
defined.
class Vector:
def __init__(self, start=[]):
self.data = start
def __add__(self, value):
res = []
for x in self.data: res.append(x+value)
return Vector(res)
__radd__ = __add__
def __mul__(self, value):
res = []
for x in self.data: res.append(x*value)
return Vector(res)
__rmul__ = __mul__
def sum(self, start=0):
return reduce(lambda x, y: x+y, self.data, start)
def prod(self, start = 1):
return reduce(lambda x, y: x * y, self.data, start)
def __repr__(self):return 'self.data'
def test():
x = Vector([2,4,6])
print x+3, 3+x
print x*4, 4*x
print x.sum(), x.prod()
y = Vector([1,2,3])
print x+y
print x*y
if __name__ == '__main__':test()
__________________________________________________
Do You Yahoo!?
Try FREE Yahoo! Mail - the world's greatest free email!
http://mail.yahoo.com/
_______________________________________________
ActivePython mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
ActivePython mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs