-4

Is there a way to give classes access to global variables in python without passing it as a parameter? The following is what I want to do:

# Global
data = []

# Class
Foo:
    def __init__(self):
        self.name = data[0]

# Main
data = ["name", "name2"]
f = Foo()
Aginor
  • 178
  • 5
  • 18
  • 2
    Possible duplicate of [How can I access global variable inside class in Python](http://stackoverflow.com/questions/10814452/how-can-i-access-global-variable-inside-class-in-python) – Riftus Oct 14 '15 at 20:38
  • It will work if you fix it to use valid syntax. – BrenBarn Oct 14 '15 at 20:41
  • Riftus not really a duplicate; in that case, the user was creating a new variable with the same name; here, as BrenBarn notes, this works fine if you properly do class Foo: and def __init__ ... however still voting to close as this is a typo – Foon Oct 14 '15 at 20:58

1 Answers1

0

I just typed that pseudo code up really quickly, that's why there was the def error. My issue was that I didn't declare data as a global within the function.

Aginor
  • 178
  • 5
  • 18