Getting a tuple to return values.

Thread Solved

Join Date: Jan 2009
Posts: 42
Reputation: thehivetyrant is an unknown quantity at this point 
Solved Threads: 0
thehivetyrant thehivetyrant is offline Offline
Light Poster

Getting a tuple to return values.

 
0
  #1
30 Days Ago
Evening there.
I need to return an (x,y) tuple of an object, This x,y will be used to help 1 object calculate where the other object is and move towards it.

My problem is that both the objects need to know the others' x,y.
(i'm unsure how to get the x,y of both the objects in one tuple and still tell them apart.)

While writing that i thought that maybe i put 4 values in the tuple say, x1,y1,x2,y2 and then for the 1st object call x1,y1 only and the same for object 2, would that work?

Also i thought tuples are not changable and hence after the objects move towards each other, the loop'll start again and check their x,y but the x,y will be the same wont it because tuples cant be changed?

am i right or am i missing some way around this?


Sorry if i'm unclear and i apologies for the long post, i sometime have ideas of solving things while i'm writing the problem out.
Gets your brain moving and all.

Cheers guys
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 122
Reputation: slate is an unknown quantity at this point 
Solved Threads: 30
slate slate is offline Offline
Junior Poster
 
1
  #2
30 Days Ago
You can use the observer pattern.
  1.  
  2. class Observed(object):
  3. def register_observer(self,observer):
  4. self.observer=observer
  5. def notify(self):
  6. self.observer.update(self)
  7.  
  8. class Observer(object):
  9. def update(self,other):
  10. print(self,"gets that ",other," has moved")
  11. #decide what to do
  12. pass
  13.  
  14.  
  15. class Something(Observer,Observed):
  16. def __init__(self,x,y):
  17. self.x=x
  18. self.y=y
  19. #super(Something,self).__init__()
  20. def move_to(self,x,y):
  21. self.x=x
  22. self.y=y
  23. self.notify()
  24. def update(self,other):
  25. super(Something,self).update(other)
  26. if other.x<self.x: self.move_to(self.x-1,self.y)# or whatever
  27.  
  28. o1=Something(1,2)
  29. o2=Something(5,4)
  30.  
  31. o1.register_observer(o2)
  32. o2.register_observer(o1)
  33. o1.move_to(1,2)
More detailed
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 50
Reputation: lukerobi is an unknown quantity at this point 
Solved Threads: 16
lukerobi lukerobi is offline Offline
Junior Poster in Training
 
0
  #3
29 Days Ago
why not use a set for this?

  1. info = {'x1': 123, 'y1': 456, 'x2: 789, 'y2': 012}
  2.  
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 42
Reputation: thehivetyrant is an unknown quantity at this point 
Solved Threads: 0
thehivetyrant thehivetyrant is offline Offline
Light Poster
 
0
  #4
29 Days Ago
I have heard sets are pretty much better than tuple but for this piece of code i am required to use tuples.
Doctor's orders.

Thanks slate i'll have a look that this observer's pattern and try to see if i can work out my issues from that.

I'll get back with my progress later today.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 42
Reputation: thehivetyrant is an unknown quantity at this point 
Solved Threads: 0
thehivetyrant thehivetyrant is offline Offline
Light Poster
 
0
  #5
29 Days Ago
I'm having problems with a beast of an error.
A Beast i tell you!

I'm going to try explaining it without pasting ALL my code.

i wrote my getPos like this:
def getpos():
        r1 = x,y = 100,100
        r2 = x,y = 800,800

i get the error:
Traceback (most recent call last):
    r1.step(r2.getPos()[0],r2.getPos()[1])
AttributeError: r2 instance has no attribute 'getPos'

thats quite a shortened down version, it might be enough.
The part that confuses me is the part in the error was written by my lecturer and therefore is correct.

I have defined getPos() for both r1 and r2 (my objects), so why do i get that error?
Any one got any clues? As i dont.

cheers
Last edited by thehivetyrant; 29 Days Ago at 9:46 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,045
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 263
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is
 
1
  #6
29 Days Ago
Originally Posted by thehivetyrant View Post
  1. def getpos():
  2. r1 = x,y = 100,100
  3. r2 = x,y = 800,800

i get the error:
Traceback (most recent call last):
    r1.step(r2.getPos()[0],r2.getPos()[1])
AttributeError: r2 instance has no attribute 'getPos'
I have defined getPos() for both r1 and r2 (my objects), so why do i get that error?
Any one got any clues?
So if getpos() is a function for a class object, then you forgot the most important detail. The parameter self !

Your function should be defined as such:
  1. def getpos(self):
  2. r1 = x,y = 100,100
  3. r2 = x,y = 800,800

But then again you're using r1 and r2 inside this function, which may indicate this isn't a class function at all. So in that case, you wouldn't use r2.getPos(), you should just use getPos().

Maybe you should post a little bit more code so that we have some context to go by.
Last edited by jlm699; 29 Days Ago at 9:53 am.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,959
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 918
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
1
  #7
29 Days Ago
Check the spelling of getPos()
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 42
Reputation: thehivetyrant is an unknown quantity at this point 
Solved Threads: 0
thehivetyrant thehivetyrant is offline Offline
Light Poster
 
0
  #8
29 Days Ago
oh fudge, i must have changed that by mistake with all my changing of code to try and fix the problem, i definetly had it as getPos before.

I changed it and i got the error that i remember seeing before i must have screwed it up... Progress! haha.

r1.step(r2.getPos()[0],r2.getPos()[1])
TypeError: getPos() takes no arguments (1 given)

i've given 1 arguement for getPos which apprently shouldnt take one.
Is that my r1=x,y=100,100 thing? should i not give them the values of 100,100 and let them do by themselves?
Last edited by thehivetyrant; 29 Days Ago at 10:00 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,045
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 263
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is
 
0
  #9
29 Days Ago
Originally Posted by thehivetyrant View Post
oh fudge, i must have changed that by mistake with all my changing of code to try and fix the problem, i definetly had it as getPos before.

I changed it and i got the error that i remember seeing before i must have screwed it up... Progress! haha.

r1.step(r2.getPos()[0],r2.getPos()[1])
TypeError: getPos() takes no arguments (1 given)
See my above post
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,959
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 918
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #10
29 Days Ago
Give it a little test ...
  1. class RobotB:
  2. def getPos(self):
  3. self.r1 = x,y = 100,200
  4. self.r2 = x,y = 700,800
  5. return self.r1, self.r2
  6.  
  7. r2 = RobotB()
  8. print r2.getPos() # ((100, 200), (700, 800))
  9. print r2.getPos()[1] # (700, 800)
  10. print r2.getPos()[1][0] # 700
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC