Python Forum
Global variables not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Global variables not working
#4
(Jan-15-2021, 01:16 PM)hobbyist Wrote: So, you imply return variables of each function?

Let's start by saying you have SyntaxErrors in your code. Then, if it was just functions it's better to take arguments and then return the result.

Because it is a supposedly class (i.e. OOP) you can add and use attributes. But before that you need to fix the structure of your class (e.g. make the functions methods, not nested functions inside __init__() method. And if the functions don't need access to instance or class, you should ask yourself does it really belong to the class.

This makes it easier to debug and test your code, reuse it and so on.
(Jan-15-2021, 12:55 PM)hobbyist Wrote: Do I miss something here?
Also, what you have here is just the class definition, you never instantiate your class so that it execute (after you fix the multiple problems)

So this work, but it's BAD code and you should not do it:

sentence_1 = "hello"
sentence_2 = "hello"
 
class MyClass:
    def __init__(self, parent = None):
        # do something

        def function_A():
            # do something ...
            global sentence_1
            sentence_1 = "new string 1"
 
        def function_B():
            # do something ...
            global sentence_2
            sentence_2 = "new string 2"

        function_A()
        function_B()


spam = MyClass()
print(sentence_1)
print(sentence_2)
hobbyist likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
Global variables not working - by hobbyist - Jan-15-2021, 12:55 PM
RE: Global variables not working - by ndc85430 - Jan-15-2021, 01:04 PM
RE: Global variables not working - by hobbyist - Jan-15-2021, 01:16 PM
RE: Global variables not working - by buran - Jan-15-2021, 02:34 PM
RE: Global variables not working - by deanhystad - Jan-15-2021, 03:05 PM
RE: Global variables not working - by hobbyist - Jan-15-2021, 09:01 PM
RE: Global variables not working - by buran - Jan-15-2021, 03:16 PM
RE: Global variables not working - by ndc85430 - Jan-15-2021, 04:13 PM
RE: Global variables not working - by deanhystad - Jan-15-2021, 04:42 PM
RE: Global variables not working - by jefsummers - Jan-16-2021, 03:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand global variables 357mag 5 1,242 May-12-2023, 04:16 PM
Last Post: deanhystad
  Global variables or local accessible caslor 4 1,134 Jan-27-2023, 05:32 PM
Last Post: caslor
  global variables HeinKurz 3 1,242 Jan-17-2023, 06:58 PM
Last Post: HeinKurz
  Clarity on global variables JonWayn 2 1,018 Nov-26-2022, 12:10 PM
Last Post: JonWayn
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,600 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  Global vs. Local Variables Davy_Jones_XIV 4 2,737 Jan-06-2021, 10:22 PM
Last Post: Davy_Jones_XIV
  Global - local variables Motorhomer14 11 4,419 Dec-17-2020, 06:40 PM
Last Post: Motorhomer14
  Global not working why? Milfredo 13 4,682 Oct-06-2020, 02:37 AM
Last Post: Milfredo
  Question regarding local and global variables donmerch 12 5,288 Apr-12-2020, 03:58 PM
Last Post: TomToad
  local/global variables in functions abccba 6 3,557 Apr-08-2020, 06:01 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020