A small python question....

…regarding scope.

I noticed a bit of variance between tutorials/code snippets etc.

If I have a class inheriting from ShowBase, I can make a calls such as:




I can also make calls to base:


Though self does not apply:

self.base.... [ error ]

Although base seems to be the same as self:

self.camera.setPos(...)  [ works ]

So what’s the deal with Python and the self keyword here? To my understanding, a member variable would be declared via self.someVar …and later referenced via self.someVar too. Absence of self implies a local variable that drops out of scope post-method.

Why do I not need the self keyword for render?
Why do I not need the self keyword for base?

Could be me being a thick Python n00b, but thought I’d ask!


Instaniating showbase also generates a bunch of global variables that point to things inside your showbase class.

Things like render and camera

In other words

camera = base.camera
… etc.

Take a look at the Showbase.py in the direct/showbase directory


Though what’s the benefit of having these as globals? Kinda breaks all the rules in this Old Skool head of mine!


It certainly does break rules. This kind of builtin stuffing was done back when Panda was just an internal tool for a small group of people. It doesn’t make as much sense for a robust middleware graphics engine to do this, but now we’re stuck with it because too much existing code relies on it.

Panda3D 2.0 won’t do this kind of thing.


I was curious about this as well.

I notice a lot of example inherit directObject rather than showbase. Is the prefered method going forward not to use showbase at all?

That’s a stylistic thing. I don’t think it matters much whether you inherit from ShowBase or not; but you need to instantiate a ShowBase in order to open a window (unless you want to write all of the ShowBase code yourself); and you also need a place to put your own application-specific code, so it often makes sense to put it in class that inherits from ShowBase. But there’s no requirement that you do so.