简体   繁体   English

IronPython中的默认成员/属性

[英]Default members/properties in IronPython

Is there a possibility to make IronPython access the default property of an object if the object instance is (directly) used? 如果(直接)使用对象实例,是否可以使IronPython访问对象的默认属性?

The values accessed by IronPython are (in our program) wrapped in some class Variable<T> object, always having a property of type T named Value. IronPython访问的值(在我们的程序中)包装在某些class Variable<T>对象中,该对象始终具有名为Value的T类型的属性。 When I write 当我写

if someBoolVariable:
    print "Hello World"

IronPython checks if someBoolVariable != null , which in our case always is true. IronPython检查someBoolVariable != null ,在我们的情况下始终为true。 Instead, I would prefer if it checks someBoolVariable.Value . 相反,我希望它是否检查someBoolVariable.Value

The same is true for numeric variables: 对于数字变量也是如此:

Temp = someDoubleVariable + someDoubleVariable

returns an error that the add operator is not implemented in the type. 返回一个错误,指出该类型未实现add运算符。 I would like to have it automatically evaluated to Temp = someDoubleVariable.Value + someDoubleVariable.Value . 我想让它自动评估为Temp = someDoubleVariable.Value + someDoubleVariable.Value

I still want to be able to access the other properties of the object. 我仍然希望能够访问该对象的其他属性。 Therefore, it is not posssible to convert the object when passing out of the scope. 因此,超出范围时无法转换对象。 I've already tried to attach the DefaultMemberAttribute and/or the DefaultPropertyAttribute, but that does not seem to have any effect... 我已经尝试附加DefaultMemberAttribute和/或DefaultPropertyAttribute,但这似乎没有任何效果。

Edit: 编辑:

For bool variables (which is one important group of them), I have found my answer. 对于布尔变量(这是其中的重要一组),我找到了答案。 However, for numeric values, this would be nice too. 但是,对于数值,这也很好。 In theory, I could create methods for all arithmetic operations, but there are a very large number of them - especially when mixing different types... It would be nicer to have something like a __value__() or __self__() method (which do not seem to exist) which simply returns the value to be used instead of the object... 从理论上讲,我可以为所有算术运算方法,但也有一个非常大的数量的人-混合不同类型的尤其是当......这将是更好的有东西像一个__value__()__self__()方法(它做似乎不存在),它只是返回要使用的值而不是对象...

I found a partial solution for boolean statements and expressions: To make variables behave the way I expect them, you only have to implement a special function named __nonzero__() : 我找到了布尔语句和表达式的部分解决方案:要使变量按我期望的方式运行,您只需实现一个名为__nonzero__()的特殊函数:

public bool __nonzero__()
{
     return EqualityComparer<T>.Default.Equals(_value, default(T)) == false;
}

If the object is now used in boolean statements and expressions, it's value is evaluated instead of checking if it is null . 如果现在在布尔语句和表达式中使用该对象,则将对它的值求值,而不是检查它是否为null

One drawback: The object that is implementing this method has to be public, otherwise it is ignored by python. 一个缺点:实现此方法的对象必须是公共的,否则python将忽略它。 There is maybe also be a possibility to implement it as a static function, however I'm not sure how to register it to python so that it is used. 也可能将其实现为静态函数,但是我不确定如何将其注册到python以便使用。

IronPython doesn't support default properties (I think VB is the only language that does). IronPython不支持默认属性(我认为VB是唯一的语言)。 Your best bet is to implement the operators on Variable<T> using dynamic , which should make it manageable. 最好的选择是使用dynamicVariable<T>上实现运算符,这应该使其易于管理。 If you overload them in normal C# style IronPython will use them, or you can implement Python's __*__ methods. 如果以常规C#样式重载它们,IronPython将使用它们,或者可以实现Python的__*__方法。

For example (untested): 例如(未测试):

public static dynamic operator +(Value<t> v, dynamic d) {
  return v._value + d;
}

(Heh, static dynamic .) (嘿, static dynamic 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM