0

Using pandas==1.2.1

MRE:

data = "aaaa"
def JoinStr(a):
    return data + a

JoinStr("sd")

this outputs "aaaasd"

But from my understanding function doesn't know what data is unless it is given it, why is this possible in jupyter notebook? all data assigned in jupyter notebook(or is this for all python) work as global variable?

haneulkim
  • 4,406
  • 9
  • 38
  • 80

1 Answers1

0

In Python (with or without Jupyter) variables are looked for in the local space and, if there's no match, in the global space.

A function like yours can see the global value of data, unless you have a local variable of data as well.

If your function wants to modify the value of data, you need to insert the line global data inside the function

BTW, there's a python convention that function names should only user lower case letters and the underscore. If you name your functions differently, you code will run but confuse other people reading it.

576i
  • 7,579
  • 12
  • 55
  • 92