-1

I am trying to find the best way of accessing an instance variable from another instance. So far I've been able to pass that variable as an argument and save it in the new instance. But I am wondering if there is some sort of "Global" variable that will work best. Specially if the classes are from different modules.

Here is my example:

class A(object):
    def __init__(self):
        self.globalObject = "Global Object"
        self.listB = self.generateBList()


    def generateBList(self):
        return [B(self.globalObject, i) for i in range(10)]
    


class B(object):
    def __init__(self, globalObject, index):
        self.index = index
        self.globalObject = globalObject

    def splitGlobalObject(self):
        return self.globalObject.split(" ")


a = A()
firstB = a.listB[0]
print firstB.splitGlobalObject()

Here when I generateBList() I need to pass always that globalObject as an argument B(self.globalObject, i), and then this object gets saved into B.globalObject, butif i had many classes that needed to access that global object im not sure if passing it always as an argument would be the best option. What would be the best way of accessing it without having to pass it always as an argument when you create instances?

I hope I explained my way properly.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Alvaro Bataller
  • 487
  • 8
  • 29

2 Answers2

0

It's possible to use global variables in python.

class A(object):
    def __init__(self):
        global globalObject
        globalObject = "Global Object"
        self.listB = self.generateBList()


    def generateBList(self):
        return [B(i) for i in range(10)]



class B(object):
    def __init__(self, index):
        self.index = index

    def splitGlobalObject(self):
        return globalObject.split(" ")

Usually you want to avoid globals. Visit http://www.python-kurs.eu/python3_global_lokal.php for more examples.

Patrick M
  • 1
  • 2
  • Maybe one better option would be putting `globalObject` in the module directly (eg. outside class A) - obviously that depends on what you plan to do with `globalObject`. – rcmachado Jun 30 '16 at 01:52
  • This works but the problem is that my classes might be located in different modules, and they all have to access the same instance "Global Object" – Alvaro Bataller Jun 30 '16 at 01:58
  • 1
    In that case you could put your global object in a third module and have any modules that need to make use of it import that third module. – alphaloop Jun 30 '16 at 02:01
0

Your example seems unnecessarily complicated, so I'll try to illustrate one way I've used before that I think may do what you want. If you think of creating a "world" that is the stage for what you want to happen, you can have each class instance inside the world, know the world. Like this:

class Thing(object):
    def __init__self(self, world, name):
        self.world = world
        self.name = name

class Word(object):
    def __init__(self):
        self.everyone = [Thing(self, i) for i in range(10)]

if __name__ == '__main__':
    world = World()

In this example, the World class instance carries around an attribute called everyone that is a list of ten Thing objects. More importantly for your example, each instance of Thing now carries around a pointer called self.world that points to the world class. So all Things can access all other Things via self.world.everyone, as well as anything else in the world. I also passed i into each Things init so they have a unique name in the form of an integer between 0 and 9, but that may be extra for what you need.

From here there's basically nothing that your instances can't do to each other via methods, and all without using lots of globals.

Edit: I should add that being from different modules will make no difference here, just import as many as you want and create instances of them that pass knowledge of the World instance into them. Or obviously tailor the structure to your needs while using the same idea.

Jeff
  • 2,158
  • 1
  • 16
  • 29
  • 1
    If classes are saved on other modules i used alphaloop's comment, I also found a similar example here: http://stackoverflow.com/questions/13034496/using-global-variables-between-files-in-python – Alvaro Bataller Jun 30 '16 at 02:17