当我将一个类导入另一个文件时,将其转换为并调用其中一个方法,它会运行两次
班级本身是:
class DBAuth(object):
def __init__(self, driver=None, username=None, password=None,
host='localhost', port=None, database=None):
self.driver = driver
self.username = username
self.password = password
self.host = host
self.port = port
self.database = database
def test_method(self):
teststr = "database is '{0}' and your user is '{1}'".format(
self.database, self.username)
return teststr
我正在使用它:
from lbauth.model import DBAuth
dbauth = DBAuth(
driver = 'postgresql',
username = 'user',
password = 'passwd',
host = 'localhost',
port = '5432',
database = 'dbname'
)
print(dbauth.test_method())
返回:
>database is 'dbname' and your user is 'user'
>database is 'dbname' and your user is 'user'
为什么它的行为如此,以及正确调用此方法的正确方法是什么?
答案 0 :(得分:0)
我发现了问题。
事实证明我使用__init__.py
来导入和测试脚本,这是一种不好的做法。