Re: Simple questions on use of objects (probably faq)

2006-03-10 Thread Brian Elmegaard
bruno at modulix <[EMAIL PROTECTED]> writes: > I should just take some time and learn to read !-) Then I am better of than you. I just had to learn the syntax of a language :-) -- Brian (remove the sport for mail) http://www.et.web.mek.dtu.dk/Staff/be/be.html http://www.rugbyklubben-speed.dk --

Re: Simple questions on use of objects (probably faq)

2006-03-10 Thread bruno at modulix
Steven D'Aprano wrote: > On Thu, 09 Mar 2006 13:44:25 +0100, bruno at modulix wrote: > > (snip) >>And it's not "self-referential" - it introduces a references cycle >>(class object -> instances -> class object), which may or may not be a >>problem. > > > Every instance knows about a list of ev

Re: Simple questions on use of objects (probably faq)

2006-03-10 Thread Brian Elmegaard
Steven D'Aprano <[EMAIL PROTECTED]> writes: > With this method in the class, your solution is easier than ever: Nice solution. -- Brian (remove the sport for mail) http://www.et.web.mek.dtu.dk/Staff/be/be.html http://www.rugbyklubben-speed.dk -- http://mail.python.org/mailman/listinfo/python-l

Re: Simple questions on use of objects (probably faq)

2006-03-10 Thread Brian Elmegaard
Michael <[EMAIL PROTECTED]> writes: > Based on the code that runs, you want* this: > > [(y[x+1].x-y[x].x) for x in range(len(y)-1) ] Yes. > Since personally I find that a lot clearer than: > > map(float.__sub__, [X.x for X in y[1:]], [X.x for X in y[:-1] ]) Me too. -- Brian (remove the sport

Re: Simple questions on use of objects (probably faq)

2006-03-09 Thread Steven D'Aprano
On Thu, 09 Mar 2006 13:44:25 +0100, bruno at modulix wrote: > Steven D'Aprano wrote: > (snip) > >> I say "think you want" because I don't know what problem you are trying to >> solve with this messy, self-referential, piece of code. > > This messy piece of code is mine, thanks !-) You're welcom

Re: Simple questions on use of objects (probably faq)

2006-03-09 Thread Steven D'Aprano
On Thu, 09 Mar 2006 13:23:22 +0100, Brian Elmegaard wrote: > Steven D'Aprano <[EMAIL PROTECTED]> writes: > >> Can you explain more carefully what you are trying to do? If you want the >> square of the maximum value, just do this: > > I want to get the value of another attribute of the instance w

Re: Simple questions on use of objects (probably faq)

2006-03-09 Thread Michael
Brian Elmegaard wrote: ... > 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_

Re: Simple questions on use of objects (probably faq)

2006-03-09 Thread Brian Elmegaard
bruno at modulix <[EMAIL PROTECTED]> writes: > May I suggest that you first learn the language syntax and basics ?-) I'll try -- Brian (remove the sport for mail) http://www.et.web.mek.dtu.dk/Staff/be/be.html http://www.rugbyklubben-speed.dk -- http://mail.python.org/mailman/listinfo/python-li

Re: Simple questions on use of objects (probably faq)

2006-03-09 Thread bruno at modulix
Brian Elmegaard wrote: > bruno at modulix <[EMAIL PROTECTED]> writes: > > >>So it's time to move to 2.4x !-) > > > I guess so. > > > >>What is "going wrong" exactly ? > > > def _add_instance(cls, instance): > _add_instance=classmethod(_add_instance) > cls._instances.append(in

Re: Simple questions on use of objects (probably faq)

2006-03-09 Thread bruno at modulix
Steven D'Aprano wrote: (snip) > I say "think you want" because I don't know what problem you are trying to > solve with this messy, self-referential, piece of code. This messy piece of code is mine, thanks !-) And it's not "self-referential" - it introduces a references cycle (class object -> in

Re: Simple questions on use of objects (probably faq)

2006-03-09 Thread Brian Elmegaard
Steven D'Aprano <[EMAIL PROTECTED]> writes: > What you probably think you want is something like this: Thanks, that made it run. Now I need to study what classmethods are. > I say "think you want" because I don't know what problem you are trying to > solve with this messy, self-referential, piec

Re: Simple questions on use of objects (probably faq)

2006-03-09 Thread Brian Elmegaard
Steven D'Aprano <[EMAIL PROTECTED]> writes: > Can you explain more carefully what you are trying to do? If you want the > square of the maximum value, just do this: I want to get the value of another attribute of the instance with maximum x. I know I could do it like you suggest for the case wi

Re: Simple questions on use of objects (probably faq)

2006-03-09 Thread Steven D'Aprano
On Thu, 09 Mar 2006 11:54:11 +0100, Brian Elmegaard wrote: >> What is "going wrong" exactly ? > > def _add_instance(cls, instance): > _add_instance=classmethod(_add_instance) > cls._instances.append(instance) > > gives me: > d:/DTU/80494 $ python.exe ooo.py > Traceback (most recen

Re: Simple questions on use of objects (probably faq)

2006-03-09 Thread Steven D'Aprano
On Thu, 09 Mar 2006 12:24:05 +0100, Brian Elmegaard wrote: > James Stroud <[EMAIL PROTECTED]> writes: > >> You should look into __cmp__ and other magic methods. This is probably >> the type of functionality you seem to be after. > > Good example, I need to look at the magic methods. > What I wan

Re: Simple questions on use of objects (probably faq)

2006-03-09 Thread Brian Elmegaard
James Stroud <[EMAIL PROTECTED]> writes: > You should look into __cmp__ and other magic methods. This is probably > the type of functionality you seem to be after. Good example, I need to look at the magic methods. What I want is to get the value of another variable in C. Would I need to use __re

Re: Simple questions on use of objects (probably faq)

2006-03-09 Thread Brian Elmegaard
bruno at modulix <[EMAIL PROTECTED]> writes: > So it's time to move to 2.4x !-) I guess so. > What is "going wrong" exactly ? def _add_instance(cls, instance): _add_instance=classmethod(_add_instance) cls._instances.append(instance) gives me: d:/DTU/80494 $ python.exe ooo.py Tr

Re: Simple questions on use of objects (probably faq)

2006-03-09 Thread bruno at modulix
Brian Elmegaard wrote: > bruno at modulix <[EMAIL PROTECTED]> writes: > > >>Now how you could do it the OO way (Q&D, not really tested): > > > Something goes wrong in my 2.3 So it's time to move to 2.4x !-) What is "going wrong" exactly ? > when I change the syntax to > _add_instance=classm

RE: Simple questions on use of objects (probably faq)

2006-03-08 Thread Delaney, Timothy (Tim)
Max M wrote: > decorated = [(obj.x, obj) for obj in objects] > max_decorated = max(decorated) > max_obj = max_decorated[-1] Python 2.5 will make this even easier - max() and min() aquire a `key` keyword parameter much like list.sort()/sorted(). max_obj = max(objects, key=operator.attrgetter(

Re: Simple questions on use of objects (probably faq)

2006-03-08 Thread James Stroud
Brian Elmegaard wrote: > "Matt Hammond" <[EMAIL PROTECTED]> writes: > > >>y_max = max([e.x for e in y]) > > > Would there be a way to refer back to the e with maximum x, or how > could I find other attributes of it? > You should look into __cmp__ and other magic methods. This is probably the

Re: Simple questions on use of objects (probably faq)

2006-03-08 Thread Brian Elmegaard
bruno at modulix <[EMAIL PROTECTED]> writes: > Now how you could do it the OO way (Q&D, not really tested): Something goes wrong in my 2.3 when I change the syntax to _add_instance=classmethod(_add_instance). If I understand this correctly the class is keeping track of the instances of itself. T

Re: Simple questions on use of objects (probably faq)

2006-03-08 Thread Brian Elmegaard
"Matt Hammond" <[EMAIL PROTECTED]> writes: > y_max = max([e.x for e in y]) Would there be a way to refer back to the e with maximum x, or how could I find other attributes of it? -- Brian (remove the sport for mail) http://www.et.web.mek.dtu.dk/Staff/be/be.html http://www.rugbyklubben-speed.dk

Re: Simple questions on use of objects (probably faq)

2006-03-08 Thread bruno at modulix
Brian Elmegaard wrote: > 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 si

Re: Simple questions on use of objects (probably faq)

2006-03-08 Thread Brian Elmegaard
"Matt Hammond" <[EMAIL PROTECTED]> writes: > Hmmm, rereading, I think you're right ... and I think I'm confused too :-) You both are. > Attempt #2: > > yz = [ (y1.x - y2.x) for (y1,y2) in zip(y[:-1], y[1:]) ] > > Frankly, a for loop with an index would probably be easier to read :) Me too,

Re: Simple questions on use of objects (probably faq)

2006-03-08 Thread Brian Elmegaard
Steven D'Aprano <[EMAIL PROTECTED]> writes: Thanks for the answers. They are very useful. > self.args = (x, y, z) # save a copy of the arguments As always python makes it easy. max(obj.lister()) > 4 Actually I wanted to get the maximum of attributes of several instances. List com

Re: Simple questions on use of objects (probably faq)

2006-03-08 Thread Max M
Brian Elmegaard wrote: > "Matt Hammond" <[EMAIL PROTECTED]> writes: > > >>y_max = max([e.x for e in y]) > > > Would there be a way to refer back to the e with maximum x, or how > could I find other attributes of it? In that case a common idiom is to "decorate" decorated = [(obj.x, obj) for

Re: Simple questions on use of objects (probably faq)

2006-03-08 Thread Max M
Brian Elmegaard wrote: > 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. > What I hoped I could

Re: Simple questions on use of objects (probably faq)

2006-03-08 Thread Matt Hammond
On Wed, 08 Mar 2006 11:29:29 -, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > On Wed, 08 Mar 2006 11:00:09 +, Matt Hammond wrote: > >>> 4: Can I avoid the dummy counter i in the for loop and do something >>> like: >>> yz=[y[:-1].x-y[1:].x] >> >> yz = [e.x for e in y] >> yz.reverse() > > I

Re: Simple questions on use of objects (probably faq)

2006-03-08 Thread Brian Elmegaard
"Matt Hammond" <[EMAIL PROTECTED]> writes: > See "List comprehensions" in python docs: Great, thanks for the hint. -- Brian (remove the sport for mail) http://www.et.web.mek.dtu.dk/Staff/be/be.html http://www.rugbyklubben-speed.dk -- http://mail.python.org/mailman/listinfo/python-list

Re: Simple questions on use of objects (probably faq)

2006-03-08 Thread Steven D'Aprano
On Wed, 08 Mar 2006 11:00:09 +, Matt Hammond wrote: >> 4: Can I avoid the dummy counter i in the for loop and do something >> like: >> yz=[y[:-1].x-y[1:].x] > > yz = [e.x for e in y] > yz.reverse() I don't think that's what the O.P. actually wants. He seems to have misused slicing syntax as

Re: Simple questions on use of objects (probably faq)

2006-03-08 Thread Steven D'Aprano
On Wed, 08 Mar 2006 11:04:41 +0100, Brian Elmegaard wrote: > 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

Re: Simple questions on use of objects (probably faq)

2006-03-08 Thread Matt Hammond
Hi, > 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] y_max = max([e.x for e in y]) See "List comprehensions" in python docs: http://docs.python.org/tut/node7.html#SECTION00714 > 4: Can I avoid the dummy cou

Simple questions on use of objects (probably faq)

2006-03-08 Thread Brian Elmegaard
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