Calling a function in a class of another program - Python
Question: As the title says, how do you call a function in a class of another program?

Class.py

    
    
    class Object:
         def __init__(self, mood):
             self.__mood = mood
         def set_mood(self, mood):
             self.__mood = mood
         def check_mood(self):
             if random.randint(1, 3) == 1:
                 self.__mood = 'happy'
             elif random.randint(1, 3) == 2:
                 self.__mood = 'hungry'
             elif random.randint(1, 3) == 3:
                 self.__mood = 'sleepy'
        def get_mood(self):
            return self.__mood
    

Generator.py

    
    
    from test import Object
    import test
    
    mood = 'happy'
    ani = Object.test(mood)
    
    print("This is your mood: " + get_mood())
    

Mood is set as a default value of "happy". I need that to change based on the
random integer roll in test so that when mood is displayed, it isn't always
displayed as "happy"


Answer: You have to **_explicitly_** call `check_mood` to change the object's
attribute. See code below.

    
    
    from random import randint
    
    class Foo(object):
    
        def __init__(self, mood):
            self.__mood = mood
            self.__selection = {
                                1:'happy',
                                2:'hungry',
                                3:'sleepy',
                                4:'angry',
                                5:'bored'
                                }
    
        def check_mood(self):
            m = randint(1,5)
            self.__mood = self.__selection[m]
    
        def get_mood(self):
            return self.__mood
    
    mood = 'happy'
    
    f = Foo(mood)
    print 'This is your previous mood:', f.get_mood()
    
    f.check_mood() # This rerolls the mood.
    print 'This is your current mood:', f.get_mood()
    
    # This is your previous mood: happy
    # This is your current mood: bored
    

Otherwise, if you want it to change "behind-the-scenes", I suggest calling
`check_mood` _inside_ `get_mood` (or just get rid of `check_mood` entirely and
put its code inside `get_mood`).

However, the way `get_mood` is defined makes it modify the original
`self.__mood`, rendering the original passed-in argument as lost once you call
`get_mood`.

