Python | Using variable outside and inside the class and method
Last Updated :
18 May, 2020
In Python, we can define the variable outside the class, inside the class, and even inside the methods. Let's see, how to use and access these variables throughout the program.
Variable defined outside the class:
The variables that are defined outside the class can be accessed by any class or any methods in the class by just writing the variable name.
Python3
# Program to demonstrate 'Variable
# defined outside the class'
# Variable defined outside the class.
outVar = 'outside_class'
print("Outside_class1", outVar)
''' Class one '''
class Geek:
print("Outside_class2", outVar)
def access_method(self):
print("Outside_class3", outVar)
# Calling method by creating object
uac = Geek()
uac.access_method()
''' Class two '''
class Another_Geek_class:
print("Outside_class4", outVar)
def another_access_method(self):
print("Outside_class5", outVar)
# Calling method by creating object
uaac = Another_Geek_class()
uaac.another_access_method()
Output:
Outside_class1 outside_class
Outside_class2 outside_class
Outside_class3 outside_class
Outside_class4 outside_class
Outside_class5 outside_class
Variable defined inside the class:
The variables that are defined inside the class but outside the method can be accessed within the class(all methods included) using the instance of a class. For Example - self.var_name.
If you want to use that variable even outside the class, you must declared that variable as a
global. Then the variable can be accessed using its name inside and outside the class and not using the instance of the class.
Python3
# Program to demonstrate 'Variable
# defined inside the class'
# print("Inside_class1", inVar) # Error
''' Class one'''
class Geek:
# Variable defined inside the class.
inVar = 'inside_class'
print("Inside_class2", inVar)
def access_method(self):
print("Inside_class3", self.inVar)
uac = Geek()
uac.access_method()
''' Class two '''
class another_Geek_class:
print()
# print("Inside_class4", inVar) # Error
def another_access_method(self):
print()
# print("Inside_class5", inVar) # Error
uaac = another_Geek_class()
uaac.another_access_method()
Output:
Inside_class2 inside_class
Inside_class3 inside_class
The statements which are marked as
error will produce an error upon execution as the variable is not accessible there.
Variable defined inside the method:
The variables that are defined inside the methods can be accessed within that method only by simply using the variable name. Example - var_name.
If you want to use that variable outside the method or class, you have to declared that variable as a
global.
Python3
# Program to demonstrate 'Variable
# defined inside the method'
# print("Inside_method1", inVar) # Error
'''class one'''
class Geek:
print()
# print("Inside_method2", inVar) # Error
def access_method(self):
# Variable defined inside the method.
inVar = 'inside_method'
print("Inside_method3", inVar)
uac = Geek()
uac.access_method()
'''class two'''
class AnotherGeek:
print()
# print("Inside_method4", inVar) # Error
def access_method(self):
print()
# print("Inside_method5", inVar) # Error
uaac = AnotherGeek()
uaac.access_method()
Output:
Inside_method3 inside_method
The statements which are marked as error will produce error upon execution as the variable is not accessible there.
Summary:
Similar Reads
Accessing Python Function Variable Outside the Function In Python, function variables have local scope and cannot be accessed directly from outside. However, their values can still be retrieved indirectly. For example, if a function defines var = 42, it remains inaccessible externally unless retrieved indirectly.Returning the VariableThe most efficient w
4 min read
Variables under the hood in Python In simple terms, variables are names attached to particular objects in Python. To create a variable, you just need to assign a value and then start using it. The assignment is done with a single equals sign (=): Python3 # Variable named age age = 20 print(age) # Variable named id_number id_no = 4067
3 min read
Python | Accessing variable value from code scope Sometimes, we just need to access a variable other than the usual way of accessing by it's name. There are many method by which a variable can be accessed from the code scope. These are by default dictionaries that are created and which keep the variable values as dictionary key-value pair. Let's ta
3 min read
How to Call a Method on a Class Without Instantiating it in Python? In Python programming, classes and methods are like building blocks for organizing our code and making it efficient. Usually, we create instances of classes to access their partsâattributes and methods. But sometimes, we might want to use a class or its methods without actually creating an instance.
3 min read
How To Make a Subclass from a Super Class In Python In Python, you can create a subclass from a superclass using the class keyword, and by specifying the superclass in parentheses after the subclass name. The subclass inherits attributes and behaviors from the superclass, allowing you to extend or modify its functionality while reusing the existing c
3 min read