开发者

Variables and function

Hey all, is it possible to call a function value instead of calling the whole function?As, if i call the whole function, it will run unnecessarily which i do not want. For example:

def main():
    # Inputing the x-value for the first start point of the line
    start_point_x_1()
    # Inputing the x-value for the 2nd end point of the line
    end_point_x_2()
    # The first output point calculated and printed
    first_calculated_point()

def start_point_x_1():
    return raw_input("Enter the x- value for the 1st " +
                        "start point for the line.\n")

def end_point_x_2():
    return raw_input("Enter the x- value for the 2nd " +
                      "end point for the line.\n")

def first_calculated_point():
    x0 = int(start_point_x_1())
    a  = int(end_point_x_2()) - int(start_point_x_1())
    lamda_0 = 0
    x = x0 + (lamda_0)*a

main()

The code above works but when i reach the function first_calculated_poin开发者_JS百科t and when i calculate x0, the function start_point_x_1() runs again.I tried storing the function like ' for example x1 = raw_input("Enter the x- value for the 1st " + "start point for the line.\n") under the function start_point_x_1() but when i call the variable x1 at x0 = x1, they said x1 is not defined. Is there any way to store the value of the function and call it instead of calling the whole function?


Change

start_point_x_1()

to

x0 = start_point_x_1()

Similarly, do

x2 = end_point_x_2()

and finally:

first_calculated_point()

becomes

first_calculated_point(x0, x2)

The definition of the function changes to:

def first_calculated_point(x0, x2):
    a  = int(x2) - int(x0)
    lamda_0 = 0
    x = x0 + (lamda_0)*a

main()

Is this what you want? The idea is that you need to save the values taken from the user and then pass them to the function doing the calculation.

If this is not what you want, you need to explain yourself more, (and good indentation will help, particularly because indentation is significant in Python!).


Why are you calling start_point_x_1 and end_point_x_2 from both main and first_calculated_point?

You could change the definitions of main

def main():
  first_calculated_point()

and first_calculated_point:

def first_calculated_point():
  x0 = int(start_point_x_1())
  a  = int(end_point_x_2()) - x0
  lamda_0 = 0
  x = x0 + (lamda_0)*a

  # did you mean to return x?

Note that in the assignment to a, I replaced int(start_point_x_1()) with the variable to which the same expression was assigned on the previous line, but you can do this safely only when the expression doesn't have side effects, such as printing to the screen or reading input from the user.


You can use 'memoization' that is cache the result of functions based on function arguments, for that you can write a decorator so that you can decorate whatever functions you think need that behaviour but if you problem is as simple as your code is why not assign it a variable an d used that assigned values?

e.g

x0 = int(start_point_x_1())
a  = int(end_point_x_2()) - x0 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜