【发布时间】:2018-02-12 17:46:57
【问题描述】:
如何将以下 Matlab 代码转换为 Python? Python中是否有与Matlab的varargin和nargin等效的函数?
function obj = Con(varargin)
if nargin == 0
return
end
numNodes = length(varargin);
obj.node = craft.empty();
obj.node(numNodes,1) = craft();
for n = 1:numNodes
% isa(obj,ClassName) returns true if obj is an instance of
% the class specified by ClassName, and false otherwise.
% varargin is an input variable in a function definition
% statement that enables the function to accept any number
% of input arguments.
if isa(varargin{n},'craft')
obj.node(n) = varargin{n};
else
error(Invalid input for nodes property.');
end
end
end
【问题讨论】:
-
幸好你不是在寻找
varargout——这在 Python 中是不可能的。 -
@user2357112。它是,但实施方式不同。在 MATLAB 中,您可以访问 nargout。在 Python 中,可以传入标志来指示您想要哪些输出(就像 scipy 经常做的那样),或者接受所有输出并自己丢弃不需要的输出。
-
@OP。使用
*args,其中len(args)是nargin