我有以下课程:
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"})
\u'{"Asd":"asd"}'
为什么python(我假设是)将实例化的对象作为第一个参数传递?我该如何避免这种行为?
如果你不需要它成为一个方法,那就不要把它变成一个方法。
如果类不保持任何状态,为什么要创建它?
我使用了一个类,因为我不想污染全球范围。那么在这种情况下,我的名称空间如何工作呢?
将一个函数放入模块的名称空间并不会污染它,而是在使用它。看看这个问