2

I have a quick question regarding python classes. The following is the setup: I have one class as the "mastermind" class which contains various instances of other classes. Now these classes need to call a method of that other class, but I don't know how to do that. For example:

class mastermind(object):

    def __init__(self):
        self.hand = hand()

    def iNeedToCallThisMethod(self, funzies):
        print funzies

class hand(object):

    def __init(self):
        pass #here should be a call to the mastermind instance's method

example = mastermind()

Hope you guys can help me with that, my brain is steaming! Thanks a lot!

4 Answers 4

9

If you want to call mastermind's method, you need to have a reference to it.

For example

class mastermind(object):
    def __init__(self):
        self.hand = hand(self)

    def iNeedToCallThisMethod(self, funzies):
        print funzies

class hand(object):
    def __init__(self, mastermind)
        mastermind.iNeedToCallThisMethod('funzies')
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! That worked fine! But doesn't it take a lot more resources if I save the Mastermind instance into the other instance? Or is it really just a reference?
In the example I've given you are not even saving the instance - the method is called on a provided parameter. But even if you did save it, it is just a reference, so there is no reason to worry about it unless you have hundreds of thousands of hands.
1

If you need to call iNeedToCallThisMethod from the __init__ of hand, you should probably put the method in that class.

However, what you want to do can be achieved using a classmethod:

class mastermind(object):
   def __init__(self):
        self.hand = hand()

   @classmethod
   def iNeedToCallThisMethod(cls, funzies):
        print funzies

class hand(object):
   def __init__(self):
        mastermind.iNeedToCallThisMethod('funzies')

example = mastermind()

Comments

-1

Both object need a reference to one another, try passing the instance to the constructor.

class Mastermind(object):

    def __init__(self):
        self.hand = Hand(self)

    def test_funct(self, arg):
        print arg


class Hand(object):

    def __init(self, mastermind):
        self.mastermind = mastermind
        self.mastermind.test_funct()

Comments

-1
class hand(object):

    def __init(self, other_class):
        #here should be a call to the mastermind instance's method
        other_class.iNeedToCallThisMethod()
m_m = mastermind()
example = hand(m_m) # passes mastermind to new instance of hand

I'd just pass the object like above

6 Comments

You've got this all wrong, you need a reference between both object.
Can you elaborate I believe what I posted is correct @MalikBrahimi
iNeedToCallThisMethod is an instance method, so cannot therefore be called directly using the mastermind class, as you posted. mastermind.iNeedToCallThisMethod() would only be valid for class methods and static methods.
I believe you have mistaken that the mastermind was a call to the class mastermind but it was actually a call to a parameter being passed to the class. I should've made that clearer but the code works fine. I've changed mastermind to other_class
YES! So sorry, didn't realize that at all. The variable name threw me off
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.