多个构造函数 [英] Multiple constructors
问题描述
如果你愿意,可以称之为C ++程序员挂断。
我似乎无法在我的矩阵中定义__init__的多个版本
class(即从值列表或2维中初始化
(行/列))。
即使Python无法'不能解决__init__在论证的基础上使用
类型肯定可以根据参数数字这样做???
无论如何 - 任何建议我如何编码这个????
谢谢
Phil
Call this a C++ programmers hang-up if you like.
I don''t seem to be able to define multiple versions of __init__ in my matrix
class (ie to initialise either from a list of values or from 2 dimensions
(rows/columns)).
Even if Python couldn''t resolve the __init__ to use on the basis of argument
types surely it could do so on the basis of argument numbers???
At any rate - any suggestions how I code this????
Thanks
Phil
推荐答案
菲利普史密斯写道:
我似乎无法在我的矩阵
类中定义__init__的多个版本(即从一个类中初始化值列表或来自2维
(行/列))。
I don''t seem to be able to define multiple versions of __init__ in my matrix
class (ie to initialise either from a list of values or from 2 dimensions
(rows/columns)).
您可以使用带* args的if语句:
类Matrix(对象):
def __init __(self, * args):
如果len(args)== 1:
#从值列表初始化
elif len(args)== 2:
#从行/列初始化
else:
引发TypeError(构造函数接受1或2个参数。)
或者有两种不同的功能:
class Matrix(对象):
def __init __(self,values) :
#从值列表初始化
@classmethod
def from_pair(self,rows,columns):
返回矩阵([rows,columns])#或者使用正确的参数
You could either use an if statement with *args:
class Matrix(object):
def __init__(self, *args):
if len(args) == 1:
# Initialize from list of values
elif len(args) == 2:
# Initialize from rows/columns
else:
raise TypeError("Constructor accepts 1 or 2 arguments.")
Or with two different functions:
class Matrix(object):
def __init__(self, values):
# Initialize from a list of values
@classmethod
def from_pair(self, rows, columns):
return Matrix([rows, columns]) # Or with the right argument
Philip Smith写道:
Philip Smith wrote:
调用此方法如果你愿意的话,C ++程序员会挂断电话。
我似乎无法在我的矩阵
类中定义多个版本的__init__(即从一个列表中初始化值或来自2维
(行/列))。
即使Python无法解析__init__在辩论的基础上使用nt
类型肯定它可以在参数数字的基础上这样做???
无论如何 - 任何建议我如何编码这个????
检查参数的数量并不是那么难:
class Klass:
def __init__ (* args):
self.args = args
if len(self.args)== 1:
#etc. />
但这感觉相当不合理。也许你可以使用工厂
功能,忘记__init__一起(2.2或更高):
class Klass(对象):
def fromList(seq):
result = Klass()
#在这里填充属性
#并返回请求的对象
返回结果
fromList = staticmethod(fromList)
def fromDimensions(cols,rows):
结果= Klass()
#在这里填充属性
#并返回请求的对象
返回结果
fromDimensions = staticmethod(fromDimensions)
#more methods here
k = Klass.fromList(seq)
等..
问候
-
Vincent Wehren
谢谢
Phil
Call this a C++ programmers hang-up if you like.
I don''t seem to be able to define multiple versions of __init__ in my matrix
class (ie to initialise either from a list of values or from 2 dimensions
(rows/columns)).
Even if Python couldn''t resolve the __init__ to use on the basis of argument
types surely it could do so on the basis of argument numbers???
At any rate - any suggestions how I code this????
Checking the number of arguments ain''t all that hard:
class Klass:
def __init__(*args):
self.args = args
if len(self.args) == 1:
# etc.
This feels rather unpythonic, though. Maybe you could use factory
functions, forgetting about __init__ all together (2.2 or higher):
class Klass(object):
def fromList(seq):
result = Klass()
# populate attributes here
# and return the requested object
return result
fromList = staticmethod(fromList)
def fromDimensions(cols, rows):
result = Klass()
# populate attributes here
# and return the requested object
return result
fromDimensions = staticmethod(fromDimensions)
#more methods here
k = Klass.fromList(seq)
etc..
Regards
--
Vincent Wehren
Thanks
Phil
Leif K-Brooks写道:
Leif K-Brooks wrote:
@classmethod
def from_pair(self,rows,columns):
@classmethod
def from_pair(self, rows, columns):
return Matrix([rows, columns]) # Or with the right argument
呃...我不知道为什么我将这个参数命名为自我,如果你不想混淆任何阅读你代码的人,那么它应该是cls
。
Er... I''m not sure why I named that argument "self", it should be "cls"
if you don''t want to confuse anyone reading your code.
这篇关于多个构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!