可能的常量赋值运算符":="和“:: =”对于Python [英] Possible constant assignment operators ":=" and "::=" for Python
问题描述
嗨Python人,
首先我要道歉,我不是很有经验
Python程序员所以请原谅我,如果以下文字没有任何意义。
我一直缺少Python语言中的常量。有一些
可用的变通方法,例如const-module。对我来说,这看起来非常麻烦且非常不直观。对于口译员而言,在
效率方面,我无法分辨。
对于解决方案,我想出了两个新的赋值运算符给
语言会创建一个常量名和常量值:让's
欢迎新的常量赋值运算符:=和:: =
:=赋值运算符表示声明名称为常量
和不可变。但是,可能会有一些副作用,并且
常数可能并不总是恒定,如下所示:
a = [1,2,3 ]
b:= [4,5,6]#将[4,5,6]分配给''b''并将名称''b''声明为
是恒定的和不可变的
c:=#将a分配给c,并声明名称c是
常量 ;和不可变的
d = c#将''c''分配给''d''和''''继承
" immutable" -attribute来自''c'',但''d''可以分配
之后
e = d#assign''d''到''e''和' 'e''从''d'继承
immutable-attribute,但是'e''可以分配
以后
a.append(b)
打印#打印[1,2,3,4,5,6]
打印c#打印[1,2 ,3,4,5,6]因为副作用
打印d#打印[1,2,3,4,5,6]以及
print e#prints [1,2,3,4,5,6]这里没有魔法
c = b#''c''不能被指定为''c''是常数
c:= b#''c''不能重新定义为''c''
常数
c。 append(7)#无法完成,因为''c''是不可变的
d.appen d(7)#不能完成,因为''d''是不可变的因为它是从''c'继承的
。
e.append(7)#不能完成,因为''e''是不可变的因为它是继承自''d'的
。
d = [7,8,9]#正常变量赋值因为''d''不是
声明为常数
d.append(10)#now现在允许,因为''d''不是不可变的更多
:: = copy& assign-operator表示制作
右侧对象的副本并声明名称保持不变和
不可变的。这将给我们一个真正的恒定对象,不能改变b
。示例如下:
a = [1,2,3]
b = [4,5,6]
c :: = a#制作'a'的副本并将新副本分配给''c''和
声明名称''c''是一个常数和不可变的
d = c#将''c''分配给''d''和''''继承
" immutable" -attribute来自''c'',但''d''可以分配
以后
e:= d#将''d''分配给''e''和声明名称''e''是
常数和不可变
a.append(b)
打印#打印[1,2,3,4,5,6]
print c#将[1,2,3]打印为''a''和'c''是两个不同的
对象,因为:: =
打印d#打印[1,2,3]这里没有魔法
打印e#打印[1,2,3]这里没有魔法
c = b#' 'c''不能被指定为''c''是常数
c:= b#''c''不能重新定义为''c''是
常数
c.append(7)#无法完成,因为''c''是不可变的
d.append(7 )#不能完成,因为''d''是不可变的因为它是从''c'继承的
。
e.append(7)#不能完成,因为''e''是不可变的因为它是继承自''d'的
。
d = [7,8,9]#正常变量赋值因为''d''不是
声明为常数
d.append(10)#now现在允许,因为''d''不是不可变的更多
:=运算符将具有计算效率,因为它只对现有对象创建了一个
引用,但它可能会受到副作用。
:: =效率较低,因为它在现有对象上复制,但是
给出了真正的常量对象。
这对你有什么意义,还是有一些致命的问题我只是偶然忽略了?
Br,
>
TS
Hi Pythonians,
To begin with I''d like to apologize that I am not very experienced
Python programmer so please forgive me if the following text does not
make any sense.
I have been missing constants in Python language. There are some
workarounds available, for example the const-module. To me, this looks
quite cumbersome and very unintuitive. For the interpreter, in the
efficiency-wise, I just cannot tell.
For the solution I came up with two new assignment operators to the
language which would create a constant name and constant value: Let''s
welcome the new constant assignment operators := and ::=
The := assignment operator says that "declare the name to be constant
and immutable". However, there might be some side-effects and the
constant may not always be constant as you will see below:
a = [1, 2, 3]
b := [4, 5, 6] # assign [4,5,6] to ''b'' and declare name ''b'' to
be "constant" and "immutable"
c := a # assign ''a'' to ''c'' and declare that name ''c'' is
"constant" and "immutable"
d = c # assign ''c'' to ''d'' and ''d'' inherits the
"immutable"-attribute from ''c'', but ''d'' can be assigned
later
e = d # assign ''d'' to ''e'' and ''e'' inherits the
"immutable"-attribute from ''d'', but ''e'' can be assigned
later
a.append( b )
print a # prints [1, 2, 3, 4, 5, 6]
print c # prints [1, 2, 3, 4, 5, 6] because of the side-effect
print d # prints [1, 2, 3, 4, 5, 6] as well
print e # prints [1, 2, 3, 4, 5, 6] no magic here
c = b # ''c'' cannot be assigned as ''c'' is "constant"
c := b # ''c'' cannot be redefined either as ''c'' is
"constant"
c.append( 7 ) # cannot be done as ''c'' is "immutable"
d.append( 7 ) # cannot be done as ''d'' is "immutable" as it was
inherited from ''c''.
e.append( 7 ) # cannot be done as ''e'' is "immutable" as it was
inherited from ''d''.
d = [7, 8, 9] # normal variable assignment because ''d'' was not
declared to be a "constant"
d.append( 10 ) # now allowed as ''d'' is not "immutable" any more
The ::= copy&assign-operator says that "make a copy of the
right-hand-side object and declare the name to be constant and
immutable ". This would give us a true constant object which cannot be
changed. Example follows:
a = [1, 2, 3]
b = [4, 5, 6]
c ::= a # make copy of ''a'' and assign that new copy to ''c'' and
declare that name ''c'' is a "constant" and "immutable"
d = c # assign ''c'' to ''d'' and ''d'' inherits the
"immutable"-attribute from ''c'', but ''d'' can be assigned
later
e := d # assign ''d'' to ''e'' and declare that name ''e'' is
"constant" and "immutable"
a.append( b )
print a # prints [1, 2, 3, 4, 5, 6]
print c # prints [1, 2, 3] as ''a'' and ''c'' are two different
objects because of the ::=
print d # prints [1, 2, 3] no magic here
print e # prints [1, 2, 3] no magic here either
c = b # ''c'' cannot be assigned as ''c'' is "constant"
c := b # ''c'' cannot be redefined either as ''c'' is
"constant"
c.append( 7 ) # cannot be done as ''c'' is "immutable"
d.append( 7 ) # cannot be done as ''d'' is "immutable" as it was
inherited from ''c''.
e.append( 7 ) # cannot be done as ''e'' is "immutable" as it was
inherited from ''d''.
d = [7, 8, 9] # normal variable assignment because ''d'' was not
declared to be a "constant"
d.append( 10 ) # now allowed as ''d'' is not "immutable" any more
The := operator would be computionally efficient as it creates only a
reference to an existing object but it may suffer from side-effects.
The ::= is less efficient as it makes a copy on an existing object, but
gives truly constant objects.
Does this make any sense to you, or are there some fatal issues that I
just happened to overlook?
Br,
T.S.
推荐答案
ts ******* @ yahoo.com 写道:
首先,我想道歉,我不是很有经验的Python程序员所以如果以下文字没有任何意义,请原谅我。
我一直缺少Python语言中的常量。有一些可用的变通方法,例如const-module。对我来说,这看起来非常麻烦且非常不直观。对于口译员而言,在效率方面,我只是无法分辨。
To begin with I''d like to apologize that I am not very experienced
Python programmer so please forgive me if the following text does not
make any sense.
I have been missing constants in Python language. There are some
workarounds available, for example the const-module. To me, this looks
quite cumbersome and very unintuitive. For the interpreter, in the
efficiency-wise, I just cannot tell.
问题是,你为什么要错过它们?常量以其他语言提供各种用途,其中一些可能在Python中不值得b
。有一个关于''符号''的帖子一会儿
它涵盖了常量的许多用途。
-
Ben Sizer
The question is, why have you been missing them? Constants serve a
variety of purposes in other languages, some of which might not be
worthwhile in Python. There was a thread about ''symbols'' a while back
which covers many of the uses of constants.
--
Ben Sizer
ts ***** **@yahoo.com écrit:
嗨Python人,
首先,我想道歉,我不是很有经验如果以下文字没有任何意义,请原谅我。
我一直在用Python语言遗漏常量。
Hi Pythonians,
To begin with I''d like to apologize that I am not very experienced
Python programmer so please forgive me if the following text does not
make any sense.
I have been missing constants in Python language.
>
为什么会这样?
我想你在谈论命名(符号)常量?如果是这样,只需
遵循惯例:ALL_UPPERCASE中的名称是一个常量(或者
至少将被任何不想弄乱你的人的对待/>
包的实现)。不需要在这里添加额外的语法恕我直言。
Why so ?
I guess you''re talking about named (symbolic) constants ? If so, just
follow the convention : a name in ALL_UPPERCASE is a constant (or at
least will be treated as such by anyone not wanting to mess with your
package''s implementation). No need to add extra syntax here IMHO.
Bruno Desthuilliers写道:
Bruno Desthuilliers wrote:
ts ******* @ yahoo.com écrit:
嗨Python人,
首先,我我想道歉,我不是很有经验的Python程序员所以如果以下文字没有任何意义,请原谅我。
我一直在Python中缺少常量语言。
Hi Pythonians,
To begin with I''d like to apologize that I am not very experienced
Python programmer so please forgive me if the following text does not
make any sense.
I have been missing constants in Python language.
为什么会这样?
我想你在谈论命名(符号)常量?如果是这样,只需遵循约定:ALL_UPPERCASE中的名称是一个常量(或者至少将被任何不想弄乱你的
包的实现的人视为这样) 。不需要在这里添加额外的语法恕我直言。
Why so ?
I guess you''re talking about named (symbolic) constants ? If so, just
follow the convention : a name in ALL_UPPERCASE is a constant (or at
least will be treated as such by anyone not wanting to mess with your
package''s implementation). No need to add extra syntax here IMHO.
您好Bruno,
例如:
Hi Bruno,
For example:
A = []#let'声明一个常量"这里
b = A#并让我们在这里分配常数
b.append('''')#OOPS!
c = A
打印A
[''1''] print b
[''1''] print c
A = [] # let''s declare a "constant" here
b = A # and let''s assign the constant here
b.append(''1'') # OOPS!
c = A
print A [''1''] print b [''1''] print c
[''1'' ]
如你所见,常数 A可以很容易地修改。但是如果
有一个直观的机制来声明一个符号是不可变的,那么就不会有这个问题。
Br,
- TS
[''1'']
As you can see, the "constant" A can be modified this easily. But if
there were an intuitive mechanism to declare a symbol to be immutable,
then there won''t be this problem.
Br,
- T.S.
这篇关于可能的常量赋值运算符":="和“:: =”对于Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!