Accessing another classes variables

I am working with Panda3d 1.7 and Python 2.5. In my code, I have a world class, that, inside of it, are the player, and some test objects that I am using to test parts of code. In one of them, I am working on the targeting system, and I need to access the player.root (which is a Node, which represents the center of the ship) to set the target. How would I access it every frame?


I don’t think I understand what you are asking for, but I’ll take two sporting cracks at it:

  1. “How do I access instance variables in Python?” – If player is a variable of your World class, and your code is a method in World, you can use self.player.root to access the node. In Python, instance variables are unprotected, so you can access them directly from any other class.

  2. “How do I do something every frame?” – Use a Task.

If neither of those answer your question, I’m afraid you’ll need to explain it a bit further.

I dont think I understand the post either, but from the topic title:

  1. There are class variables and instance variables.
    To access them outside of the class/instance, you can do this:

If you have an instance of a class inside an instance of another class and want to access it outside of the instances, it will be like this:

import direct.directbase.DirectStart

class World():
    def __init__(self):


Do you mean something like this??

Almost, only if you make it:

ff = World()

Right…it was an old code and I forgot to change that (ss) to (World)…Thanks

Wow, thanks for trying so hard, but you all missed. (sorry that sounds sarcastic, none intended)
Here’s what I mean:

class player:
     def __init__(self):
          self.root = render.attachNewNode(etc)

class World:
     def __init__(self):
          self.player = player()
 = 1234

What I want to know is how I would access from inside world.player.
Now that I’ve written this out, I think I know. I would just make master an argument of player, and pass self as the argument inside world. If this is right what would I make it an argument of though? init? but then wouldn’t it only be accessible inside init?

You can do it like this:

class Player:
    def __init__(self, world): = world

    def eat_banana(self): -= 1

    def count_bananas(self):
        if > 0:
            print "there are",, "bananas in my world"
            print "all out of bananas, surely i will starve to death"

class World:
    def __init__(self):
        self.bananas = 24
        self.player = Player(self)

I recommend writing your class names starting with a capital letter, this will help prevent any confusion later on.

Ok thank you,
I normally do write my classes like that, it was just an example.
One last question, is just a pointer to the world class, it doesn’t create a new world class right?
Thanks again guys for all the help,

That is correct, it is just a reference to the instance of world that was passed to init.
Creating a new instance of the World class would look like = World()

Don’t mix “class” and “instance”.
A class is an abstract original.
Think of it as of a car. You know what a car does and how it might look like but you can’t point at “car”.
If you say “my car” or “the car over there” then you also have a “car”, but a specific one. This would be an instance.

Creating instances is done in Python like this:

mysinstance = Classname()

Inside “Classname”, i mean the class, you can describe how the class and its methods interact with future instances by writing “self”, which is a placeholder for any instance.
That said, from the above line “myinstance” could be seen the same as “self” depending on context (inside or outside the class).

Without the finishing brackets, the variable to the left would get a reference to the class, not to a just created object/instance:

myclass = Classname

I suggest you read on the web about object oriented programming in general (OOP). It’s kind of the base of Python.