Python variable is declared and still gives error

Thinked

I want to assign the data from the request to the variable currentKampjson that is used multiple places in the code. But when I try to assign currentKampjson to anything it gets declared as a new variable, this applies to every variable.

currentKampjson is a dictionary type variable

currentKampjson = {"info":1, "info2":3}

@app.route("/stream/currentkamp")
def streamCurrentKamp():
    return render_template("stream/currentKamp.html", kamp = currentKampjson)

@app.route("/currentKamp/getKamp")
def getCurrentKamp():
    return jsonify(currentKamp)
    
print(currentKampjson)
@app.route("/currentKamp/update", methods=["POST"])
def updateCurrentKamp():
    indata = eval(request.data)
    
    currentKampjson = indata
    with open("jsonFiles/kamper/currentKamp.json","w") as f:
        json.dump(indata, f)
    return "SUCCESS"

This code runs fine, even though I am using currentKampjson here:

@app.route("/currentKamp/update", methods=["POST"])
def updateCurrentKamp():
    indata = eval(request.data)
    print(currentKampjson)
    #currentKampjson = indata
    with open("jsonFiles/kamper/currentKamp.json","w") as f:
        json.dump(indata, f)
    return "SUCCESS"

Wierd variable interaction

dukkee

In the current case, you need to use global keyword to your variable, but in a common case, global variables aren't a good practice.

More about scopes you can read e.g. here:

https://realpython.com/python-scope-legb-rule/ https://matthew-brett.github.io/teaching/global_scope.html

currentKampjson = {"info":1, "info2":3}

@app.route("/stream/currentkamp")
def streamCurrentKamp():
    global currentKampjson
    return render_template("stream/currentKamp.html", kamp=currentKampjson)

@app.route("/currentKamp/getKamp")
def getCurrentKamp():
    return jsonify(currentKamp)
    
print(currentKampjson)

@app.route("/currentKamp/update", methods=["POST"])
def updateCurrentKamp():
    global currentKampjson
    indata = eval(request.data)
    
    currentKampjson = indata

    with open("jsonFiles/kamper/currentKamp.json", "w") as f:
        json.dump(indata, f)

    return "SUCCESS"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

"number" not a declared variable but still works?

From Dev

Python: Assigning staticmethod to class variable gives error

From Dev

Error: Variable Not declared in this scope

From Dev

Declared Exception still throwing error must be caught or declared to be thrown

From Dev

variable declaration gives an error

From Dev

Not an error to use a variable before it is declared?

From Dev

Variable is declared but error messages says not

From Dev

Not an error to use a variable before it is declared?

From Dev

variable was not declared in this scope gcc error

From Dev

Python3.4: Opening file with mode 'w' still gives me FileNotFound error

From Dev

I declared my variable with let but it is still not in global scope

From Dev

Variable with function gives an error ajax

From Dev

php 5.5.9 gives error when subclass is declared before parent

From Dev

error: variable or field ‘myfunction’ declared void

From Dev

Unidentified identifier error when the variable is declared

From Dev

How to fix the 'variable' was not declared in this scope error?

From Dev

oracle Sql Developer Error Bind Variable not declared

From Dev

The variable declared but value is never used error

From Dev

Python Global variable gives NameError

From Dev

Python changing variable values declared in __init__

From Dev

rhc setup gives error `no such file dl/import` still

From Dev

This program gives me a circular import error but still works

From Dev

Updating image via Dispatcher still gives "different thread owns it" error

From Dev

Server gives 500 code error but still serves content

From Dev

Running Python 2.7 gives error

From Dev

Installing python gives dpkg error

From Dev

Python gives a "Syntax Error" on "else:"

From Dev

Python gives syntax error but there is no mistake?

From Dev

super() gives an error in Python 2

Related Related

  1. 1

    "number" not a declared variable but still works?

  2. 2

    Python: Assigning staticmethod to class variable gives error

  3. 3

    Error: Variable Not declared in this scope

  4. 4

    Declared Exception still throwing error must be caught or declared to be thrown

  5. 5

    variable declaration gives an error

  6. 6

    Not an error to use a variable before it is declared?

  7. 7

    Variable is declared but error messages says not

  8. 8

    Not an error to use a variable before it is declared?

  9. 9

    variable was not declared in this scope gcc error

  10. 10

    Python3.4: Opening file with mode 'w' still gives me FileNotFound error

  11. 11

    I declared my variable with let but it is still not in global scope

  12. 12

    Variable with function gives an error ajax

  13. 13

    php 5.5.9 gives error when subclass is declared before parent

  14. 14

    error: variable or field ‘myfunction’ declared void

  15. 15

    Unidentified identifier error when the variable is declared

  16. 16

    How to fix the 'variable' was not declared in this scope error?

  17. 17

    oracle Sql Developer Error Bind Variable not declared

  18. 18

    The variable declared but value is never used error

  19. 19

    Python Global variable gives NameError

  20. 20

    Python changing variable values declared in __init__

  21. 21

    rhc setup gives error `no such file dl/import` still

  22. 22

    This program gives me a circular import error but still works

  23. 23

    Updating image via Dispatcher still gives "different thread owns it" error

  24. 24

    Server gives 500 code error but still serves content

  25. 25

    Running Python 2.7 gives error

  26. 26

    Installing python gives dpkg error

  27. 27

    Python gives a "Syntax Error" on "else:"

  28. 28

    Python gives syntax error but there is no mistake?

  29. 29

    super() gives an error in Python 2

HotTag

Archive