Simple questions on use of objects (probably faq)

Brian Elmegaard brian at rkspeed-rugby.dk
Wed Mar 8 07:19:20 EST 2006


Steven D'Aprano <steve at REMOVETHIScyber.com.au> 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 comprehension is the answer.

> method. Do this instead:
>
> ys.append(s.x)

I always get confused by extend and append.

> this is wasteful. Just call the function at the end, after collecting all
> the values:

Easier indeed.

> for index, value in enumerate(ys[:-1]):
>     yz.append(ys[index+1] - value)
>

I will need to study enumerate a bit.

> By the way, don't be shy about using more meaningful names for variables.
> ys and yz are terribly similar, and is a bug waiting to happen.

I know, and in the real code I use better names.

> You can't use continue in there, it isn't a null-op. Perhaps you wanted
> "pass"?

Yes.

>> yz=[y[:-1].x-y[1:].x]
>
> How about, before trying to invent short cuts, you actually learn some of
> the Python syntax? The [x:y] syntax already has a meaning to Python,
> just not what you want.

Perhaps it is not the same, but quite close. In matlab .* is
element-by-element multiplication. I was thinking about a .-
operator. wouldn't that make sense here?

> Also, while everything in Python is an object, you don't *have* to use
> object oriented techniques. 

In the real problem the class is:
class Stream:
    def __init__(self,T_start,T_end,Q_dot):
        self.T_start=T_start
        self.T_end=T_end
        self.Q_dot=Q_dot
        self.mcp=abs(Q_dot/(T_start-T_end))
        if T_start>T_end:
            self.type='hot'
        else:
            self.type='cold'

and I thought it would make sense to store this a objects. Otherwise I
would need to store each stream as a list is refer their indexes. 


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