How to call an object's methods from another object?

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2009
Posts: 73
Reputation: lrh9 is an unknown quantity at this point 
Solved Threads: 10
lrh9 lrh9 is offline Offline
Junior Poster in Training

How to call an object's methods from another object?

 
0
  #1
Oct 24th, 2009
I want to call another object's methods from another object, and I have no idea how.

Can someone please help me? Thank you.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 161
Reputation: snippsat is an unknown quantity at this point 
Solved Threads: 49
snippsat snippsat is offline Offline
Junior Poster
 
1
  #2
Oct 24th, 2009
It easier to help if you post some code and explain what the problem is.
Last edited by snippsat; Oct 24th, 2009 at 3:57 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 73
Reputation: lrh9 is an unknown quantity at this point 
Solved Threads: 10
lrh9 lrh9 is offline Offline
Junior Poster in Training
 
0
  #3
Oct 24th, 2009
OK. I tested calling one object's methods from another object like so.

  1. class Test:
  2.  
  3. def Call_Test2_Method(self, target):
  4. target.Test2_Method()
  5.  
  6.  
  7. class Test2:
  8.  
  9. def Test2_Method(self):
  10. print("This is Test2's method.")
  11.  
  12.  
  13. x = Test()
  14. y = Test2()
  15.  
  16. x.Call_Test2_Method(y)
  17.  
  18. def nested_test():
  19. c = Test()
  20. d = Test2()
  21.  
  22. c.Call_Test2_Method(d)
  23.  
  24. def test():
  25. a = Test()
  26. b = Test2()
  27.  
  28. a.Call_Test2_Method(b)
  29.  
  30. nested_test()
  31.  
  32. test()

I obtained:

This is Test2's method.
This is Test2's method.
This is Test2's method.
Must be a bug in my actual program.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 161
Reputation: snippsat is an unknown quantity at this point 
Solved Threads: 49
snippsat snippsat is offline Offline
Junior Poster
 
0
  #4
Oct 24th, 2009
Look at this,and se if inherits from other classes make it clearer.
What you are doing is not the way to this.
  1. >>> class Test:
  2. def a(self):
  3. self.var1 = 5000
  4. print 'i am a'
  5.  
  6.  
  7. >>> class Test1(Test):
  8. def b(self):
  9. print 'i am b'
  10. print 'value from class Test method a is %d' % (self.var1)
  11.  
  12.  
  13. >>> c = Test1()
  14. >>> c.a()
  15. i am a
  16. >>> c.b()
  17. i am b
  18. value from class Test method a is 5000
  19. >>>
Last edited by snippsat; Oct 24th, 2009 at 4:43 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 73
Reputation: lrh9 is an unknown quantity at this point 
Solved Threads: 10
lrh9 lrh9 is offline Offline
Junior Poster in Training
 
0
  #5
Oct 24th, 2009
That works fine if the second class builds on the function of the first. I'm talking about messaging between two conceptually different objects.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,054
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: 265
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is
 
0
  #6
Oct 24th, 2009
Originally Posted by lrh9 View Post
OK. I tested calling one object's methods from another object like so.
...
Must be a bug in my actual program.
Can you explain what the bug is? Each call worked and printed "This is Test2's method."

What was the expected output?

EDIT: I think I understand now. You're saying the real program that you made didn't work but this test script did? If that's the case disregard my above questions
Last edited by jlm699; Oct 24th, 2009 at 6:26 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 2009
Posts: 73
Reputation: lrh9 is an unknown quantity at this point 
Solved Threads: 10
lrh9 lrh9 is offline Offline
Junior Poster in Training
 
0
  #7
Oct 24th, 2009
Originally Posted by jlm699 View Post
EDIT: I think I understand now. You're saying the real program that you made didn't work but this test script did? If that's the case disregard my above questions
That's what I meant. I'm sorry I wasn't clear.

Originally Posted by snippsat View Post
It easier to help if you post some code and explain what the problem is.
Basically I'm trying to show these individuals a little object oriented design and code.

http://www.daniweb.com/forums/thread232741.html

http://www.daniweb.com/forums/thread232184.html

I was working on writing something similar to the first one, nothing complex but just a player class, a monster class, and a small battle, when I wanted to write a few methods. I wrote a method for a player to select among battle options, but only coded one option. The "attack" option. Then the player would need to select his or her target. So instead of taking time to code a way to display the monsters in the current battle and select from one, I just hard coded a target. Then I'd call the actual attack method to trigger the target's on hit method. That's where it failed. I'll probably go back and write the test battle with just one opponent.

I haven't worked on the second one yet.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC