944,149 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 566
  • Python RSS
Oct 24th, 2009
0

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

Expand Post »
I want to call another object's methods from another object, and I have no idea how.

Can someone please help me? Thank you.
Similar Threads
Reputation Points: 106
Solved Threads: 35
Posting Whiz in Training
lrh9 is offline Offline
238 posts
since Oct 2009
Oct 24th, 2009
1
Re: How to call an object's methods from another object?
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.
Reputation Points: 280
Solved Threads: 280
Master Poster
snippsat is offline Offline
773 posts
since Aug 2008
Oct 24th, 2009
0
Re: How to call an object's methods from another object?
OK. I tested calling one object's methods from another object like so.

Python Syntax (Toggle Plain Text)
  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:

Quote ...
This is Test2's method.
This is Test2's method.
This is Test2's method.
Must be a bug in my actual program.
Reputation Points: 106
Solved Threads: 35
Posting Whiz in Training
lrh9 is offline Offline
238 posts
since Oct 2009
Oct 24th, 2009
0
Re: How to call an object's methods from another object?
Look at this,and se if inherits from other classes make it clearer.
What you are doing is not the way to this.
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 280
Solved Threads: 280
Master Poster
snippsat is offline Offline
773 posts
since Aug 2008
Oct 24th, 2009
0
Re: How to call an object's methods from another object?
That works fine if the second class builds on the function of the first. I'm talking about messaging between two conceptually different objects.
Reputation Points: 106
Solved Threads: 35
Posting Whiz in Training
lrh9 is offline Offline
238 posts
since Oct 2009
Oct 24th, 2009
0
Re: How to call an object's methods from another object?
Click to Expand / Collapse  Quote originally posted by lrh9 ...
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.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Oct 24th, 2009
0
Re: How to call an object's methods from another object?
Click to Expand / Collapse  Quote originally posted by jlm699 ...
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.

Click to Expand / Collapse  Quote originally posted by snippsat ...
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.
Reputation Points: 106
Solved Threads: 35
Posting Whiz in Training
lrh9 is offline Offline
238 posts
since Oct 2009

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: Getattr Problem
Next Thread in Python Forum Timeline: Return selected variables





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


Follow us on Twitter


© 2011 DaniWeb® LLC