Need help with python coding

Hi everyone,
I’m having trouble with toggling gravity for certain objects. As I don’t know pretty much anything about python language, I was trying to change some lines of code and in the end tryed using Logic Nodes but failed. I’m thankful to BluePrintRandom for the code he provided for me, but I didin’t want to bother him too much with my questions.
So short story long, I have gravity in one planet, it works quite good, yet the other planet is nearby with same property gravity doesn’t work on it, even if i get near it ( like bump into it ). So all in all I’m looking maybe someone can post a sample script to toggle gravity on per-say when “player” is near a planet, and toggle it off when “player” is further away. Yet it needs to work not in whole universe lets say, just if you’re near enough to the planet. (I’m talking about when the gravity is toggled on so it doesn’t pull everything from whole map to itself )

Ok, after reviewing what I just wrote, I will be happy even with pointers to lead me to right direction, as I’m asking a tad bit too much from you guys.

Thank you in advance,
P1r0d0x

if this is for the game engine, you posted in the wrong section, and you wont get as much help. you can report the post to get it moved to game engine support section if need be.

## for objects with physics ##

if owner["planet"] == "":
    for planet in scene.objects:
        if owner.getDistanceTo(planet) < planet["radius"]:
            owner["planet"] = planet.name
## for planets ##

for object in scene.objects:
    if object["planet"] == owner.name:
        dist = owner.getDistanceTo(object)
        if dist > owner["radius"]:
            object["planet"] = ""

i dont know what the gravity code looks like, but im sure this can be partially merged in to reduce the number of times getDistanceTo() is run.