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

Can someone please help me? Thank you.

Recommended Answers

All 6 Replies

It easier to help if you post some code and explain what the problem is.

OK. I tested calling one object's methods from another object like so.

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.

Must be a bug in my actual program.

Look at this,and se if inherits from other classes make it clearer.
What you are doing is not the way to this.

>>> 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
>>>
commented: Not the same concept as what is being discussed in the topic. +0
commented: Use negrep sparingly +4

That works fine if the second class builds on the function of the first. I'm talking about messaging between two conceptually different objects.

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

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.