SkylineGodzilla SkylineGodzilla - 1 year ago 108
Python Question

Python How to use global verables and DRY

So I have finally got my base class set up nicely and ready to build a class to test. However when I started buildign this I noticed that I had to repeat myself Lot with setting everything to global

the start() event is called to set up everything
Update() is called every tick (20 ticks a sec)

Is there a more efficient way to use global venerable then this.

class BodyClass(BaseClass):
def __init__(self, name):

def Start(self):
global i
i = 0
global d
d = True
global obj1
obj1 = ChildClassA('Obj1')
global obj2
obj2 = ChildClassB('Obj2')
global obj3
obj3 = ChildClassC('Obj3')
global obj4
obj4 = ChildClassB('Obj4')
global obj5
obj5 = ChildClassB('Obj5')
global obj6
obj6 = ChildClassB('Obj6')

def Update(self):

global i
global d
global obj1
global obj2
global obj3
global obj4
global obj5
global obj6

print ("TYPE:", obj3.CType())
i = i+ 1
print("i: ", i)
if i >= 10:
if d:


Answer Source

Replace global variables


 global obj1;
 obj1 = ChildClassA('Obj1');  => self.obj1 = ChildClassA('Obj1');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download