Python 将 python2.7 升级到 python3.4 后,错误 object() 不带参数
Python error object() takes no parameters after upgrading python2.7 to python3.4
bashhostlist.py
from __future__ import print_function
import subprocess
import sys
import logging
class Singleton(object):
"""
Creating a single object that will work for all the plugin
We create an object with an attrubute of the file path which is same for all the plugins
So rather than creating new object for all the same object works so making singleton
"""
_instance = None
def __new__(class_, *args, **kwargs):
if not isinstance(class_._instance, class_):
class_._instance = object.__new__(class_, *args, **kwargs)
return class_._instance
class InventoryMgmt(Singleton):
hostlist = {'hostcontent':None}
def __init__(self, path):
""" bash file path where environement are set """
self._scriptpath = path
Singleton class 用于单例模式。
错误类型:
The error occurs in python version3.4 but not in python2.7
错误日志:
2017-11-03 03:09:39,187 - root - INFO - Plugin started
2017-11-03 03:09:39,187 - root - INFO - Trying to fetch from etc_shadow_plugin
2017-11-03 03:09:39,187 - root - ERROR - object() takes no parameters
Traceback (most recent call last):
File "/home/sjoshi/python3.4/check-acess/plugins/plugin_etc_shadow/plugin_etc_shadow.py", line 91, in run
listofhost=phelper.getallthehosts(hostnamewithoutdollar, envscriptpath)
File "/home/sjoshi/python3.4/check-acess/lib/sshplugin/sshplugin.py", line 172, in getallthehosts
myhosts=hostname.InventoryMgmt(envscriptpath)
File "/home/sjoshi/python3.4/check-acess/lib/inventory/bashhostlist.py", line 16, in __new__
class_._instance = object.__new__(class_, *args, **kwargs)
这就是我调用 class
的方式
myhosts=hostname.InventoryMgmt(envscriptpath)
错误信息中也清楚显示
疑问:
Even though the init() method exists that takes one parameter why
is it throwing that error. And most importantly the error was not
there when it was run on python2.7
我已经确认这不是制表符和间距问题,我看到很多回答都怀疑这个问题所以
你应该删除那些额外的参数。在 python2.7 中,如果您将额外的参数传递给 object.__new__
,则会发出 DeprecationWarning
,但是 this warning has been suppressed by default,如果您 运行 您的脚本带有 [=13] =]切换,你会看到:
$ python2 -Wdefault singleton.py
singleton.py:13: DeprecationWarning: object() takes no parameters
class_._instance = object.__new__(class_, *args, **kwargs)
<__main__.InventoryMgmt object at 0x106a2fd90> <__main__.InventoryMgmt object at 0x106a2fd90> True
自 python3.3 起,此警告已转换为错误。
bashhostlist.py
from __future__ import print_function
import subprocess
import sys
import logging
class Singleton(object):
"""
Creating a single object that will work for all the plugin
We create an object with an attrubute of the file path which is same for all the plugins
So rather than creating new object for all the same object works so making singleton
"""
_instance = None
def __new__(class_, *args, **kwargs):
if not isinstance(class_._instance, class_):
class_._instance = object.__new__(class_, *args, **kwargs)
return class_._instance
class InventoryMgmt(Singleton):
hostlist = {'hostcontent':None}
def __init__(self, path):
""" bash file path where environement are set """
self._scriptpath = path
Singleton class 用于单例模式。
错误类型:
The error occurs in python version3.4 but not in python2.7
错误日志:
2017-11-03 03:09:39,187 - root - INFO - Plugin started
2017-11-03 03:09:39,187 - root - INFO - Trying to fetch from etc_shadow_plugin
2017-11-03 03:09:39,187 - root - ERROR - object() takes no parameters
Traceback (most recent call last):
File "/home/sjoshi/python3.4/check-acess/plugins/plugin_etc_shadow/plugin_etc_shadow.py", line 91, in run
listofhost=phelper.getallthehosts(hostnamewithoutdollar, envscriptpath)
File "/home/sjoshi/python3.4/check-acess/lib/sshplugin/sshplugin.py", line 172, in getallthehosts
myhosts=hostname.InventoryMgmt(envscriptpath)
File "/home/sjoshi/python3.4/check-acess/lib/inventory/bashhostlist.py", line 16, in __new__
class_._instance = object.__new__(class_, *args, **kwargs)
这就是我调用 class
的方式myhosts=hostname.InventoryMgmt(envscriptpath)
错误信息中也清楚显示
疑问:
Even though the init() method exists that takes one parameter why is it throwing that error. And most importantly the error was not there when it was run on python2.7
我已经确认这不是制表符和间距问题,我看到很多回答都怀疑这个问题所以
你应该删除那些额外的参数。在 python2.7 中,如果您将额外的参数传递给 object.__new__
,则会发出 DeprecationWarning
,但是 this warning has been suppressed by default,如果您 运行 您的脚本带有 [=13] =]切换,你会看到:
$ python2 -Wdefault singleton.py
singleton.py:13: DeprecationWarning: object() takes no parameters
class_._instance = object.__new__(class_, *args, **kwargs)
<__main__.InventoryMgmt object at 0x106a2fd90> <__main__.InventoryMgmt object at 0x106a2fd90> True
自 python3.3 起,此警告已转换为错误。