Hey all,

I have a class with various methods and I want to pass the output of one method into the next method. For example:

class RockPaperScissors:
	'''Rock paper scissors game, best of 3'''
	tries = 3

	def __init__(self, name):
		'Reads in users call'
		self.name = name
		print 'You picked %s' % self.name   #%d if define a class variable
		RockPaperScissors.tries -= 1

	def random(self):
		'Generates a random number'
		number = randint(1, 3000)       
		print number
		'Test if even or odd'
		if number <= 10:
			output = 'paper'
		elif number < 20:
			output = 'scissors'
		else:
			output = 'rock'

	def decision(self):
		'Decides if win or loss'
		print ???????

At the end of the code, I'd like to print the output from def random; however, I'm not sure how to tell the code to recognize that output was used in the above method/instance.

I see there is a lot of information on how to pass variables between classes, but not on instances. Also, if anyone could recommend a better random number generator than the one from random array input, that would be really helpful. Also, am I confusing the terminology of instance and method here?

Thanks.

Also, am I confusing the terminology of instance and method here?

Yes. A method is a function. So a class's method would be one of the functions you have defined in your class. The instance is what you do to actually create and object of that class.

So

class MyClass(object):
    def __init__(self):
        # This is the __init__ method
    def random(self):
        # This is the random method... etc.

class_obj1 = MyClass()
# class_obj1 is the instance of the class named MyClass

Get it?

Okay. Now, your question is convoluted to me. But I think what you're trying to ask is how to get one method to read values from another method. Well there's two different ways that are easy to use.

1) Use your self.
The self variable that you pass to every class method is very important, because it represents the object itself. So when you want something to 'stick', you can assign it to self like so:

class MyClass(object):
    def __init__(self):
        self.foo = 'Bar'
        self.bar = 'Foo'
    
    def random(self):
        self.bar = 'Foobar'

class_inst = MyClass()
print class_inst.foo, class_inst.bar # out -> Bar Foo
class_inst.random()
print class_inst.foo, class_inst.bar # out -> Bar Foobar

In the above example you can see that I use self to change the value of bar when the method random is called.

2) Return things
None of your methods return any values. I would assume that your random function means to return the variable output , so then at the end of your method you should use return output . Then when using the instance of your class you would do:

# my_inst is an instance of your class
result = my_inst.random()
# Now result contains the value of output from random

Now what do you do with result ? Well, you could pass it to your function decision . Modify the definition of that function to something like this: def decision(self, rand_output): , so that your usage would look like this (continuation of above code block)....

# my_inst is an instance of your class
result = my_inst.random()
# Now result contains the value of output from random
my_inst.decision( result )
# here you just passed result to the function decision
# Within the decision method you would do your operation
#  on the variable called rand_output (if you named it as above)

Do you understand my explanations?

Got it. Thanks for straightening out that terminonology.

I'm still a bit unsure of how to pass variables through a method. For example, if I had a random number generated in def random and wanted to pass it later to a method called def othermethod, how would I do that?


P.S. Can't wait to see the pens crush the wings tonight. My brothers, sister and dad are going to be there, got tickets for like 300 bucks a pop. I'm stuck in D.C.

Sorry I edited my above post to hopefully answer your question. I had prematurely hit 'Post' so that I could re-read your code and get function names...

Also, if anyone could recommend a better random number generator than the one from random array input, that would be really helpful.

Can you explain this question? What do you mean by "the one from random array input" ?

Aren't you just using import random ?

Oh and I just noticed where you have RockPaperScissors.tries -= 1 , which should be self.tries -= 1 . Where you have tries defined is the same thing as moving it into the __init__ and declaring it as self.tries = 3

Can you explain this question? What do you mean by "the one from random array input" ?

Aren't you just using import random ?

I'm using from RandomArray import

Is there a better/simpler import?

Thanks. I was able to get the code to compile with your advice. I used the return method. In standard programming, do you think there is a preferred way to do it between the two ways you proposed.

I think when dealing with classes it's more standard to use self. Whereas stand-alone functions would be the return method. But even still, all functions should return something, even if it's just stating return which is basically the same as omitting a return statement (as both return None ).

And yes, instead of from RandomArray import you should use import random

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.