我有下面的類被傳遞:Python的進口類的實例化對象
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"}'
爲什麼蟒通(我假設是)實例化對象作爲第一個參數?我將如何避免這種行爲?
如果你不需要它是一種方法,那麼[不要使它成爲一種方法](http://dirtsimple.org/2004/12/python-is-not-java.html)。 –
爲什麼你創建的課程,如果它不會保持任何狀態? – BrenBarn
我使用了一個類,因爲我不想污染全球範圍。那麼我在這種情況下如何命名空間函數呢? – Carey