来自从另一个继承的 class 的异常 "object.__init__() takes exactly one argument"
Anomalous "object.__init__() takes exactly one argument" from a class that inherit from another
我不明白是什么导致了 object.__init__
的异常调用
非常感谢您的帮助。
这里是代码。我包括了所有代码,除了无用的文本,因为我不知道是什么导致了错误
class IdXY(bclasses.GenStructData):
_dtype_names=('idx', 'x', 'y');
_dtype_nfld=len(_dtype_names);
_dtype_formats=(str, float, float);
_dtype_nlines=1;
_dtype_delimiter=' ';
_str_format="{idx:>12} {x:10f} {y:10f}";
(idx,idn, x,y)='-',0, 0.,0.;
withid=True;
def _check_def(self, check_value=True):
"""Function to check if the class is defined correctly
""";
_name_='IdXY object';
if(self._dtype_nlines!=1): raise ValueError(_name_+": `_dtype_nlines` for this king of object can be just 1. Use the multi-lines version");
if(not self.withid):
if('idx' in self._dtype_names):
if(self._dtype_names.index('idx')==0):
self._dtype_names=self._dtype_names[1:];
self._dtype_formats=self._dtype_formats[1:];
else: raise SkZpipeError(_name_+"...", exclocus=_name_);
super()._check_def(check_value=check_value); #=>bclasses.GenStructData
###__INIT__###
def __init__(self, data=None, withid=None, idn=0):
"""IdXY object with 2D coordinates and eventually an ID.
""";
_name_='IdXY object';
if(data is None): raise ValueError(_name_+": No data to create the object");
self.idx="-";
if(withid is not None): self.withid=withid;
print(self.__class__.__mro__, flush=True); #added to check
super().__init__(data=data);
那就没有重要代码了。
运行 我得到了:
> IdXY("1 2 3")
(<class 'skzpipe.parameters.classes.fileclass.basefile.IdXY'>, <class 'skzpipe.parameters.classes.bclasses.GenStructData'>, <class 'object'>)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/skz/SkZ_pipeline/SkZpipe/skzpipe/parameters/classes/fileclass/basefile.py", line 562, in __init__
super().__init__(data=data);
TypeError: object.__init__() takes exactly one argument (the instance to initialize)
为什么代码跳转到object.__init__()
而不是执行GenStructData.__init__()
? GenStructData
不使用 super
.
有些异常是因为:
> issubclass(skzpipe.parameters.classes.fileclass.basefile.IdXY,
> skzpipe.parameters.classes.bclasses.GenStructData)
> False
并且我将IdXY
定义为GenStructData
的子类。但是:
> skzpipe.parameters.classes.fileclass.basefile.IdXY.__mro__
> (<class 'skzpipe.parameters.classes.fileclass.basefile.IdXY'>, <class 'skzpipe.parameters.classes.bclasses.GenStructData'>, <class 'object'>)
编辑:
GenStructData 有一个 __init__
class GenStructData():
_dtype_names=((), ());
_dtype_datanames=None;
_dtype_nlines=len(_dtype_names);
_dtype_formats=((), )*_dtype_nlines;
_dtype_nfield=tuple(len(x) for x in _dtype_names);
_dtype_delimiter=(None,)*_dtype_nlines;
_str_format=("", "");
_str_len=(None, None);
###__INIT__###
def _check_def(self, check_value=True):
pass;
def __init__(self, data=None, check_default=True, attributes=None):
"""An object from multi-lined structured data. This is not a class for direct use.""";
_name_='GenStructData object';
if(data is None or not isinstance(data,(str,list,tuple,GenStructData,numpy.ndarray))):
raise TypeError(_name_+": Wrong type for `data` to create the object <{data}>".format(data=data));
if(not self._dtype_datanames):
self._dtype_datanames=self._dtype_names;
self._check_def(check_value=check_default);
[...]
我猜 GenStructData 没有 __init__
。在这种情况下,它将从 object
继承 __init__
,因此会出现您看到的错误。
顺便说一句,Python 不在语句末尾使用分号。您必须习惯其他语言。你应该删除它们;虽然它们不是语法错误,但它们不是惯用语。
原因是没有 pythonic 方法来解决由嵌套导入引起的属性错误。
bclasses.py(定义 GenStructData 的地方)开始于
"""Generic classes.""";
import os, re, math, io, copy;
import multiprocessing as multiprc;
import psutil, numpy;
from sys import stdout, stderr;
#Predefinition to avoid Attribute Error
class GenStructData:
pass;
from ...exceptions import *
from .. import basicpar as bpar
from .. import options as _opt
from ... import functions as funct
将 from ... import functions as funct
移动到使用它的代码的 2 部分(实际上应该这样做)并删除空定义解决了问题
skzpipe.parameters.classes.fileclass.basefile.IdXY("1 2 3")
1 2.000000 3.000000
issubclass(skzpipe.parameters.classes.fileclass.basefile.IdXY,skzpipe.parameters.classes.bclasses.GenStructData)
True
最初的空定义以某种方式弄乱了内部结构。
异常是 IdXY
也有一个空的预定义,但它的 __init__
被正确调用
我不明白是什么导致了 object.__init__
的异常调用
非常感谢您的帮助。
这里是代码。我包括了所有代码,除了无用的文本,因为我不知道是什么导致了错误
class IdXY(bclasses.GenStructData):
_dtype_names=('idx', 'x', 'y');
_dtype_nfld=len(_dtype_names);
_dtype_formats=(str, float, float);
_dtype_nlines=1;
_dtype_delimiter=' ';
_str_format="{idx:>12} {x:10f} {y:10f}";
(idx,idn, x,y)='-',0, 0.,0.;
withid=True;
def _check_def(self, check_value=True):
"""Function to check if the class is defined correctly
""";
_name_='IdXY object';
if(self._dtype_nlines!=1): raise ValueError(_name_+": `_dtype_nlines` for this king of object can be just 1. Use the multi-lines version");
if(not self.withid):
if('idx' in self._dtype_names):
if(self._dtype_names.index('idx')==0):
self._dtype_names=self._dtype_names[1:];
self._dtype_formats=self._dtype_formats[1:];
else: raise SkZpipeError(_name_+"...", exclocus=_name_);
super()._check_def(check_value=check_value); #=>bclasses.GenStructData
###__INIT__###
def __init__(self, data=None, withid=None, idn=0):
"""IdXY object with 2D coordinates and eventually an ID.
""";
_name_='IdXY object';
if(data is None): raise ValueError(_name_+": No data to create the object");
self.idx="-";
if(withid is not None): self.withid=withid;
print(self.__class__.__mro__, flush=True); #added to check
super().__init__(data=data);
那就没有重要代码了。 运行 我得到了:
> IdXY("1 2 3")
(<class 'skzpipe.parameters.classes.fileclass.basefile.IdXY'>, <class 'skzpipe.parameters.classes.bclasses.GenStructData'>, <class 'object'>)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/skz/SkZ_pipeline/SkZpipe/skzpipe/parameters/classes/fileclass/basefile.py", line 562, in __init__
super().__init__(data=data);
TypeError: object.__init__() takes exactly one argument (the instance to initialize)
为什么代码跳转到object.__init__()
而不是执行GenStructData.__init__()
? GenStructData
不使用 super
.
有些异常是因为:
> issubclass(skzpipe.parameters.classes.fileclass.basefile.IdXY,
> skzpipe.parameters.classes.bclasses.GenStructData)
> False
并且我将IdXY
定义为GenStructData
的子类。但是:
> skzpipe.parameters.classes.fileclass.basefile.IdXY.__mro__
> (<class 'skzpipe.parameters.classes.fileclass.basefile.IdXY'>, <class 'skzpipe.parameters.classes.bclasses.GenStructData'>, <class 'object'>)
编辑:
GenStructData 有一个 __init__
class GenStructData():
_dtype_names=((), ());
_dtype_datanames=None;
_dtype_nlines=len(_dtype_names);
_dtype_formats=((), )*_dtype_nlines;
_dtype_nfield=tuple(len(x) for x in _dtype_names);
_dtype_delimiter=(None,)*_dtype_nlines;
_str_format=("", "");
_str_len=(None, None);
###__INIT__###
def _check_def(self, check_value=True):
pass;
def __init__(self, data=None, check_default=True, attributes=None):
"""An object from multi-lined structured data. This is not a class for direct use.""";
_name_='GenStructData object';
if(data is None or not isinstance(data,(str,list,tuple,GenStructData,numpy.ndarray))):
raise TypeError(_name_+": Wrong type for `data` to create the object <{data}>".format(data=data));
if(not self._dtype_datanames):
self._dtype_datanames=self._dtype_names;
self._check_def(check_value=check_default);
[...]
我猜 GenStructData 没有 __init__
。在这种情况下,它将从 object
继承 __init__
,因此会出现您看到的错误。
顺便说一句,Python 不在语句末尾使用分号。您必须习惯其他语言。你应该删除它们;虽然它们不是语法错误,但它们不是惯用语。
原因是没有 pythonic 方法来解决由嵌套导入引起的属性错误。
bclasses.py(定义 GenStructData 的地方)开始于
"""Generic classes.""";
import os, re, math, io, copy;
import multiprocessing as multiprc;
import psutil, numpy;
from sys import stdout, stderr;
#Predefinition to avoid Attribute Error
class GenStructData:
pass;
from ...exceptions import *
from .. import basicpar as bpar
from .. import options as _opt
from ... import functions as funct
将 from ... import functions as funct
移动到使用它的代码的 2 部分(实际上应该这样做)并删除空定义解决了问题
skzpipe.parameters.classes.fileclass.basefile.IdXY("1 2 3")
1 2.000000 3.000000
issubclass(skzpipe.parameters.classes.fileclass.basefile.IdXY,skzpipe.parameters.classes.bclasses.GenStructData)
True
最初的空定义以某种方式弄乱了内部结构。
异常是 IdXY
也有一个空的预定义,但它的 __init__
被正确调用