Python variable scope with working code example

Published: 01 January 1970
on channel: Prem Aseem Jain
18
0

Here is the code:
We learn about scopes of the variable in python:
which are local, instance, class, global

my_var = "global level"

class MyClass:
my_var = "class level"

def __init__(self,val):
self.my_var = val

def set_var(self, val):
global my_var
my_var = val

def get_var(self):
my_var = "local"
global my_var
return my_var

def func(self):
for i in range(5):
print(i)



def method():
global my_var
my_var = "prem"
print(my_var)

method()
print(MyClass.my_var)
obj = MyClass("instance level")
obj.set_var("global")
print(my_var)
print(obj.get_var())


On this page of the site you can watch the video online Python variable scope with working code example with a duration of hours minute second in good quality, which was uploaded by the user Prem Aseem Jain 01 January 1970, share the link with friends and acquaintances, this video has already been watched 18 times on youtube and it was liked by 0 viewers. Enjoy your viewing!