Simple questions on use of objects (probably faq)
Brian Elmegaard
brian at rkspeed-rugby.dk
Wed Mar 8 05:04:41 EST 2006
Hi,
I am struggling to understand how to really appreciate object
orientation. I guess these are FAQ's but I have not been able to find
the answers. Maybe my problem is that my style and understanding are
influenced by matlab and fortran.
I tried with the simple example below and ran into several questions:
1: Why can't I do:
def __init__(self,self.x):
and avoid the self.x=x
2: Is it a good idea to insert instances in a list or is there a simpler
way to do something with all instances of a given type?
3: Why canøt I say and get the maximum of instance attributes and a
list of them?
y_max=max(y[].x) and
ys=[y[].x]
4: Can I avoid the dummy counter i in the for loop and do something
like:
yz=[y[:-1].x-y[1:].x]
The code that runs:
class Foo:
def __init__(self,x):
self.x=x
y=[]
y.append(Foo(10.0))
y.append(Foo(110.0))
y.append(Foo(60.0))
ys=[]
y_max=0.0
y_min=0.0
for s in y:
ys.extend([s.x])
y_max=max(s.x,y_max)
y_min=min(s.x,y_min)
yz=[]
for i in range(len(ys)-1):
yz.append(ys[i+1]-ys[i])
What I hoped I could do:
class Foo:
def __init__(self,self.x):
continue
y=[]
y.append(Foo(10.0))
y.append(Foo(110.0))
y.append(Foo(60.0))
ys=([y[].x])
y_max=max(y[].x)
y_min=min(y[].x)
yz=[y[:-1].x-y[1:].x]
--
Brian (remove the sport for mail)
http://www.et.web.mek.dtu.dk/Staff/be/be.html
http://www.rugbyklubben-speed.dk
More information about the Python-list
mailing list