Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python - Using variable outside and inside the class and method
Python is an object-oriented programming language where variables can be defined at different scopes. Understanding variable scope is crucial for writing clean, maintainable code. Variables can be defined outside classes (global scope), inside classes (class scope), or inside methods (local scope).
Variables Defined Outside the Class (Global Variables)
Variables defined outside any class or function have global scope and can be accessed from anywhere in the program ?
# Variable defined outside the class (global scope)
outVar = 'outside_class'
print("Global access:", outVar)
# Class one
class Ctest:
print("Inside class:", outVar)
def access_method(self):
print("Inside method:", outVar)
# Creating object and calling method
obj1 = Ctest()
obj1.access_method()
# Class two
class AnotherCtest:
print("Inside another class:", outVar)
def another_access_method(self):
print("Inside another method:", outVar)
# Creating object and calling method
obj2 = AnotherCtest()
obj2.another_access_method()
The output of the above code is ?
Global access: outside_class Inside class: outside_class Inside another class: outside_class Inside method: outside_class Inside another method: outside_class
Variables Defined Inside Methods (Local Variables)
Variables defined inside a method have local scope and can only be accessed within that specific method ?
class Ctest:
def access_method(self):
# Variable defined inside the method (local scope)
inVar = 'inside_method'
print("Local variable:", inVar)
def another_method(self):
# This will cause an error if we try to access inVar
try:
print("Trying to access inVar:", inVar)
except NameError as e:
print("Error:", e)
# Creating object and testing
obj = Ctest()
obj.access_method()
obj.another_method()
The output of the above code is ?
Local variable: inside_method Error: name 'inVar' is not defined
Variables Defined Inside Classes (Class Variables)
Variables defined directly inside a class (but outside methods) are class variables shared by all instances ?
class Student:
# Class variable (shared by all instances)
school_name = "Python Academy"
def __init__(self, name):
# Instance variable (unique to each instance)
self.name = name
def display_info(self):
print(f"Student: {self.name}")
print(f"School: {Student.school_name}")
# Creating multiple instances
student1 = Student("Alice")
student2 = Student("Bob")
student1.display_info()
print()
student2.display_info()
The output of the above code is ?
Student: Alice School: Python Academy Student: Bob School: Python Academy
Variable Scope Summary
| Scope Type | Definition Location | Access Level |
|---|---|---|
| Global | Outside all classes/functions | Accessible everywhere |
| Class | Inside class, outside methods | Shared by all instances |
| Local | Inside methods | Only within that method |
Conclusion
Global variables can be accessed from anywhere in the program, while local variables are confined to their method scope. Class variables provide a way to share data between all instances of a class.
