【发布时间】:2015-12-24 15:26:56
【问题描述】:
我有以下课程:
classes/helper.py
import json
class Helper:
def uJSONEncode(_, dict):
print(type(_))
return json.dumps(dict).decode('unicode-escape')
我将类实例化如下:
Python 2.7.9 (default, Feb 10 2015, 03:28:08)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from classes.helper import Helper
>>> h = Helper()
>>> h.uJSONEncode({"Asd": "asd"})
<type 'instance'>
\u'{"Asd": "asd"}'
为什么 python 将实例化对象作为第一个参数传递(我假设是)?我将如何避免这种行为?
【问题讨论】:
-
如果您不需要它作为方法,那么don't make it a method。
-
如果类不保存任何状态,你为什么要创建它?
-
我使用了一个类,因为我不想污染全局范围。那么在这种情况下我该如何命名函数呢?
-
将函数放在模块的命名空间中并没有污染它,而是在使用它。见this question。
-
from classes import helper
标签: python