944,188 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 904
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 25th, 2009
0

Getting a tuple to return values.

Expand Post »
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
Similar Threads
Reputation Points: 12
Solved Threads: 0
Junior Poster in Training
thehivetyrant is offline Offline
62 posts
since Jan 2009
Oct 25th, 2009
1
Re: Getting a tuple to return values.
You can use the observer pattern.
Python Syntax (Toggle Plain Text)
  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
Reputation Points: 56
Solved Threads: 65
Posting Whiz in Training
slate is offline Offline
242 posts
since Jun 2008
Oct 26th, 2009
0
Re: Getting a tuple to return values.
why not use a set for this?

Python Syntax (Toggle Plain Text)
  1. info = {'x1': 123, 'y1': 456, 'x2: 789, 'y2': 012}
  2.  
Reputation Points: 14
Solved Threads: 16
Junior Poster in Training
lukerobi is offline Offline
50 posts
since Sep 2009
Oct 26th, 2009
0
Re: Getting a tuple to return values.
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.
Reputation Points: 12
Solved Threads: 0
Junior Poster in Training
thehivetyrant is offline Offline
62 posts
since Jan 2009
Oct 26th, 2009
0
Re: Getting a tuple to return values.
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; Oct 26th, 2009 at 9:46 am.
Reputation Points: 12
Solved Threads: 0
Junior Poster in Training
thehivetyrant is offline Offline
62 posts
since Jan 2009
Oct 26th, 2009
1
Re: Getting a tuple to return values.
Python Syntax (Toggle Plain Text)
  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:
python Syntax (Toggle Plain Text)
  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; Oct 26th, 2009 at 9:53 am.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Oct 26th, 2009
1
Re: Getting a tuple to return values.
Check the spelling of getPos()
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 26th, 2009
0
Re: Getting a tuple to return values.
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; Oct 26th, 2009 at 10:00 am.
Reputation Points: 12
Solved Threads: 0
Junior Poster in Training
thehivetyrant is offline Offline
62 posts
since Jan 2009
Oct 26th, 2009
0
Re: Getting a tuple to return values.
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
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Oct 26th, 2009
0
Re: Getting a tuple to return values.
Give it a little test ...
python Syntax (Toggle Plain Text)
  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
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: python books or websites
Next Thread in Python Forum Timeline: Clear the terminal window.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC