T
T
Tremo2016-12-08 14:37:19
Flask
Tremo, 2016-12-08 14:37:19

How to pass a variable to a python function?

Good day.
How can I pass a variable to the "add_numbers" function from another function.
The difficulty is that the add_numbers function is already returning a different value with eturn jsonify(result=v).
How can I accept a variable in the add_numbers function?
Sample code below

@app.route('/_add_numbers')
def add_numbers():   
     return jsonify(result=<b>v</b>)


Suppose I want to send value 1 from here:
@app.route('/home/')
def home():
    v = 1


It is necessary that the add_numbers function takes data from the home function and substitutes it in place of the variable v

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Danil Biryukov-Romanov, 2016-12-08
@urtow

I am not an architecture guru, but why not implement this option:

@app.route('/_add_numbers')
def add_numbers():   
      _home=get_home()
     return jsonify(result=<b>_home</b>)

@app.route('/home/')
def home():
    return get_home()

def get_home():
    return 1

M
Magomed Magomedov, 2016-12-08
@GebekovAS

If I understand you correctly, then:

class Foo:
  __data=''
  
  def jsonify(self,param1):
      return param1+'_ok'
  
  @property
  def add_numbers(self):
    return self.jsonify(self.__data)
  
  @add_numbers.setter
  def add_numbers(self,value):
    self.__data=value
    
def home():
  e=Foo()
  e.add_numbers = 'test'
  print (e.add_numbers)

home()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question