我在C#4.0中有一个接口和一个抽象基类:
public interface IScript
{
ScriptResources Resources { get; set; }
string MessageLog { get; }
void Init();
ScriptResult Execute();
void Cleanup();
}
public abstract class Script : IScript
{
public ScriptResources Resources { get; set; }
public string MessageLog { get; protected set; }
public abstract void Init();
public ScriptResult Execute()
{
try
{
Init();
ScriptResult = RunScript();
}
finally
{
Cleanup();
}
return ScriptResult;
}
protected abstract ScriptResult RunScript();
public abstract void Cleanup();
}
在IronPython方面,我有以下继承自C#Script类的类:
import clr
clr.AddReference("MyCSharpLibrary.dll")
from MyCSharpLibrary import *
class IronPythonScript(Script):
def __new__(self):
return Script.__new__(self)
def __init__(self):
return Script.__init__(self)
def Init(self):
Script.LogMessage(self, "< IronPython: Init() >")
Script.LogMessage(self, "")
# Do stuff
def RunScript(self):
Script.LogMessage(self, "< IronPython: RunScript() >")
# Do stuff
def Cleanup(self):
Script.LogMessage(self, "< IronPython: Cleanup() >")
# Do stuff
但是,为了在IronPython中使用基类脚本属性(MessageLog,Resources),我必须使用以下相当丑陋的语法,这对我来说是错误的:
Script.MessageLog.__set__(self, "TEST VALUE")
print Script.MessageLog.__get__(self)
我做错了什么? (注意:我对Python的经验有限)
答案 0 :(得分:1)
怎么样:
self.MessageLog = "TEST VALUE"
print self.MessageLog