[英]Python error object() takes no parameters after upgrading python2.7 to python3.4
bashhostlist.py 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
The Singleton class is for singleton pattern . Singleton类用于Singleton模式 。
Error type: 错误类型:
The error occurs in python version3.4 but not in python2.7 该错误发生在python version3.4中,但未在python2.7中发生
Error Log: 错误日志:
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)
This is how I am making the call to the class 这就是我打电话给全班的方式
myhosts=hostname.InventoryMgmt(envscriptpath)
which is also clearly shown in the error message 错误消息中也清楚显示了
Doubts: 疑问:
Even though the init () method exists that takes one parameter why is it throwing that error. 即使存在采用一个参数的init ()方法,为什么也会抛出该错误。 And most importantly the error was not there when it was run on python2.7 最重要的是,在python2.7上运行时错误不存在
I have confirmed its not the tab and spacing problem, I saw many answers doubting on that problems so 我已经确认它不是制表符和间距问题,我看到许多答案对此问题表示怀疑
You should remove those extra arguments. 您应该删除那些多余的参数。 In python2.7 if you pass extra arguments to object.__new__
, a DeprecationWarning
will be issued, but this warning has been suppressed by default , if you run your script with -Wdefault
switch, you will see: 在python2.7中,如果将额外的参数传递给object.__new__
,则会发出DeprecationWarning
,但是默认情况下此警告已被抑制 ,如果使用-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
Since python3.3 this warning was converted to an error. 从python3.3开始,此警告已转换为错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.