Python错误对象()在将python2.7升级到python3.4后不接受任何参数

Python错误对象()在将python2.7升级到python3.4后不接受任何参数,python,python-3.x,singleton,python-3.3,Python,Python 3.x,Singleton,Python 3.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

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类用于Singleton模式

错误类型:

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)
该错误发生在python版本3.4中,但不在python版本2.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)
我就是这样给全班打电话的

myhosts=hostname.InventoryMgmt(envscriptpath)
这也清楚地显示在错误消息中

怀疑:

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)
即使存在使用一个参数的init()方法,为什么 是它抛出了那个错误。最重要的是,错误并没有发生 当它在python2.7上运行时


我已经确认这不是制表符和间距的问题,我看到很多答案对这个问题表示怀疑,因此

您应该删除这些额外的参数。在python2.7中,如果将额外参数传递给
对象。\uuuuu new\uuuuu
,将发出
弃用警告
,但是,如果使用
-Wdefault
开关运行脚本,您将看到:

$ 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
$python2-Wdefault singleton.py
py:13:DeprecationWarning:object()不接受任何参数
class.\u instance=object.\u新建(class.*args,**kwargs)
真的

由于python3.3,此警告已转换为错误。

您应该删除这些额外的参数。在python2.7中,如果将额外参数传递给
对象。\uuuuu new\uuuuu
,将发出
弃用警告
,但是,如果使用
-Wdefault
开关运行脚本,您将看到:

$ 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
$python2-Wdefault singleton.py
py:13:DeprecationWarning:object()不接受任何参数
class.\u instance=object.\u新建(class.*args,**kwargs)
真的

由于python3.3,此警告已转换为错误。

@georgexsh将很快回复您ASAP@georgexsh我会尽快给你回复