| | |
How to call an object's methods from another object?
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2009
Posts: 73
Reputation:
Solved Threads: 10
0
#3 Oct 24th, 2009
OK. I tested calling one object's methods from another object like so.
I obtained:
Must be a bug in my actual program.
Python Syntax (Toggle Plain Text)
class Test: def Call_Test2_Method(self, target): target.Test2_Method() class Test2: def Test2_Method(self): print("This is Test2's method.") x = Test() y = Test2() x.Call_Test2_Method(y) def nested_test(): c = Test() d = Test2() c.Call_Test2_Method(d) def test(): a = Test() b = Test2() a.Call_Test2_Method(b) nested_test() test()
I obtained:
•
•
•
•
This is Test2's method.
This is Test2's method.
This is Test2's method.
•
•
Join Date: Aug 2008
Posts: 161
Reputation:
Solved Threads: 49
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.
What you are doing is not the way to this.
Python Syntax (Toggle Plain Text)
>>> class Test: def a(self): self.var1 = 5000 print 'i am a' >>> class Test1(Test): def b(self): print 'i am b' print 'value from class Test method a is %d' % (self.var1) >>> c = Test1() >>> c.a() i am a >>> c.b() i am b value from class Test method a is 5000 >>>
Last edited by snippsat; Oct 24th, 2009 at 4:43 am.
0
#6 Oct 24th, 2009
•
•
•
•
OK. I tested calling one object's methods from another object like so.
...
Must be a bug in my actual program.
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.
•
•
Join Date: Oct 2009
Posts: 73
Reputation:
Solved Threads: 10
0
#7 Oct 24th, 2009
•
•
•
•
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
•
•
•
•
It easier to help if you post some code and explain what the problem is.
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.
![]() |
Similar Threads
- Call to another class and instantiate object (Java)
- Get generic Object Methods. (Java)
- ArrayList of lists and arrays (Java)
- Object Oriented Design (PHP)
- Object Oriented Programming (Computer Science)
- what is object oriented???? (IT Professionals' Lounge)
- Calling Object Methods within a LinkedList (Java)
- using(STL)function object (bind2nd) with a user defined function object (C++)
Other Threads in the Python Forum
- Previous Thread: Getattr Problem
- Next Thread: Return selected variables
| Thread Tools | Search this Thread |
alarm assignment avogadro beginner bluetooth character cmd code customdialog cx-freeze data decimals dictionary directory dynamic error examples exe file float format function generator gnu graphics gui halp homework http ideas import input itunes java leftmouse line linux list lists logging loop maintain maze module mouse mysqldb number numbers output parsing path port prime programming projects push py2exe pygame pyglet pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh stdout string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode urllib urllib2 variable variables ventrilo verify vigenere web webservice wikipedia windows wxpython xlib






