- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python - Using variable outside and inside the class and method
Python is an object-oriented programming language. Almost everything in Python is an object, with its properties and methods. A Class is like an object constructor, or a "blueprint" for creating objects.
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.
Example
# defined outside the class' # Variable defined outside the class. outVar = 'outside_class' print("Outside_class1", outVar) ''' Class one ''' class Ctest: print("Outside_class2", outVar) def access_method(self): print("Outside_class3", outVar) # Calling method by creating object uac = Ctest() uac.access_method() ''' Class two ''' class Another_ Ctest_class: print("Outside_class4", outVar) def another_access_method(self): print("Outside_class5", outVar) # Calling method by creating object uaac = Another_ Ctest_class() uaac.another_access_method() The variables that are defined inside the methods can be accessed within that method only by simply using the variable name. # defined inside the method' '''class one''' class Ctest: print() def access_method(self): # Variable defined inside the method. inVar = 'inside_method' print("Inside_method3", inVar) uac = Ctest() uac.access_method() '''class two''' class AnotherCtest: print() def access_method(self): print() uaac = AnotherCtest() uaac.access_method()
- Related Articles
- Explain the variables inside and outside of a class __init__() function in Python.
- Defining generic method inside Non-generic class in Java
- JavaScript variables declare outside or inside loop?
- Using predefined class name as Class or Variable name in Java
- Create global variable in jQuery outside document.ready function?
- Class method vs static method in Python
- Checking table existence using Class and it’s method in SE11 without using FM in ABAP
- Using a JavaScript object inside a static() method?
- Explain Python class method chaining
- Best practice for variable and method naming in Java
- Write the names of the parts of a plant from outside to inside.
- Many magazines and newspapers talk about the possibility of life outside the Earth. Read these articles and have a discussion in the class about what could be defined as life outside Earth.
- Command Line and Variable Arguments in Python?
- Print Single and Multiple variable in Python?
- How do I declare a global variable in Python class?

Advertisements