我有一个C#类看起来有点像:
public class MyClass
{
private Func<IDataCource, object> processMethod = (ds) =>
{
//default method for the class
}
public Func<IDataCource, object> ProcessMethod
{
get{ return processMethod; }
set{ processMethod = value; }
}
/* Other details elided */
}
Run Code Online (Sandbox Code Playgroud)
我有一个IronPython脚本,可以在应用程序中运行
from MyApp import myObj #instance of MyClass
def OtherMethod(ds):
if ds.Data.Length > 0 :
quot = sum(ds.Data.Real)/sum(ds.Data.Imag)
return quot
return 0.0
myObj.ProcessMethod = OtherMethod
Run Code Online (Sandbox Code Playgroud)
但是当ProcessMethod被调用(在IronPython之外)时,在这个赋值之后,运行默认方法.
我知道脚本是运行的,因为脚本的其他部分都有效.
我该怎么做?
我正在学习Go并且无法理解一件事,为什么这种语言的创作者支持同时分配?很容易犯a,b = a,b而不是a,b = b,a这样的错误,正如我想要的那样,提前感谢任何好的解释.
我有一个我想要分配的字节数组,如下所示:
(byte)string.length()string.getBytes()除了使用for循环之外,还有一种使用来自两个不同变量的字节初始化字节数组的快捷方法吗?
基本上,更好的做法是在第一次运行时将值存储到变量中,还是继续使用该值?代码将更好地解释它:
TextWriter tw = null;
if (!File.Exists(ConfigurationManager.AppSettings["LoggingFile"]))
{
// ...
tw = File.CreateText(ConfigurationManager.AppSettings["LoggingFile"]);
}
Run Code Online (Sandbox Code Playgroud)
要么
TextWriter tw = null;
string logFile = ConfigurationManager.AppSettings["LoggingFile"].ToString();
if (!File.Exists(logFile))
{
// ...
tw = File.CreateText(logFile);
}
Run Code Online (Sandbox Code Playgroud) 作为一个小练习,我开始在python中使用数字代码,我正在尝试制作LDLT算法.只是为了"弄湿脚".
但是我似乎缺乏对numpy数组的基本理解.请参阅以下示例:
def ldlt(Matrix):
import numpy
(NRow, NCol) = Matrix.shape
for col in range(NCol):
Tmp = 1/Matrix[col,col]
for D in range(col+1, NCol):
Matrix[col,D] = Matrix[D,col]*Tmp
if __name__ == '__main__':
import numpy
A = numpy.array([[2,-1,0],[-1,2,-1],[0,-1,2]])
ldlt(A)
Run Code Online (Sandbox Code Playgroud)
这个例子不是我正在处理的完整代码.但是,尝试运行它,并在Matrix [col,D] = ...设置断点.
我对第一次评估的期望是第0行第1列(起始值-1)被设置为等于= -1*(1/2)= -0.5.
但是在运行代码时,它似乎设置为0.为什么?必须有一些基本的东西,我还没有真正理解?
在此先感谢所有帮助我的人.
Python Ver.:3.3 Tmp.:变为0.5(由我的调试器报告).
这是我上一个问题的后续问题.
我收到了这个答案
function foo($arg1 = null, $arg2 = null) {
// if they are not set retrieve from a session
$arg1 = ($arg1 !== null) ? $arg1 : $_SESSION['arg1'];
// rinse repeat.
}
Run Code Online (Sandbox Code Playgroud)
所以我想,也许我可以通过阻止在每个函数上调用相同的变量来减少代码行.
我想出了这个.
function foo($arg1 = $_SESSION['user_id'], $arg2 = $_SESSION['campaign_id']) {
//do something
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,我收到了这个错误.
Parse error: syntax error, unexpected T_VARIABLE
Run Code Online (Sandbox Code Playgroud)
它有结果导致这个问题吗?我只是用一个带有值的变量替换了null.
有没有办法分配一个函数,该函数的参数已经有一个值而不是赋值为null?
PS
我正在尝试使用输出数据echo $campaign->getName(),因此在调用函数时没有提供参数.
所以我试图调用这样的函数
function foo($arg1 = $_SESSION['user_id'], $arg2 = $_SESSION['campaign_id']) {
//do something
}
Run Code Online (Sandbox Code Playgroud)
如果没有任何方法可以一次又一次地调用相同的变量.
任何帮助,将不胜感激.:)
我有一个java代码片段如下:
int arr[] = new int[5];
int index = 0;
arr[index] = index = 3;
System.out.println("arr[0] = " + arr[0]);
System.out.println("arr[3] = " + arr[3]);
Run Code Online (Sandbox Code Playgroud)
输出是:
arr[0] = 3
arr[3] = 0
Run Code Online (Sandbox Code Playgroud)
第3行究竟发生了什么?
当Swift中的函数被赋值给一个变量或作为嵌套函数从高级函数返回时,它是通过值还是通过引用传递的?我写的时候:
func foo() -> Bool
{
return false
}
var funcVar = foo
Run Code Online (Sandbox Code Playgroud)
确实funcVar收到了对"funcVar"名称的引用foo(),或者是foo()创建并存储在内存中的副本?以下代码的相同问题:
func otherfoo() -> (Int) -> ()
{
func bar(num :Int) {}
return bar
}
var funcVar = otherfoo()
Run Code Online (Sandbox Code Playgroud)
第二个例子特别困惑我,因为,如果我调用funcVar(3)我不应该能够访问bar的情况下函数是通过引用分配/返回的,因为bar它位于同一范围内的另一个函数中funcVar,但它仍然有效(来自C++)背景我只是惊讶它编译).有人可以帮我解释一下这个问题吗?
function pass-by-reference pass-by-value variable-assignment swift
GO中是否存在显式数组初始化(声明和赋值),或者唯一的方法是使用速记运算符?这是一个实际的例子 - 这两个是平等的:
a := [3]int{1, 0, 1}
var a [3]int = [3]int{1, 0, 1}
Run Code Online (Sandbox Code Playgroud) 我试图在一个函数中调用一个函数if,将值保存在变量中并检查该值是否等于某个值.试过:
def funk():
return ("Some text")
msg=""
if (msg=funk()) == "Some resut":
print ("Result 1")
Run Code Online (Sandbox Code Playgroud)
我收到语法错误.可以这样做吗?
我来自C,我知道这可行:
#include <stdio.h>
char funk()
{
return 'a';
}
int main()
{
char a;
if( a=funk() == 'a' )
printf("Hello, World!\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这在python中可能吗?
PS功能很简单,因此更容易理解我想要的东西.我不会在程序中使用某些功能.如果你给我一个MINUS请在评论中解释,以便我将来可以提出更好的问题
arrays ×4
c# ×2
function ×2
go ×2
java ×2
python ×2
conditional ×1
declaration ×1
delegates ×1
if-statement ×1
ironpython ×1
null ×1
numpy ×1
operation ×1
parameters ×1
php ×1
simultaneous ×1
swift ×1