Simple questions on use of objects (probably faq)

Brian Elmegaard brian at
Wed Mar 8 13:19:20 CET 2006

Steven D'Aprano <steve at> 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"?


>> 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):
        if T_start>T_end:

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)

More information about the Python-list mailing list