方法在python中执行两次 [英] Method executed twice in python

查看:285
本文介绍了方法在python中执行两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下带有静态方法的类:

I have the following class with a static method:

class Helper:
     @staticmethod
    def LookForCuiInLocal(disease, language):
        conn = mysql.connector.connect(stuff here)

        cursor = conn.cursor(buffered=True)

        findLocalQuery = ("SELECT umls_concept_id from translation WHERE source_text = '{}'".format(disease))
        print("hey")

        try:
            cursor.execute(findLocalQuery)
            resultList = cursor.fetchone()[0]
            cursor.close()
            conn.close()
            return resultList
        except (mysql.connector.Error, TypeError) as e:
            print("Error when finding local CUI : {}".format(e))
            return None

print(Helper().LookForCuiInLocal("paradentose", "da"))

我有一个运行查询的数据库,它可能会返回我需要的字符串.如果它不包含字符串,则该方法失败并返回 None.

I have a database where i run the query and it might return a string i need. If it doesn't contain the string the method fails and returns None.

我得到以下输出:

hey
c0031099
hey
c0031099

为什么这个方法会执行两次?我希望有人能帮助我.

Why is the method executed twice? I hope someone can help me with this.

我使用 PyCharm 作为我的 IDE

I'm using PyCharm as my IDE

更改为以下对问题没有帮助:

Changing to the following does not help the issue:

def LookForCuiInLocal(disease, language):
    conn = mysql.connector.connect(stuff here)

    cursor = conn.cursor(buffered=True)

     findLocalQuery = ("SELECT umls_concept_id from translation WHERE source_text = '{}'".format(disease))
     print("hey")

      try:
          cursor.execute(findLocalQuery)
          resultList = cursor.fetchone()[0]
          cursor.close()
          conn.close()
          return resultList
    except (mysql.connector.Error, TypeError) as e:
          print("Error when finding local CUI : {}".format(e))
          return None

print(Helper().LookForCuiInLocal("paradentose", "da"))

但是,从另一个脚本调用 LookForCuiInLocal() 不会使该方法运行两次

However, calling LookForCuiInLocal() from another script does not make the method run twice

推荐答案

假设我们有包含此代码的文件 foo.py

Suppose we have the file foo.py containing this code

print("running from", __name__)
import foo

当你运行文件(它是程序的main入口点)时,它以__main__的名字运行,当它被导入时它运行文件第二次在名称 foo 下,所以输出将是

When you run the file (it is the main entry point for the program) it is run under the name __main__, when it is imported it runs the file a second time under the name foo, so the output would be

running from __main__
running from foo

您很少有文件导入本身,通常这种情况发生在循环导入中 - 当 foo 将导入 bar 将导入 foo 时.

Rarely do you have a file import itself, usually this happens with circular imports - when foo will import bar which will import foo.

为了让模块具有可导入的代码和仅在主程序时运行的代码,请将仅主代码放在条件中:

For the module to have code that should be importable and code that should only be run when it is the main program, put the main-only code inside the condition:

if __name__ == "__main__":

这将导致它仅在模块是入口点时运行,而在导入时不会运行.

This will cause it to only run when the module is the entry point and not when it is imported.

这篇关于方法在python中执行两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆