Do you understand what global does? Do you understand the concept of scope?
You can think of scope as a kind of dictionary that stores names. When you use a variable Python searches for the name in these scope dictionaries. Every function has a scope. Every module (file) has a scope. Python has a scope. The scopes even have names; Local for the function scope, Global for the module scope, and Built-in for the Python scope. There is an additional scope called Enclosing which occurs if you define functions inside of functions.
When you use a variable in your code Python searches for the variable in these scopes. The order of the search is Local, Enclosing, Global, Built-in. If you read about scope you may see this order referred to as LEGB. This is why ndc85430 asked why you are using the global keyword to use a mutable variable. Global variables are visible for use in any function defined in the module. However ndc85430 is wrong about "sentence_1" being mutable. "sentence_1" is a string, and strings are not mutable in Python.
When you assign a variable in your code Python creates the variable in the current scope. If your function uses the "global" keyword to declare the variable, the assignment creates the variable in the global scope. Your code would work if you got the syntax correct.
1 2 3 4 |
def function_A:
global sentence_1
sentence_1 = "new string 1"
|
You would know this if you ever bothered to read the Python documents before using new language features. If all your learning is focused on tutorials or looking at examples you risk never learning about how things work and why they work this way. The Python documents explain how a language feature fits into the Python puzzle. If you start reading the docs not only will you have fewer questions, but you will get a deeper understanding of Python and grow an intuition about why something is not working.
buran's suggestion about using attributes is a good one. In a class not only can you think of a scope as being a dictionary, it is a dictionary in the literal sense. An attribute is a variable defined in the context of the class. You use a special syntax to access these variables. From inside the class you use "self.name" to access the "name" attribute. From outside the class you use "instance.name" where "instance" is the name of a variable that references an instance of the class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
class MyClass:
def __init__( self ):
self .sentence_1 = "Hello"
self .sentence_2 = "World"
def function_A( self ):
self .sentence_1 = "Goodnight"
def function_B( self ):
self .sentence_2 = "moon"
def __repr__( self ):
return f '{self.sentence_1} {self.sentence_2}!'
a = MyClass()
print (a)
print (a.sentence_1)
print (a.sentence_2)
a.sentence_2 = 'sun'
print (a)
a.function_A()
a.function_B()
print (a)
|
The most useful thing about classes is that each instance gets their own namespace that they carry around wherever they go. If I create another instance of MyClass and assigned to the variable "b", I now have two instances of the class that are independent of each other. Changing attributes in "a" does not affect the corresponding attribute in "b". This is something that would be very difficult to do using just functions and modules.
Output:
Hello World!
Hello
World
Hello sun!
Goodnight moon!