简体   繁体   English

在Python中,如何在第二个函数中使用函数的return语句而不使用全局变量?

[英]In Python, how can I use a function's return statement within a second function without using global variables?

I'm working on an assignment in which the constraints specify that I cannot use global variables. 我正在做一个分配,其中约束指定我不能使用全局变量。 Variables are only defined for the specific function. 仅为特定功能定义变量。 I'm wondering if there is a way to take what a function returns (a string) and then create a new function that can use that string, without global variables? 我想知道是否有一种方法可以接受一个函数返回的内容(一个字符串),然后创建一个可以使用该字符串而无需全局变量的新函数? Note: I CAN use variables, just not globals. 注意:我可以使用变量,但不能使用全局变量。 Thanks! 谢谢!

Per request, here is an example. 根据请求,这是一个示例。

def func1():
     return "output"
def func2():
     #loop over the string "output",just for example
>>> def a():
...     return 'x'
... 
>>> def b(s):
...     print 'b recieved', s
... 
>>> b(a())
b recieved x

Why not pass the variable to the new function as a parameter? 为什么不将变量作为参数传递给新函数?

eg 例如

s = someFunc1()

someFunc2(s)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在其他函数中使用函数的返回值(不使用类) - How to use function's return within other function (without using class) 如何使用在一个 function 中声明的局部变量并将其实现到另一个 function? (没有全局变量或调用函数) - How can I use a local variable declared in one function and implement it to another function? (Without global variables or calling the function) 在不使用全局变量的情况下,如何在新函数中使用返回的结果? - How can you use a returned result in a new function without using global variables? 如何在不使用全局变量的情况下编写重复动作 function? - How can I write a repeating movement function without using global variables? 在Python中,如何获取函数中使用的全局变量? - In Python, how can I get the global variables that are used in a function? 如何使用与上述函数相同的 Word? 如果没有办法做到这一点,我如何在不使用全局变量的情况下使用相同的随机数? - How can I use the same Word as in the function above? If ther's no way to do it how can I use the same random number without using a global variable? 在函数中使用python中的全局变量 - Using global variables in python in a function 在python函数中使用全局变量 - Using global variables in python function 如何访问嵌套函数中更多全局函数中的变量? (蟒蛇) - How to access variables in more global functions within a nested function? (Python) 检测python函数中的所有全局变量? - Detect all global variables within a python function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM