Hard Light Productions Forums

Off-Topic Discussion => Programming => Topic started by: Locutus of Borg on July 02, 2011, 07:38:46 am

Title: Python Newbie Question - Calling a class' variable from another class
Post by: Locutus of Borg on July 02, 2011, 07:38:46 am
Here's basically how I have it set up

I have a class called World, which has a function called somethingPicked

somethingPicked defines a variable, Position, and then sends a message to the Coin class to execute a function flipCoin

in flipCoin I need to compare the variable Position to the variable r1, which is defined in the coin class.

How do I write it so that I can use the variable position from the World class in a comparison with the variable r1 from the coin class?
Title: Re: Python Newbie Question - Calling a class' variable from another class
Post by: The E on July 02, 2011, 07:47:32 am
DISCLAIMER: I do not know Python

Unless World.Position is publicly accessible, you can't. In most object-oriented languages, you need to declare it as being publicly accessible. See http://en.wikipedia.org/wiki/Object-oriented_programming for pointers.
Title: Re: Python Newbie Question - Calling a class' variable from another class
Post by: Locutus of Borg on July 02, 2011, 12:24:22 pm
I declared it as self.Position and it still didn't work
Title: Re: Python Newbie Question - Calling a class' variable from another class
Post by: Spicious on July 02, 2011, 05:24:04 pm
In Python everything is publicly accessible.

Are you passing your World instance to your flipCoin call? If you don't need anything else from World, why not just pass Position?
Title: Re: Python Newbie Question - Calling a class' variable from another class
Post by: Locutus of Borg on July 02, 2011, 06:06:16 pm
I'm not that familiar with Python. Could you please explain that? :S

I tried calling the variable World.somethingPicked.Position and that didn't work either

Title: Re: Python Newbie Question - Calling a class' variable from another class
Post by: Spicious on July 02, 2011, 06:53:03 pm
Make your flipCoin take a position:
Code: [Select]
def flipCoin(self, position):
  ...

and in somethingPickled
Code: [Select]
coin.flipCoin(position)
or if position is a field in World
Code: [Select]
coin.flipCoin(self.position)
Title: Re: Python Newbie Question - Calling a class' variable from another class
Post by: Locutus of Borg on July 02, 2011, 07:27:03 pm
TypeError: unbound method coinFlip() must be called with Coin instance as first argument (got int instance instead)
Title: Re: Python Newbie Question - Calling a class' variable from another class
Post by: Spicious on July 02, 2011, 07:39:12 pm
Is coin a Coin?

If you want more help, post your code.