How to use a global variable inside the initialiser of a class in Python 3?
for eg
import urllib
from urllib.request import urlopen
class ABC(object):
def __init__(self):
x=urlopen('http://www.google.com/').read()
How do I convert x into a global variable?
You have to declare your variable before the class declaration and use the global statement on the init function:
import urllib
from urllib.request import urlopen
x = None
class ABC(object):
def __init__(self):
global x
x=urlopen('http://www.google.com/').read()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With