寻找assignement运营商 [英] Looking for assignement operator

查看:69
本文介绍了寻找assignement运营商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


是否有一个分配操作员,我可以覆盖?


等级MyInt:

def __init __(self,val):

断言(isinstance(val,int))

self._val = val


a = MyInt(10)

#这里我需要覆盖分配运营商

a = 12

谢谢

Alexander

Hello,

is there a assignement operator, that i can overwrite?

class MyInt:
def __init__(self, val):
assert(isinstance(val, int))
self._val = val

a = MyInt(10)

# Here i need to overwrite the assignement operator
a = 12
Thanks
Alexander

推荐答案

2006年10月17日,Alexander Eisenhuth< ne ****** @ stacom-software.dewrote :
On 10/17/06, Alexander Eisenhuth <ne******@stacom-software.dewrote:

您好,


是否有可以覆盖的分配运营商?
Hello,

is there a assignement operator, that i can overwrite?



Soirry,不,赋值是一个声明,而不是运算符,并且不能被覆盖。


- -

干杯,

Simon B
si***@brunningonline.net
http://www.brunningonline.net/simon / blog /




Alexander Eisenhuth写道:

Alexander Eisenhuth wrote:

你好,


有没有一个分配运营商,我可以覆盖?
Hello,

is there a assignement operator, that i can overwrite?



你不能覆盖赋值运算符,但你可以

覆盖数字对象的方法:

http://docs.python.org/ref/numeric-types。 HTML


HTH,

Rob

You can''t overwrite assignment operator, but you can
overwrite methods of numeric objects:

http://docs.python.org/ref/numeric-types.html

HTH,
Rob


Alexander Eisenhuth a écrit:
Alexander Eisenhuth a écrit :

您好,


是否有可以覆盖的分配运营商?
Hello,

is there a assignement operator, that i can overwrite?



添加到Simon Brunning的回复(赋值是一个声明)。

Adding to Simon Brunning reply (assignment is a statement).


class MyInt:

def __init __(self,val):

断言(isinstance(val,int))

self._val = val

a = MyInt(10)


a = 12
class MyInt:
def __init__(self, val):
assert(isinstance(val, int))
self._val = val

a = MyInt(10)

# Here i need to overwrite the assignement operator
a = 12



在这里你将12(Python int值)绑定到name''a'',然后''a''具有

int类型,而不是你的MyInt(这个值)已经丢失了。


您可以定义一个''set''方法,然后写:

a = MyInt(10)

a.set(12)


并且,如果a是另一个班级的成员,你可以为

定义一个a的访问者'该类中的成员,在给出int值时会自动调用你的set方法




ba = MyInt(10)

ba = 12 --- baset(12)


A +


Laurent。

Here you bind the 12 (Python int value) to name ''a'', then ''a'' has the
int type, not your MyInt (which value has been lost).

You may define a ''set'' method, and write:
a = MyInt(10)
a.set(12)

And, if a is a member of another class, you may define an accessor for
that ''a'' member in that class, which automatically call your set method
when giving an int value.

b.a = MyInt(10)
b.a = 12 ---b.a.set(12)

A+

Laurent.


这篇关于寻找assignement运营商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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