Use Global Variables in a Class

How can I access global variable inside class in Python

By declaring it global inside the function that accesses it:

g_c = 0

class TestClass():
def run(self):
global g_c
for i in range(10):
g_c = 1
print(g_c)

The Python documentation says this, about the global statement:

The global statement is a declaration which holds for the entire current code block.

How to define global variable in class in python

If you just want to define your variable outside of the class you don't need to use the global keyword unless you plan on modifying it. If you just want to read the variable and not modify it you could just do something like.

ask = input("What type you are writing? (absolute value and phase angle or real and imaginary parts)")

class ComplexMethods:
if ask == "real and imaginary parts":
pass

if ask == "real and imaginary parts":
firstcomplexreal = float(input("Enter real part of first complex number: "))
firstcompleximaginary = float(input("Enter imaginary part of first complex number: "))
secondcomplexreal = float(input("Enter real part of second complex number: "))
secondcompleximaginary = float(input("Enter imaginary part of second complex number: "))
complexnumbers = ComplexMethods(firstcomplexreal, firstcompleximaginary, secondcomplexreal,
secondcompleximaginary)

Global variables in Java

To define Global Variable you can make use of static Keyword

public class Example {
public static int a;
public static int b;
}

now you can access a and b from anywhere
by calling

Example.a;

Example.b;

can global variables be accessed and modified in various classes in C++

Please learn more about

  • Declare vs Define in C and C++.
  • compile vs link

define global variable

// file1.cpp
int data_received;

extern tell complier that data_received can be found when linker.

// file2.cpp
extern int data_received;

in addition, static can limit my_global_var only to be used in file defining it. example

// file3.cpp
static int my_global_var = 1;

Error will be occured in linker

// file4.cpp
extern int my_global_var;

PHP class: Global variable as property in class

You probably don't really want to be doing this, as it's going to be a nightmare to debug, but it seems to be possible. The key is the part where you assign by reference in the constructor.

$GLOBALS = array(
'MyNumber' => 1
);

class Foo {
protected $glob;

public function __construct() {
global $GLOBALS;
$this->glob =& $GLOBALS;
}

public function getGlob() {
return $this->glob['MyNumber'];
}
}

$f = new Foo;

echo $f->getGlob() . "\n";
$GLOBALS['MyNumber'] = 2;
echo $f->getGlob() . "\n";

The output will be

1
2

which indicates that it's being assigned by reference, not value.

As I said, it will be a nightmare to debug, so you really shouldn't do this. Have a read through the wikipedia article on encapsulation; basically, your object should ideally manage its own data and the methods in which that data is modified; even public properties are generally, IMHO, a bad idea.



Related Topics



Leave a reply



Submit