| | |
Getting a tuple to return values.
Thread Solved |
•
•
Join Date: Jan 2009
Posts: 42
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Jun 2008
Posts: 122
Reputation:
Solved Threads: 30
1
#2 32 Days Ago
You can use the observer pattern.
More detailed
Python Syntax (Toggle Plain Text)
class Observed(object): def register_observer(self,observer): self.observer=observer def notify(self): self.observer.update(self) class Observer(object): def update(self,other): print(self,"gets that ",other," has moved") #decide what to do pass class Something(Observer,Observed): def __init__(self,x,y): self.x=x self.y=y #super(Something,self).__init__() def move_to(self,x,y): self.x=x self.y=y self.notify() def update(self,other): super(Something,self).update(other) if other.x<self.x: self.move_to(self.x-1,self.y)# or whatever o1=Something(1,2) o2=Something(5,4) o1.register_observer(o2) o2.register_observer(o1) o1.move_to(1,2)
•
•
Join Date: Sep 2009
Posts: 50
Reputation:
Solved Threads: 16
0
#3 32 Days Ago
why not use a set for this?
Python Syntax (Toggle Plain Text)
info = {'x1': 123, 'y1': 456, 'x2: 789, 'y2': 012}
•
•
Join Date: Jan 2009
Posts: 42
Reputation:
Solved Threads: 0
0
#5 31 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:
i get the error:
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
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,800i 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; 31 Days Ago at 9:46 am.
1
#6 31 Days Ago
•
•
•
•
Python Syntax (Toggle Plain Text)
def getpos(): r1 = x,y = 100,100 r2 = x,y = 800,800
i get the error:
I have defined getPos() for both r1 and r2 (my objects), so why do i get that error?Traceback (most recent call last): r1.step(r2.getPos()[0],r2.getPos()[1]) AttributeError: r2 instance has no attribute 'getPos'
Any one got any clues?
self !Your function should be defined as such:
python Syntax (Toggle Plain Text)
def getpos(self): r1 = x,y = 100,100 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; 31 Days Ago at 9:53 am.
•
•
Join Date: Jan 2009
Posts: 42
Reputation:
Solved Threads: 0
0
#8 31 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.
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?
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; 31 Days Ago at 10:00 am.
0
#9 31 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)
0
#10 31 Days Ago
Give it a little test ...
python Syntax (Toggle Plain Text)
class RobotB: def getPos(self): self.r1 = x,y = 100,200 self.r2 = x,y = 700,800 return self.r1, self.r2 r2 = RobotB() print r2.getPos() # ((100, 200), (700, 800)) print r2.getPos()[1] # (700, 800) print r2.getPos()[1][0] # 700
May 'the Google' be with you!
![]() |
Similar Threads
- Return values from functions (C)
- Function to return two values (C++)
- comparing return values of subroutines (Assembly)
- return values (C++)
Other Threads in the Python Forum
- Previous Thread: python books or websites
- Next Thread: Clear the terminal window.
| Thread Tools | Search this Thread |
abrupt alarm ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog cx-freeze data decimals dictionaries dictionary directory dynamic error examples exe file float format function gnu graphics gui halp heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite statistics string strings sudokusolver sum table terminal text thread threading time tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable ventrilo wikipedia write wxpython xlib






