Author Topic: Python Newbie Question - Calling a class' variable from another class  (Read 1576 times)

0 Members and 1 Guest are viewing this topic.

Offline Locutus of Borg

  • 28
  • Who counted those posts?????????????
Python Newbie Question - Calling a class' variable from another class
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?
We are the Borg
We will add your biological and technological distinctiveness to our own

Resistance is FUTILE

 

Offline The E

  • He's Ebeneezer Goode
  • Global Moderator
  • 213
  • Nothing personal, just tech support.
    • Steam
    • Twitter
Re: Python Newbie Question - Calling a class' variable from another class
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.
Let there be light
Let there be moon
Let there be stars and let there be you
Let there be monsters and let there be pain
Let us begin to feel again
--Devin Townsend, Genesis

 

Offline Locutus of Borg

  • 28
  • Who counted those posts?????????????
Re: Python Newbie Question - Calling a class' variable from another class
I declared it as self.Position and it still didn't work
We are the Borg
We will add your biological and technological distinctiveness to our own

Resistance is FUTILE

 

Offline Spicious

  • Master Chief John-158
  • 210
Re: Python Newbie Question - Calling a class' variable from another class
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?

 

Offline Locutus of Borg

  • 28
  • Who counted those posts?????????????
Re: Python Newbie Question - Calling a class' variable from another class
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

We are the Borg
We will add your biological and technological distinctiveness to our own

Resistance is FUTILE

 

Offline Spicious

  • Master Chief John-158
  • 210
Re: Python Newbie Question - Calling a class' variable from another class
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)

 

Offline Locutus of Borg

  • 28
  • Who counted those posts?????????????
Re: Python Newbie Question - Calling a class' variable from another class
TypeError: unbound method coinFlip() must be called with Coin instance as first argument (got int instance instead)
We are the Borg
We will add your biological and technological distinctiveness to our own

Resistance is FUTILE

 

Offline Spicious

  • Master Chief John-158
  • 210
Re: Python Newbie Question - Calling a class' variable from another class
Is coin a Coin?

If you want more help, post your code.