Skip to content
Advertisement

How to stop a method being called when accesing its attributes like return values

The question is the following: How to stop a method being called when its attributes are, in fact, return values, that are necessary in another method, of another class.

This is how the program was written:

class S1():

      def __init__(self):
         pass

      def m2(self):
         #some values

      def m3(self):
         S2.m6()
         S1.m5()
         S2.m7()

      def m4(self):
           #some values

      def m5(self):

           n = int(input('Enter the number of games to simulate: '))

           prob = int(input('Enter the prob. for the player to win first game: '))

           #some values to calculate the probability

           return p, n


class S2(S1):

       def __init__(self):
            pass

       def m6(self):
            print('This program simulates a game')

       def m7(self):     

            print('Games simulated: ', S1().m5()[1])
            print('Points for 1st player: ', S1().m5()[0])



var1 = S1()
var1.m3()


Calling var1.m3() returns this result, which is repeating the function calls, because of the p and nattributes that are retrieved in the method m7() through this syntax:

 print('Games simulated: ', S1().m5()[1])       #the value of n variable
 print('Points for the player: ', S1().m5()[0])   #the value of p variable


It results in this effect:

This program simulates a game

Enter the number of games to simulate:  10

Enter the prob for the player to win first game:  0.5

Enter the number of games to simulate:  10

Enter the prob for the player to win first game:  0.5

Games simulated: 10

Enter the number of games to simulate:   10

Enter the prob for the player to win first game:   0.5

Points for the player: 6

Basically, S1().m5()[1] and S1().m5()[0] calls the functions, but I wanted to retrieve the values p and n from the tuple returned by m5().

The methods are interspersed and I am having some difficulty understanding why is this happening and how to avoid this type of repetions when using class attributes.

Thanks to all!

Advertisement

Answer

Why not do it like this:

class S1():

      def __init__(self):
         pass

      def m2(self):
         #some values

      def m3(self):
         S2.m6()
         p, n = S1.m5()
         S2.m7(p, n)

      def m4(self):
           #some values

      def m5(self):

           n = int(input('Enter the number of games to simulate: '))

           prob = int(input('Enter the prob. for the player to win first game: '))

           #some values to calculate the probability

           return p, n


class S2(S1):

       def __init__(self):
            pass

       def m6(self):
            print('This program simulates a game')

       def m7(self, p, n):     

            print('Games simulated: ', n)
            print('Points for 1st player: ', p)



var1 = S1()
var1.m3()

I hope I didn’t mix up p and n, but the general gist should come accross..

Advertisement