Python OOP: passing an object as parameter

I have a python file I’m trying to add to the BGE, it contains (more or less) the following:


class objectOne:
    posX = 0
    posY = 0

(constructor and some function defs follow)


class objectTwo:
(some params, constructor)
    def rememberObject(object,location)
        x = object.posX
        y = object.posY    

As you can see I’d like to pass an object of the class objectOne to the rememberObject method of objectTwo. However, when I try to do this, the line

x = object.posX

gives the following error:

AttributeError: ‘objectTwo’ object has no attribute ‘posX’
I’ve been trying for hours and searching everywhere but still can’t figure out how to do this properly.
Any hints in the right direction MUCH appreciated!

Oh, sudden bright idea:

Adding “self” to the function definition parameter list fixed it


def rememberObject(<b>self</b>,object,location)         
    x = object.posX         
    y = object.posY

But still I’d like to know why it works like that, it’s not really clear from the Python documentation/tutorials :X

You can use Inheritance: http://www.sthurlow.com/python/lesson08/

the first parameter of a member function is reserved for the object instance the function is called from. So the first parameter can’t be specified when you call that member function as it is implicitly set to the object.
Usually you call this parameter “self” so that you always know that this is gonna be the parameter you can use to access the object itself. But in fact you could call it anything you like (like in your case “object”).