Simple questions on use of objects (probably faq)

  • Thread starter Brian Elmegaard
  • Start date
B

Brian Elmegaard

Steven D'Aprano said:
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, piece of code. If you could
explain what your goal is, there is probably a better way of reaching it.

Probably, this was just an example posted to show me how to add all
the instances to the class.
 
B

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 -> instances -> class object), which may or may not be a
problem.
If you could
explain what your goal is,

He did (more or less), please read the original post.
there is probably a better way of reaching it.

Probably, yes.
 
B

bruno at modulix

Brian said:
I guess so.





def _add_instance(cls, instance):
_add_instance=classmethod(_add_instance)
cls._instances.append(instance)

You want

def _add_instance(cls, instance):
cls._instances.append(instance)

_add_instance=classmethod(_add_instance)


(snip)
I will have to study classmethods.

May I suggest that you first learn the language syntax and basics ?-)

FWIW, a classmethod is a method that get the class object as first
argument (instance methods get a reference to the instance).
 
M

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_min=min(s.x,y_min)

yz=[]
for i in range(len(ys)-1):
yz.append(ys[i+1]-ys)

What I hoped I could do: ....
yz=[y[:-1].x-y[1:].x]


Based on the code that runs, you want* this:

[(y[x+1].x-y[x].x) for x in range(len(y)-1) ]

FWIW, "-" does not work across lists. You could use map if you wanted, but
personally I prefer the explicit loop which you wanted to not use:
yz=[]
for i in range(len(ys)-1):
yz.append(ys[i+1]-ys)


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

Regards,


Michael.
 
S

Steven D'Aprano

I want to get the value of another attribute of the instance with
maximum x.

One solution to that has already been given, using the DSU
(decorate-sort-undecorate) idiom:

list_of_instances = [C(x) for x in (1, 2, 3, 4)]
data = [(instance.x, instance.y) for instance in list_of_instances]
m = max(data)
print m[1] # prints the y value of the instance with the largest x value


But perhaps a better way would be to define a __cmp__ method for your
class so that comparisons are done by comparing the x attribute:


class C:
def __init__(self, x):
self.x = x
self.y = something_else()
def __cmp__(self, other):
# not tested
try:
if self.x < other.x:
return -1
elif self.x > other.x:
return +1
else:
return 0
except:
return NotImplemented


With this method in the class, your solution is easier than ever:

data = [C(x) for x in (1,2,3,4)]
m = max(data)
print m.x
print m.y
 
S

Steven D'Aprano

Steven D'Aprano wrote:
(snip)


This messy piece of code is mine, thanks !-)

You're welcome :)
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 every other instance, including
itself. That makes it self-referential in my book.
 
B

Brian Elmegaard

Michael said:
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.
 
B

bruno at modulix

Steven said:
On Thu, 09 Mar 2006 13:44:25 +0100, bruno at modulix wrote:

(snip)



Every instance knows about a list of every other instance, including
itself.

mmm... Well, that's True(tm).
That makes it self-referential in my book.

In mine too.
I should just take some time and learn to read !-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,289
Messages
2,571,448
Members
48,126
Latest member
ToneyChun2

Latest Threads

Top