[Solved] Global variables, member variables and instance variables in Python [closed]


You have asked a lot of questions under one! I will try to answer each of those 🙂

Variables – global vs local

global variables are those ones whose scope is global, i.e. available in entire program.

local variables by contrast are those ones which resides in one particular scope, a function, inside a loop, inside a particular condition etc.

Here is the example:

glb_var_1 = "some value"
glb_var_2 = "some other value"

def f1():
   local_var_1 = "this is local inside f1"

   print("f1(): We can use here all variables defined locally, i.e. inside f1():", local_var_1)
   print("f1(): We can use global var anywhere in program:", glb_var_1)

def f2():
   local_var_1 = "this is local inside f2"
   glb_var_1   = "this is local variable which is hiding global var of same name"

   print("f2(): locally inside f2():", local_var_1)
   print("f2(): same name as global var, but local to f2():", glb_var_1)

def f3():
    print("f3(): global var value would be one defined globally, not the one as defined in f2():", glb_var_1)

Infact, in Python there are functions which gives you list of all global variables and local variables defined.
Those functions are:

globals() and locals() respectively

member variables

member variables are simply those variables which are defined inside a class.
These variables can be used anywhere within the class’s methods or from class’s object name.

Below example would make this clear:

class SomeClass(object):

    def __init__(self):
        self.member_var_1 = "some value"
        self.member_var_2 = "some other value"

    def method1(self):
        print("Could use any member variables here:", self.member_var_1)
        self.member_var_1 = "new value"

    def method2(self):
        print("If this method is called after method1, we will get new value:", self.member_var_1)

# create object/instance of SomeClass
obj = SomeClass()

# could also use member variables like below
obj.member_var_1 = "new value modified outside method"

# similar to how we call the method of a class
obj.method2()

class variables vs instance variables

The member variables which we saw in above code listing, are nothing but instance variables.
class variables are those variables which are available at class level. What this mean is, it would be same for all instances of that class.
We could also use class variable irrespective of any class instance.

Following example would explain it better:

class SomeClass(object):

    class_var_1 = "some value"
    class_var_2 = "some other value"

    def some_method(self):
        print("Could use all class members, but need to use special syntax:", SomeClass.class_var_1)

print("Outside class method, no instance created, still can use class member:",  SomeClass.class_var_1)
obj = SomeClass()
obj.some_method()

I hope you got the idea now 🙂

solved Global variables, member variables and instance variables in Python [closed]