在python中如何在没有定义的情况下调用函数

2024-04-19 17:43:28 发布

您现在位置:Python中文网/ 问答频道 /正文

程序如下:

import sys

def IndexSearchString():
    '''

    This function search a string in a password file through index and gives the result.

    :return: none
    :return type: none 
    :author: Neeraj    

    '''
    fieldindex = int(sys.argv[1])-1
    stringsrch = sys.argv[2]

    file_name = open("passwd", "r")

    for store_file in file_name:
        temp = store_file.split(":")
        search = temp[fieldindex]
        #print search 

        if stringsrch in search:
            print store_file
            #print sys.stdout.write(store_file)   
            return

#IndexSearchString() 

Tags: storenamein程序nonesearchreturnsys
1条回答
网友
1楼 · 发布于 2024-04-19 17:43:28

确保您的代码与问题中的代码完全相同(包括@Haidro的编辑)

当您将代码粘贴到问题中时,代码表明您的缩进是这样的:

def my_function():
    '''
    docstring
    '''
code_intended_for_my_function()

#my_function()

这将导致执行code_intended_for_my_function。这是“有效的”,因为docstring使my_fuction的定义有效(它什么也不做),然后立即执行code_intended_for_my_function。为了测试这一点,在您拥有的代码中,删除docstring,并检查您是否得到了IndentationError,或者只是从问题中复制当前的代码,看看它是否如您所期望的那样工作。你知道吗

相关问题 更多 >