[Python] Can't use function imported from separate file

Discussão em 'Python' iniciado por Stack, Dezembro 22, 2024.

  1. Stack

    Stack Membro Participativo

    My app.py contains a call to calculate() in logic.py:

    import os
    from flask import Flask, jsonify
    from flask_restful import Api, Resource, request
    from logic import calculate

    app = Flask(__name__)
    api = Api(app)

    class calculate (Resource):
    def get(self):
    print("Calculation requested")
    output = calculate()
    print(output)
    return output

    api.add_resource(calculate, '/calculate')

    if __name__ == "__main__":
    app.run()


    import json
    def calculate(input):
    dic = {"aha": "Aha... " + input, "buaha": "Haha!"}
    print("Returning...")
    return json.dumps(dic)


    I've noticed several unexpected issues but I can't diagnose it properly (only relying on examples like this, this and this).

    • The printout in calculate method doesn't happen, as if not executed.
    • When I pass an actual value to it, I get error about taking no arguments.
    • The printout in get shows <app.calculate object at 0x0000012D2CDCE540>.

    It seems that I somehow get a reference to the function, not the actual function itself. So there's no execution (as it's not invoked), it's not possible to pass data to it and printing it somehow shows it's an address.

    What am I missing?

    Continue reading...

Compartilhe esta Página