建议重载assign运算符 [英] Suggesting for overloading the assign operator

查看:72
本文介绍了建议重载assign运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我一直在考虑如何超载赋值操作''=''。

在很多情况下,我想提供我的软件包的用户使用我为他们创建的扩展内置类型的自然

接口,但

assign运算符总是强制他们输入。或者为了设置变量的

值而进行简单分配时强制执行

结果。借用这个新组的一个例子,当用户只想更新c的值时,下面的

第二个赋值来自变量

''c''的所有货币知识。 ,不是它的类型:


c =货币(5)

c = 7


我完全理解的解决方法做c = Curreny(7)而不是

用户不想被它打扰。


我读过一些先前的讨论,但我没有看到建议

永久解决方案。我想提出以下建议。


我们的想法是通过引入一个新的运算符将值赋值与类型赋值分开,

,让'' s说'':=''运算符。


''=''保持原样,同时指定类型和值

' ':=''只分配值


例如:

Hi,

I have been thinking about how to overload the assign operation ''=''.
In many cases, I wanted to provide users of my packages a natural
interface to the extended built-in types I created for them, but the
assign operator is always forcing them to "type cast" or coerce the
result when they do a simple assign for the purpose of setting the
value of a variable. Borrowing an example from this newgroup, the
second assignment below ereases all Currency knowledge from variable
''c'', when the user only wanted to update the value of c, not its type:

c=Currency(5)
c=7

I fully understand the workaround of doing c=Curreny(7) instead, but
users don''t want to be bothered with that.

I read some of the prior discussions, but I did not see a suggestion
for permanent a solution. I would like to propose the following.

The idea is to separate the value assignment from the type assignment,
by introducing a new operator, let''s say the '':='' operator.

''='' remains what it is, assigns type and value at the same time
'':='' assigns only the value

For example:

class myint(int):传递
.... a = myint()
a:= 6
类型(a)

< class' '__main __敏。''> a
class myint(int): pass .... a = myint()
a := 6 type(a)
<class ''__main__.myint''> a



6


实际上,:=只会强制RHS的返回类型: =到

LHS上变量的类型:=,尽可能保留价值



如果变量的类型仍未定义,'':'''表现为

就像''=''。它可能会在未来的代码中造成混乱,但不会破坏
旧代码。


我认为这将是一个很好的功能。评论?


谢谢,

- Rim


6

In reality, := would just coerce the return type of the RHS of := to
the type of the variable on the LHS of the :=, preserving the value(s)
as best as it can.

If the type of the variable was still undefined, '':='' would behave
like ''=''. It might create confusion in future code, but does not break
old code.

I think it would be a nice feature to have. Comments?

Thanks,
- Rim

推荐答案



Rim < RI ******* @ yahoo.com>在消息中写道

新闻:6f ************************** @ posting.google.c om ...

"Rim" <ri*******@yahoo.com> wrote in message
news:6f**************************@posting.google.c om...


我一直在考虑如何重载赋值操作''=''。
Hi,

I have been thinking about how to overload the assign operation ''=''.




分配是一种陈述,而不是一个操作员,因此不会超过
。这是一个故意的设计决定,GvR将不会改变。


TJR



Assignment is a statement, not an operator, and therefore not
overloadable . This is an intentional design decision that GvR will
not change.

TJR




Rim < RI ******* @ yahoo.com>在消息中写道

新闻:6f ************************** @ posting.google.c om ...

"Rim" <ri*******@yahoo.com> wrote in message
news:6f**************************@posting.google.c om...


我一直在考虑如何重载赋值操作''=''。
在很多情况下,我想提供我的用户将自然
接口打包到我为它们创建的扩展内置类型,但
assign运算符始终强制它们键入强制转换。或者为了设置变量的
值而进行简单分配时强制结果。
Hi,

I have been thinking about how to overload the assign operation ''=''.
In many cases, I wanted to provide users of my packages a natural
interface to the extended built-in types I created for them, but the
assign operator is always forcing them to "type cast" or coerce the
result when they do a simple assign for the purpose of setting the
value of a variable.




使用属性。更改赋值的语义

语句(NOT赋值运算符)不会发生。


属性是Python 2.2中的新特性(大约一年)

现在很老了。)它们让你有一个getter和setter

方法,其接口看起来像一个实例

变量。 />

John Roth



Use a property. Changing the semantics of the assignment
statement (NOT assignment operator) is not going to happen.

Properties are new in Python 2.2 (which is around a year
old by now.) They enable you to have getter and setter
methods with an interface that looks like an instance
variable.

John Roth


" Terry Reedy" < TJ ***** @ udel.edu>在消息新闻中写道:< co ******************** @ comcast.com> ...
"Terry Reedy" <tj*****@udel.edu> wrote in message news:<co********************@comcast.com>...
" Rim" < RI ******* @ yahoo.com>在消息中写道
新闻:6f ************************** @ posting.google.c om ...
"Rim" <ri*******@yahoo.com> wrote in message
news:6f**************************@posting.google.c om...


我一直在考虑如何重载赋值操作''=''。
Hi,

I have been thinking about how to overload the assign operation ''=''.



作业是一个陈述,而不是操作员,因此不会过载。这是一个故意的设计决定,GvR将不会改变。



Assignment is a statement, not an operator, and therefore not
overloadable . This is an intentional design decision that GvR will
not change.




那么,提供另一个声明的想法是什么呢,我是
原帖中建议



''=''语句指定类型和值

'':=''语句仅指定值


Rim



Well, what about the idea of providing another statement, the one I
proposed in the original post?

''='' statement assigns type and value
'':='' statement assigns value only

Rim


这篇关于建议重载assign运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆