0

I have a code with a class using many variables.i can't Access one of those variables to use it inside of a method of my code written in Python.

I tried to declare the variable as global to make the value being accessible at any point of the code but it is not ok. I am expecting to use the variable in a specific method

LIMS
  • 1
  • 1
  • 1
    Make it an instance variable of the object. Classes with lots of variables are bad, but globals are worse. – Samwise Oct 29 '22 at 17:24
  • Does this answer your question? [Python nonlocal statement / keyword](https://stackoverflow.com/questions/1261875/python-nonlocal-statement-keyword) – ti7 Oct 29 '22 at 17:31
  • I have tried that but not ok. Pls can we discuss by chat or by email for your support on this code? I tried to put the snapping of the part i have issue but not possible to do so – LIMS Oct 30 '22 at 16:34

3 Answers3

0

In Python, "variables" are really "names" and you can access them whenever they're in scope!

However, if you intend to modify a name that's outside of the current scope, you need to tell the interpreter so (otherwise it'll assume that you mean to give the name to something else within your scope)

a = None

def foo():
    # you can access a, but not set it here
    print(a)
a = None

def foo():
    a = 1  # a is only set within foo

print(a)  # refers to a in outer scope

usage of global

a = None

def foo():
    global a
    # now you can modify a
    a = 1

creates an error

a = None

def foo():
    print(a)
    global a  # SyntaxError: name 'a' is used prior to global declaration
ti7
  • 16,375
  • 6
  • 40
  • 68
  • It's better to use "nonlocal" instead of "global". – Andrii Dubonos Oct 29 '22 at 18:08
  • @AndriiDubonos [certainly in many cases](https://stackoverflow.com/questions/74247350/is-there-a-simple-and-efficient-way-to-use-a-global-variable-into-a-method-of-a/74247397?noredirect=1#comment131084457_74247350), or better still to collect large amounts of globally-accessed names into a shared object (often simply a `dict`)! – ti7 Oct 29 '22 at 18:13
0

There is no easy way. Use (global a) when you are changing the variable in the function.

For example when you are changing the variable in a function.

a = 0

def asi():
    global a
    a = 1

Output : 1

For example when you are not changing the variable but want to use it in a function.

a = 0

def asi():
    print(a)

Output : 0

  • Pls can we discuss by chat or by email for your support on this code? I tried to put the snapping of the part i have issue but not possible to do so – LIMS Oct 30 '22 at 16:35
0

your question is not clear to me:

  1. you didn't share any code for interpretation
  2. in OOP ( us said class and methods ) how could you not be able to access variable (global variable or class variable: both may not same but its for sake of argument)
  3. by 'in my code written in python' what do you mean actually? with in same class? method? module? venv?

Note:- every question may not have answer in coding it may be literature... ( that whats logic is.....)

  • Hello Abdul. Pls what is the way to insert the code here for you guys to see it and support? Or can i have a email address of someone that i can discuss with the person personally – LIMS Oct 30 '22 at 06:11