How do you access a variable outside class in python?

class testing():
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

    def house(self):
        d = self.a+self.b+self.c
        print d

module="hello"
p = testing(1, 2, 3)
p.house()

How do I access module variable from within my testing class? I know I could just add it as a parameter to the class constructor by doing:

p=testing(1,2,3,module)

But I don't want to do that unless I have to. What other ways can I access module variable from inside the testing class?

How do you access a variable outside class in python?

kmario23

52.6k13 gold badges147 silver badges146 bronze badges

asked Feb 9, 2016 at 19:33

3

You simply reference it; you don't need any special global permission to access it. This isn't the best way, but since you haven't described your application and modularity requirements, about all we can do right now is to solve your immediate problem.

By the way, your a, b, c references are incorrect. See below.

class testing():
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c
        self.greeting = module

    def house(self):
        d = self.a + self.b + self.c
        print d
        print self.greeting

module="hello"
p = testing(1, 2, 3)
p.house()

Output:

6
hello

answered Feb 9, 2016 at 19:37

How do you access a variable outside class in python?

PrunePrune

75.7k14 gold badges56 silver badges76 bronze badges

1

You could use globals(). But I'm not sure if this is good idea at all.

class testing():
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

    def house(self):
        print(globals()['module'])
        d = self.a + self.b + self.c
        print(d)


module = 'here'
t = testing(1, 2, 3)
t.house()

Output:

# here
# 6

answered Feb 9, 2016 at 19:40

2

Maybe I don't understand the question, it already works since the global variable "module" is defined before you instantiated the class.

class testing():
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

    def house(self):
        d = self.a+self.b+self.c
        print module
        print d

module="hello"
p = testing(1, 2, 3)
p.house()

outputs:

hello

6

answered Feb 9, 2016 at 19:41

stevensteven

2281 silver badge13 bronze badges


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()

How do you access a variable outside class in python?

Updated on 06-Aug-2020 13:21:56

  • Related Questions & Answers
  • Explain the variables inside and outside of a class __init__() function in Python.
  • JavaScript variables declare outside or inside loop?
  • Create global variable in jQuery outside document.ready function?
  • Defining generic method inside Non-generic class in Java
  • Find elements inside forms and iframe using Java and Selenium WebDriver.
  • Command Line and Variable Arguments in Python?
  • Print Single and Multiple variable in Python?
  • Best practice for variable and method naming in Java
  • Using predefined class name as Class or Variable name in Java
  • Check if a point is inside, outside or on the ellipse in C++
  • Check if a point is inside, outside or on the parabola in C++
  • Select and Deselect Text Inside an Element using JavaScript
  • Explain final class and final method in PHP.
  • How to access the private methods of a class from outside of the class in Java?
  • How to call a function inside a jQuery plugin from outside?

How do you access a variable from the outside of a function?

To access a variable outside a function in JavaScript make your variable accessible from outside the function. First, declare it outside the function, then use it inside the function. You can't access variables declared inside a function from outside a function.

How can we access methods outside class using object in Python?

In Python, the class is like an object's constructor for creating the objects. Thus, the variables can be defined inside the class, outside the class, and inside the methods in Python. The variables defined outside the class can be accessed by any method or class by just writing the variable name.

How do you access local variables outside?

Variables declared inside a method have method level scope and can't be accessed outside the method. Note : Local variables don't exist after method's execution is over. The above code uses this keyword to differentiate between the local and class variables.

How do you access a private instance variable in Python?

In Python, there is no existence of “Private” instance variables that cannot be accessed except inside an object.