SkylineGodzilla SkylineGodzilla - 2 months ago 14
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):
super().__init__(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
time.sleep(0.05)
print("i: ", i)
if i >= 10:
if d:
print("UNLOADING")
obj2.Unload()
obj4.Unload()
obj5.Unload()
obj6.Unload()

d=False

Answer

Replace global variables

example:

 global obj1;
 obj1 = ChildClassA('Obj1');  => self.obj1 = ChildClassA('Obj1');
Comments