Python 在另一个函数中使用返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40801789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:00:37  来源:igfitidea点击:

Using return value inside another function

pythonpython-2.7

提问by pylearner

I have got two functions. Please see the functions below;

我有两个功能。请参阅以下功能;

 def check_channel_number(self):

    print "***************Channel Checker *********************" 

    print ''

    user_channel_number = int(re.sub('\D', '', raw_input("Enter a channel number, (3digit): "))[:3]);

    channel = ("channelNr= '%d'") % (user_channel_number)

    print channel

    # channel_search = channel + str(user_channel_number)

    datafile = file('output.txt')

    found = False

    for line in datafile:

        if channel in line:

            found = True 
            print 'The channel number you entered is correct and will be deleted'
            return user_channel_number

    print 'The channel number you entered is not on the planner'
    return False

Just want to use returning value of (user_channel_number) in the following function.

只想在以下函数中使用 (user_channel_number) 的返回值。

   def delete_events(self):



    if user_channel_number == True:

        return 'The program number is correct and will be deleted'

        # action = 'DeleteEvent'
        menu_action = 'all'
        book = 'RECYC:687869882'
        arg_list = [('C:\Users\yke01\Documents\StormTest\Scripts\Completed'
                        '\Utils\UPNP_Client_Cmd_Line.py')]
        arg_list.append(' --action=')
        arg_list.append(action)
        arg_list.append(' --ip=')
        arg_list.append('10.10.8.89')
        arg_list.append(' --objectId=')
        arg_list.append(book)

        subprocess.call(["python", arg_list])

        print 'The program deleted successfully'

When I run my script, it says user_channel_numberis not defined globally ? How can I call user_channel_numberinside the delete_events()function?

当我运行我的脚本时,它说user_channel_number不是全局定义的?如何user_channel_numberdelete_events()函数内部调用?

回答by Jason

When you define a variable inside of a function, it is a local variable, meaning that it can only be accessed within that function.

当您在函数内部定义变量时,它是一个局部变量,这意味着它只能在该函数内访问。

Within a Class

一个班级内

It looks like you're inside a class, so you can make the variable accessible to all methods in the class by defining it like this:

看起来您在一个类中,因此您可以通过如下定义来使该变量可供类中的所有方法访问:

def check_channel_number(self):

    self.user_channel_number = ...

And then in your second function, you can use it like the following:

然后在你的第二个函数中,你可以像下面这样使用它:

def delete_events(self):

    if self.user_channel_number:


Outside of a class

课外

If you aren't using methods inside of a class, you can instead use the globalbuiltin.

如果您不在类内部使用方法,则可以改用global内置方法。

For example,

例如,

def check_channel_number():

    global user_channel_number 
    user_channel_number = ...

def delete_events():

    if user_channel_number:

        ...


Using a value returned from a function

使用从函数返回的值

Instead in your first function, check_channel_number(), you can have it return user_channel_number. You can then call that function inside of delete_events(), like the following:

相反,在您的第一个函数中check_channel_number(),您可以让它返回user_channel_number。然后,您可以在 中调用该函数delete_events(),如下所示:

def check_channel_number():

    user_channel_number = ...
    return user_channel_number

def delete_events():

    if check_channel_number():

        ...

回答by Eugene Primako

Functions can not share their local variables. You can return the value from the first and pass it to the second:

函数不能共享它们的局部变量。您可以从第一个返回值并将其传递给第二个:

def check_channel_number(self):
    ...
    return user_channel_number

def delete_events(self):
    user_channel_number = self.check_channel_number()
    ...

Or save value on the object:

或者在对象上保存值:

def check_channel_number(self):
    ...
    self.user_channel_number = user_channel_number
    ...

def delete_events(self):
    if self.user_channel_number:
         ....

回答by RichSmith

So I think when you call the check_channel_numberfunction, user_channel_numberis defined in there, so when you call delete_events, it has gone out of scope, maybe something like this would help?

所以我认为当你调用check_channel_number函数时,user_channel_number在那里定义,所以当你调用时delete_events,它已经超出了范围,也许这样的事情会有所帮助?

user_channel_number = check_channel_number()
delete_events()

I'd probably have the user_channel_number as an input to the delete function too, so it would turn into this: (where ucn is the user_channel_number)

我也可能将 user_channel_number 作为删除函数的输入,所以它会变成这样:(其中 ucn 是user_channel_number

def delete_events(self, ucn):
    if ucn == True:
        print 'The program number is correct and will be deleted'

        # action = 'DeleteEvent'
        menu_action = 'all'
        book = 'RECYC:687869882'
        arg_list = [('C:\Users\yke01\Documents\StormTest\Scripts\Completed'
                    '\Utils\UPNP_Client_Cmd_Line.py')]
        arg_list.append(' --action=')
        arg_list.append(action)
        arg_list.append(' --ip=')
        arg_list.append('10.10.8.89')
        arg_list.append(' --objectId=')
        arg_list.append(book)

        subprocess.call(["python", arg_list])

        print 'The program deleted successfully'

I have also changed `return 'The program number is correct and will be deleted'' to a print statement as I have a feeling the return would end the function before the other lines of code would be run

我还将“返回“程序编号正确,将被删除”更改为打印语句,因为我感觉返回将在运行其他代码行之前结束函数

So the code would probably end up being something like:

所以代码最终可能是这样的:

user_channel_number = check_channel_number()
delete_events(user_channel_number)

EDIT:

编辑:

just noticed it looks like your functions are part of a class,

刚刚注意到看起来你的函数是一个类的一部分,

in that case, you could do:

在这种情况下,你可以这样做:

self.ucn = self.check_channel_number()
self.delete_events(self.ucn)

(or if you dont want to pass the user_channel_number into the function you could change if user_channel_number:to if self. user_channel_number:

(或者,如果您不想将 user_channel_number 传递到函数中,您可以更改if user_channel_number:if self. user_channel_number: