我正在构建一个python包,它基本上是请求包的包装器,旨在使对一组数据库的各种api调用变得更容易。你知道吗
当前,我的包具有以下目录结构:
package\
- __init__.py
- coreFunc.py
subpackage\
- __init__.py
- mod1.py
- mod2.py
包的主要功能在于mod1.py
和mod2.py
。mod1.py
看起来像这样:
import requests as _requests
from package.coreFunc import __func1
class DataPull:
def __init__(self, arg1):
self._url = "http://www.somesite.com/info?"
self._api_param = {'argument':__func1(arg1)}
self._pull = _requests.get(self._url, params = self._api_param)
def DataTableOne(self):
_headers = self._pull.json()['resultSets'][0]['headers']
_values = self._pull.json()['resultSets'][0]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
def DataTableTwo(self):
_headers = self._pull.json()['resultSets'][1]['headers']
_values = self._pull.json()['resultSets'][1]['rowSet']
return [dict(zip(_headers, value)) for value in _values]
在coreFunc.py
中,我有一些函数需要在mod1.py
和mod2.py
中的特定类中使用。事实上,在类定义的第三行中,我使用在coreFunc
中定义的函数(__func1
)修改参数的用户输入,以确保将正确的值传递给api调用。你知道吗
你知道吗coreFunc.py公司看起来像这样:
def __func1(x):
if str(x) == "1999":
return "1999-00"
elif len(str(x)) == 4:
try:
return "-".join([str(x),str(int(x) % 100 + 1)])
except:
raise Exception("Enter the four-digit year")
else: raise Exception("Enter the four-digit year")
我为这个调用使用一个类,因为调用会产生多个数据表,并且我使用方法来访问每个数据表。你知道吗
我的问题是,当我尝试创建DataPull类的对象时:
newObj = DataPull(argument)
显示以下错误:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-24-e3399cf393bd> in <module>()
----> 1 traceback.extract_stack(package.mode1.DataPull("203112"))
C:\Users\Bradley\Anaconda3\lib\site-packages\test-package-py3.4.egg\package\subpackage\mod1.py in __init__(self, arg1)
140 self._url = "http://www.somesite.com/info?"
--> 141 self._api_param = {"Season":__func1(arg1)}
NameError: name '_DataPull__func1' is not defined
如何将__func1
正确导入mod1.py以修复此错误?你知道吗
函数前面有两个下划线意味着它是私有的。您可以将它重命名为
_func1
,它就可以工作了。你知道吗或者,导入
__func1
,如下所示:在
__init__
中使用_func1
而不是__func1
。你知道吗这是name mangling的一个例子,在类定义中,任何在它前面有
__
的东西都被更改为_classname__attr
。引用文件:这是在源代码级别完成的,因此可以将类定义中要使用的函数名更改为其他名称,这样就可以正常工作。你知道吗
来自compile.c的相关CPython代码:
相关问题 更多 >
编程相关推荐